Schema builder and validator


stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
Minified + gzip package size for @az-tech/schemas in KB


##Schemas schema.js provides a simple schema constructor which provides validation methods of models.

###Usage Create a schema file for your model, ie: 'myModel.schema.js', and require the 'schema.js' file in it. Create an object representing the model and call new Schema(myModel) to create the new model schema.

###Schema API myschema.validate(object) = Will validate the data being passed in against the model schema definition. This method will run the following commands.

mySchema.validateFieldType(field, modelField) = Validates that the property is of the proper data type. field is a string representing the property name of the schema. modelField is the property on the data to be validated.

mySchema.validateDateString(val) = Validates that date strings are proper ISO format.

myschema.validateLength(field, modelField) = Validates that the length of the field is neither over the maxLength or under the minLength of that property. fieldis the name of the prop to validate, modelField is the property value to be validated.

###Model Definitions Define model schemas as an object of key: value pairs where key is the name of the property and value is an object with the property defitions. Current property values are as follows:

type: the data type of the property. Currently supported types are 'string','number','boolean','arrary', and 'date'. 'date' is current;ly assumed to be an ISO String.
required: A boolean value to specify whether a property is required to pass validation. Not providing a required key:pair will default to required:false
minLength: A number value to designate the minimum length of the property. Not providing a minLength will default the value to 0;
maxLength: A number value to designate the maximum length of the property. Not providing a minLength will default the value to null, allowing unlimited length;
default: Default value for property. If the data provided has a key: value pair it will use those values, otherwise it will use the provided default or null if no default is provided.

###Example: A model like:

const myModel = new Schema({
        name: {required: true, type: 'string'},
        age: {required: false, type: 'number', minLength: 2, maxLength: 3},
        createdAt: {required: true, type: 'date'},
        updatedAt: {required: true, type: 'date'},
        active: {required: true, type: 'boolean'}

will create a new schema with 5 properties.

Calling validate on an object will validate the object against the schema, returning either true, or an array of strings, with the strings detailing the issues with the validation.

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.