Source code

CERNY.schema

The schema package provides functions for validating a JavaScript object against a schema. A schema is a JavaScript object describing a shape (form, schema, pattern). It is composed of constraints, which have a name and a value. These constraints are the members of the schema. The name of the constraint is mapped on an constraint value, which can be a primitive value, a function, a regular expression or a schema.

The function validate is used to perform validation of an object against a schema. It returns an object (called validation report), which contains an error message for every constraint of the schema, that the object does not conform to. Thus the report builds up the *same structure* as the the schema. If the object conforms to the schema, validate returns an empty object. For convenience the function isValid takes a validation report and returns true for an empty result and false, if the report contains errors.

If the constraint value is a function this can be used within the function to navigate to properties within the object. With this._parent you can navigate up the object tree. Thus it is not recommended to perform validation on objects that do contain a _parent property. When specifying a schema, you should not use the _parent property either.

If the constraint value is a function the following rules apply. The function must return true, if the value of the object fulfills the constraint, otherwise the function should return a String containing an error message or false, which will produce a generic error message. Exceptions that occur within the function are considered an error message also.

Functions

validate

Validate an object against a schema.

The schema is a JavaScript object, which is seen as map of properties to either values, functions, regular expressions or schemas. The values of the properties in a schema are called constraints. The object is said to confirm to schema, if all properties of schema exist in object and all values of object match the corresponding constraint.

object : the object to validate
schema : the schema to validate the object against
return : a report of the validation results, if the report is empty the object confirms to the schema, otherwise it contains the validation errors
isValid

Return true, if a validation result is empty (containing no validation errors). False, otherwise.

validationResult : a return value of CERNY.schema.validate
return : True, if no validation errors are in result and false, otherwise.
optional

Defines the term optional for use in schemas.

constraint : the constraint that is optional, can be any type of constraint
return : a function that applies the constraint only if the argument is not undefined or null
arrayOf

Defines the term arrayOf for use in schemas. The type can be a function or a schema.

type : the schema or function the array should consist of
min : the minimum number of items the array should consist of, if omitted no limit is assumed
max : the maximum number of items the array should consist of, if omitted no limit is assumed
isoDate

Defines the term ISO date for use in Schemas.

str : the string value to check if it is an ISO date
return : true, if it str is an ISO date string, an error message otherwise

History

2007-02-12 : Renamed aspect to constraint.
2007-01-01 : Apdated to interceptor concept.
2006-12-13 : Generalized optional to work with any type of constraint
2006-12-13 : Refactored validate to allow easier access to
2006-12-08 : Modified arrayOf to accept boundaries.
2006-07-25 : Created.

API Documentation for version

1.1

Scripts

Scripts

schema

1.1

Functions