src/Schema.php
public function requireOneOf(array $required, string $fieldname = '', int $count = 1) {
        $result = $this->addValidator(
            $fieldname,
            function ($data, ValidationField $field) use ($required, $count) {
                // This validator does not apply to sparse validation.
                if ($field->isSparse()) {
                    return true;
                }

                $hasCount = 0;
                $flattened = [];

                foreach ($required as $name) {
                    $flattened = array_merge($flattened, (array)$name);

                    if (is_array($name)) {
                        // This is an array of required names. They all must match.
                        $hasCountInner = 0;
                        foreach ($name as $nameInner) {
                            if (array_key_exists($nameInner, $data)) {
                                $hasCountInner++;
                            } else {
                                break;
                            }
                        }
                        if ($hasCountInner >= count($name)) {
                            $hasCount++;
                        }
                    } elseif (array_key_exists($name, $data)) {
                        $hasCount++;
                    }

                    if ($hasCount >= $count) {
                        return true;
                    }
                }

                if ($count === 1) {
                    $message = 'One of {properties} are required.';
                } else {
                    $message = '{count} of {properties} are required.';
                }

                $field->addError('oneOfRequired', [
                    'messageCode' => $message,
                    'properties' => $required,
                    'count' => $count
                ]);
                return false;
            }
        );

        return $result;
    }
public function requireOneOf(array $required, string $fieldname = '', int $count = 1) {
        $result = $this->addValidator(
            $fieldname,
            function ($data, ValidationField $field) use ($required, $count) {
                // This validator does not apply to sparse validation.
                if ($field->isSparse()) {
                    return true;
                }

                $hasCount = 0;
                $flattened = [];

                foreach ($required as $name) {
                    $flattened = array_merge($flattened, (array)$name);

                    if (is_array($name)) {
                        // This is an array of required names. They all must match.
                        $hasCountInner = 0;
                        foreach ($name as $nameInner) {
                            if (array_key_exists($nameInner, $data)) {
                                $hasCountInner++;
                            } else {
                                break;
                            }
                        }
                        if ($hasCountInner >= count($name)) {
                            $hasCount++;
                        }
                    } elseif (array_key_exists($name, $data)) {
                        $hasCount++;
                    }

                    if ($hasCount >= $count) {
                        return true;
                    }
                }

                if ($count === 1) {
                    $message = 'One of {properties} are required.';
                } else {
                    $message = '{count} of {properties} are required.';
                }

                $field->addError('oneOfRequired', [
                    'messageCode' => $message,
                    'properties' => $required,
                    'count' => $count
                ]);
                return false;
            }
        );

        return $result;
    }
public function requireOneOf ( array $ required , string $ fieldname = '' , int $ count = 1 ) { $ result = $ this -> addValidator ( $ fieldname , function ( $ data , ValidationField $ field ) use ( $ required , $ count ) { // This validator does not apply to sparse validation. if ( $ field -> isSparse ( ) ) { return true ; } $ hasCount = 0 ; $ flattened = [ ] ; foreach ( $ required as $ name ) { $ flattened = array_merge ( $ flattened , ( array ) $ name ) ; if ( is_array ( $ name ) ) { // This is an array of required names. They all must match. $ hasCountInner = 0 ; foreach ( $ name as $ nameInner ) { if ( array_key_exists ( $ nameInner , $ data ) ) { $ hasCountInner ++ ; } else { break ; } } if ( $ hasCountInner >= count ( $ name ) ) { $ hasCount ++ ; } } elseif ( array_key_exists ( $ name , $ data ) ) { $ hasCount ++ ; } if ( $ hasCount >= $ count ) { return true ; } } if ( $ count === 1 ) { $ message = 'One of {properties} are required.' ; } else { $ message = '{count} of {properties} are required.' ; } $ field -> addError ( 'oneOfRequired' , [ 'messageCode' => $ message , 'properties' => $ required , 'count' => $ count ] ) ; return false ; } ) ; return $ result ; }
Require one of a given set of fields in the schema.

@param array $required The field names to require.
@param string $fieldname The name of the field to attach to.
@param int $count The count of required items.
@return Schema Returns `$this` for fluent calls.
Require one of a given set of fields in the schema .