Data validation
April 17, 2019 ยท View on GitHub
Attributes can be validated by using the attribute.validate
configuration property.
attribute.validate is a standard JSON schema,
version 7.
The following keywords are available:
- for attributes of type
integerornumber:multipleOf{float}maximum{float}:<=exclusiveMaximum{float}:<minimum{float}:>=exclusiveMinimum{float}:>
- for attributes of type
string:maxLength{integer}:<=minLength{integer}:>=pattern{regex}format{string}: amongregex,date-time,date,time,email,hostname,ipv4,ipv6,uri,uri-reference,uri-template,json-pointer,relative-json-pointer
- for attributes of array type:
maxItems{integer}:<=minItems{integer}:>=uniqueItems{boolean}: no duplicatecontains{json_schema}: at least one item is validitems{json_schema}: all items are validadditionalItems{json_schema}
- for any attribute:
const{any}: must equal that valueenum{any[]}: must equal one of those valuesrequired{boolean}: checked onupsertandcreatecommands.dependencies{string|string[]}: attributes that are required for the current attribute to be defined.
- used as combinators:
not{json_schema}allOf{json_schema[]}anyOf{json_schema[]}oneOf{json_schema[]}if{json_schema},then{json_schema},else{json_schema}
The following properties have a slightly different syntax than in
JSON schema: required, dependencies.
The following properties are not available or are available under a different
configuration property: type,
description, examples, default, title, $id, $schema, $comment,
readOnly, contentMediaType, contentEncoding, definitions.
Since attributes cannot be objects, the following properties are also not
available: maxProperties, minProperties, additionalProperties,
properties, patternProperties, propertyNames
Example
collections:
example_collection:
attributes:
example_attribute:
validate:
required: true
minimum: 10
multipleOf: 2
Custom validation
If the JSON schema validation keywords are not sufficient, one can define custom
ones using the validation
configuration property.
This property is an object of validation keywords, where the key is the keyword name and the value an object with the properties:
test{function}: function that returnsfalseif the validation failed. The parameterargrepresents the value passed to the keyword, andvaluerepresents the value to validate.message{string|function}: error message. Can be functions with the parameterarg. Must start with'must 'type{string[]}: optionally restrict the attributes types that can use that keyword
validation:
$is_not_equal:
test: (value !== arg)
message: (`must not be equal to ${arg}`)
type: number