Skip to content
jojenki edited this page Mar 12, 2013 · 1 revision

Contents


Creates a new Concordia object.

Arguments

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.

conformsTo()

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.

Arguments

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.

Returns

Nothing is returned.

Throws

Throws an exception if this schema does not fully define the other schema.

validateData()

Validates that some data conforms to this schema.

Arguments

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

Returns the data as an object or an array.

Throws

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.

Adding a Type Decorator

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;

Example Type Decorator

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.";
    }
}

Adding a Data Decorator

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;

Example Data Decorator

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).