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(mixed $value, mixed $schema, array $types = [])

Validate array with the given schema

validateType(mixed $value, array $schema, array $types = [])

Validate a value for a given type

validateTypeArray(mixed $value, array $schema, array $types = [])

Validate a value with a given list of allowed types

validateNumberType(mixed $value, array $schema)

Validate an integer value with the given schema

validateIntegerType(mixed $value, array $schema)

Validate an integer value with the given schema

validateBooleanType(mixed $value, array $schema)

Validate a boolean value with the given schema

validateArrayType(mixed $value, array $schema, array $types = [])

Validate an array value with the given schema

validateDictionaryType(mixed $value, array $schema, array $types = [])

Validate a dictionary value with the given schema

validateStringType(mixed $value, array $schema, array $types = [])

Validate a string value with the given schema

validateNullType(mixed $value, array $schema)

Validate a null value with the given schema

validateAnyType(mixed $value, array $schema)

Validate any value with the given schema. Return always a valid Result.

string
renderValue(mixed $value)

Create a string information for the given value

createError(string $expectation, mixed $value = null)

Create Error Object

bool
isSchema(array $phpArray)

Determine whether the given php array is a schema or not

bool
isNumericallyIndexedArray(array $phpArray)

Determine whether the given php array is a plain numerically indexed array

bool
isDictionary(array $phpArray)

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

Parameters

mixed $value

value to validate

mixed $schema

type as string, schema or array of schemas

array $types

the additional type schemas

Return Value

Result

protected Result validateType(mixed $value, array $schema, array $types = [])

Validate a value for a given type

Parameters

mixed $value
array $schema
array $types

Return Value

Result

protected Result validateTypeArray(mixed $value, array $schema, array $types = [])

Validate a value with a given list of allowed types

Parameters

mixed $value
array $schema
array $types

Return Value

Result

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

Parameters

mixed $value
array $schema

Return Value

Result

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

Parameters

mixed $value
array $schema

Return Value

Result

See also

SchemaValidator::validateNumberType

protected Result validateBooleanType(mixed $value, array $schema)

Validate a boolean value with the given schema

Parameters

mixed $value
array $schema

Return Value

Result

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

Parameters

mixed $value
array $schema
array $types

Return Value

Result

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

Parameters

mixed $value
array $schema
array $types

Return Value

Result

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]

Parameters

mixed $value
array $schema
array $types

Return Value

Result

protected Result validateNullType(mixed $value, array $schema)

Validate a null value with the given schema

Parameters

mixed $value
array $schema

Return Value

Result

protected Result validateAnyType(mixed $value, array $schema)

Validate any value with the given schema. Return always a valid Result.

Parameters

mixed $value
array $schema

Return Value

Result

protected string renderValue(mixed $value)

Create a string information for the given value

Parameters

mixed $value

Return Value

string

protected Error createError(string $expectation, mixed $value = null)

Create Error Object

Parameters

string $expectation
mixed $value

Return Value

Error

protected bool isSchema(array $phpArray)

Determine whether the given php array is a schema or not

there should be a more sophisticated way to detect schemas

Parameters

array $phpArray

Return Value

bool

protected bool isNumericallyIndexedArray(array $phpArray)

Determine whether the given php array is a plain numerically indexed array

Parameters

array $phpArray

Return Value

bool

protected bool isDictionary(array $phpArray)

Determine whether the given php array is a Dictionary (has no numeric identifiers)

Parameters

array $phpArray

Return Value

bool