SchemaValidator
class SchemaValidator (View source)
A general purpose Array Validator which can check PHP arrays for validity according to some schema.
The schema format is adapted from the JSON Schema standard (http://json-schema.org)
Currently the Parts 5.1 -> 5.25 of the json-schema spec are implemented.
Variations from the spec:
- The "type" constraint is required for all properties.
- The validator only executes the checks that make sense for a specific type -> see list of possible constraints below.
- The "format" constraint for string type has additional class-name and instance-name options.
- The "dependencies" constraint of the spec is not implemented.
- Similar to "patternProperties" "formatProperties" can be specified specified for dictionaries
- Definition and referencing of local-types with the '@'-sign is added.
General constraints for all types (for implementation see validate Method):
- type
- disallow
- enum
Additional constraints for types:
- string: pattern, minLength, maxLength, format(date-time|date|time|uri|email|ipv4|ipv6|ip-address|host-name|class-name|interface-name)
- number: maximum, minimum, exclusiveMinimum, exclusiveMaximum, divisibleBy
- integer: maximum, minimum, exclusiveMinimum, exclusiveMaximum, divisibleBy
- boolean: --
- array: minItems, maxItems, items
- dictionary: properties, patternProperties, formatProperties, additionalProperties
- null: --
- any: --
Combine types to allow mixed types, the primitive type of the given value is used to determine which type to validate for:
- type: ['integer', 'string']
Local type-definitions: If a schema contains properties that start with an '@'-sign on the root level those schemas are added to the local type definitions and are passed down the validation chain to be refereced with the full identifier and can be used in the same way as simple type definition.
Local Types can not be overwitten but they can extend a list of superTypes.
Methods
Validate array with the given schema
Validate a value for a given type
Validate a value with a given list of allowed types
Validate an integer value with the given schema
Validate an integer value with the given schema
Validate a boolean value with the given schema
Validate an array value with the given schema
Validate a dictionary value with the given schema
Validate a string value with the given schema
Validate a null value with the given schema
Validate any value with the given schema. Return always a valid Result.
Create a string information for the given value
Create Error Object
Determine whether the given php array is a schema or not
Determine whether the given php array is a plain numerically indexed array
Determine whether the given php array is a Dictionary (has no numeric identifiers)
Details
Result
validate(mixed $value, mixed $schema, array $types = [])
Validate array with the given schema
The following properties are handled in given $schema:
- type : value is of given type or schema (array of schemas is allowed)
- disallow : value is NOT of given type or schema (array of schemas is allowed)
- enum : value is equal to one of the given values
protected Result
validateType(mixed $value, array $schema, array $types = [])
Validate a value for a given type
protected Result
validateTypeArray(mixed $value, array $schema, array $types = [])
Validate a value with a given list of allowed types
protected Result
validateNumberType(mixed $value, array $schema)
Validate an integer value with the given schema
The following properties are handled in given $schema:
- maximum : maximum allowed value
- minimum : minimum allowed value
- exclusiveMinimum : boolean to use exclusive minimum
- exclusiveMaximum : boolean to use exclusive maximum
- divisibleBy : value is divisibleBy the given number
protected Result
validateIntegerType(mixed $value, array $schema)
Validate an integer value with the given schema
The following properties are handled in given $schema:
- all Properties from number type
protected Result
validateBooleanType(mixed $value, array $schema)
Validate a boolean value with the given schema
protected Result
validateArrayType(mixed $value, array $schema, array $types = [])
Validate an array value with the given schema
The following properties are handled in given $schema:
- minItems : minimal allowed item Number
- maxItems : maximal allowed item Number
- items : schema for all instances of the array
- uniqueItems : allow only unique values
protected Result
validateDictionaryType(mixed $value, array $schema, array $types = [])
Validate a dictionary value with the given schema
The following properties are handled in given $schema:
- properties : array of keys and schemas that have to validate
- formatProperties : dictionary of schemas, the schemas are used to validate all keys that match the string-format
- patternProperties : dictionary of schemas, the schemas are used to validate all keys that match the string-pattern
- additionalProperties : if false is given all additionalProperties are forbidden, if a schema is given all additional properties have to match the schema
protected Result
validateStringType(mixed $value, array $schema, array $types = [])
Validate a string value with the given schema
The following properties are handled in given $schema:
- pattern : Regular expression that matches the $value
- minLength : minimal allowed string length
- maxLength : maximal allowed string length
- format : some predefined formats [date-time|date|time|uri|email|ipv4|ipv6|ip-address|host-name|class-name|interface-name]
protected Result
validateNullType(mixed $value, array $schema)
Validate a null value with the given schema
protected Result
validateAnyType(mixed $value, array $schema)
Validate any value with the given schema. Return always a valid Result.
protected string
renderValue(mixed $value)
Create a string information for the given value
protected Error
createError(string $expectation, mixed $value = null)
Create Error Object
protected bool
isSchema(array $phpArray)
Determine whether the given php array is a schema or not
protected bool
isNumericallyIndexedArray(array $phpArray)
Determine whether the given php array is a plain numerically indexed array
protected bool
isDictionary(array $phpArray)
Determine whether the given php array is a Dictionary (has no numeric identifiers)