-
Notifications
You must be signed in to change notification settings - Fork 2
JavaScript
Creates a new Concordia object.
Name | Type | Description |
---|---|---|
schema | An object or an array. Or a string representing one of the two. | The schema to validate and use internally for this instance. |
Checks that this schema defines everything within another schema. If the other schema defines something as "optional", it is acceptable if it does not exist in this schema.
Name | Type | Description |
---|---|---|
schema | A Concordia object, or a string representing that will be converted into one. | The schema to which this object's internal schema must conform. |
Nothing is returned.
Throws an exception if this schema does not fully define the other schema.
Validates that some data conforms to this schema.
Name | Type | Description |
---|---|---|
data | An object or an array. Or a string representing one of the two. | The data that must conform to this object's schema. |
Returns the data as an object or an array.
Throws an exception if the data does not conform to this schema.
Adding custom type and data validators is known as "decorating" Concordia. This can be accomplished by adding functions to Concordia's prototype.
Type decorators add additional type-checking when validating Concordia schemas. To add a type decorator to Concordia, add a field of the following form to Concordia's prototype:
validateSchemaDecorator{TYPE}
For example, to add a decorator to number types:
Concordia.prototype.validateSchemaDecoratorNumber = customNumberTypeDecorator;
To remove the decorator:
delete Concordia.prototype.validateSchemaDecoratorNumber;
The following sub-schema defines a number and has two custom fields, "min" and "max".
{
"type":"octalDigit",
"doc":"A single octal digit.",
"min":0,
"max":7
}
The following code will be added to Concordia's number-type validator and will be given sub-schemas that define numbers. It will validate that the "min" field is a number, if it exists. Also, it will guarantee that the "max" field exists and that it is also a number.
function customNumberTypeDecorator(obj) {
// Get the "min" field.
var min = obj['min'];
// Guarantee that the "min" field exists.
if ((min === null) ||
(Object.prototype.toString.call(min) !== "undefined")) {
throw "The 'min' value is missing.";
}
// Guarantee that the "min" field is a number.
else if (Object.prototype.toString.call(min) !== "[object Number]") {
throw "The 'min' value is not a number.";
}
// Get the "max" field.
var max = obj['max'];
// Guarantee that the "max" field exists.
if ((max === null) ||
(Object.prototype.toString.call(max) === "undefined")) {
throw "The 'max' value is missing.";
}
// Guarantee that the "max" field exists.
else if (Object.prototype.toString.call(max) !== "[object Number]") {
throw "The 'max' value is not a number.";
}
}
Data decorators add additional data-checking when validating data that is supposed to conform to this object's schema. To add a data decorator to Concordia, add a field of the following form to Concordia's prototype:
validateDataDecorator{TYPE}
For example, to add a decorator to number types:
Concordia.prototype.validateDataDecoratorNumber = customNumberDataDecorator;
To remove the decorator:
delete Concordia.prototype.validateDataDecoratorNumber;
Using the sub-schema and custom type validator from above, we have a custom data validator that looks like:
function customNumberDataDecorator(schema, data) {
// Because the schema is stored within the object it should never be
// modified, so it can safely be assumed that the 'min' and 'max' fields
// that were validated during schema validation will still be present.
var min = schema['min'];
var max = schema['max'];
// Because the type of the data has already been validated, it can
// safely be assumed that the data is at least of the valid type.
// NOTE: If the field is marked as 'optional', this value may be 'null'
// or 'undefined', which must be checked.
var value = data['octalDigit'];
// Now, the business logic of this validation ensures that the value is
// valid.
if (value < min) {
throw "The data is invalid because its value is less than the " +
'min' (" +
min +
"): " +
data;
}
if (value > max) {
throw "The data is invalid because its value is greater than the " +
'max' (" +
max +
"): " +
data;
}
}
And, we have data that looks like:
{
"octalDigit":9
}
When this component of the data is validated, the customNumberDataDecorator
will be fired, and it will throw an error because the data is outside of the given range (0-8).