Skip to content

Jay-Schema is and object validation module that can be used stand alone or as a database schema with Jay-Mongo. Jay-Schema+Jay-Mongo were built as alternative to Mongoose.

Notifications You must be signed in to change notification settings

zombitos/jay-schema

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jay-Schema

Jay-Schema is and object validation module that can be used stand alone or as a data base schema with <a href='https://github.com/zombitos/jay-mongo', target='_blank'>Jay-Mongo.

Installation

npm install Jay-Schema

Basic Usage

var Jay-Schema = require('jay-schema');
var schemaOptions = {
  requiredErrorFormatter: function(key) {
    return key + ' is required';
  },
  typeErrorFormatter: function(key) {
    return key + ' is not of correct data type';
  }
};
var schema = {
  name: {
    required: true,
  },
  lastname: {
    required: true
  },
  createdAt: {
    type: Date,
    required: true,
    default: function() {
      return new Date();
    }
  },
  deletedAt: {
    type: Date,
    default: null
  }
};

Schema Options

Jay-Schema uses predefined functions to format errors.

requiredErrorFormatter:
Called when a required field is undefined, null, or an empty string.
Receives the name of the missing property.
Returns the error in the format you need.

var requiredErrorFormatter= function(key) {
  var err = {};
  err[key] = 'this field is required';
  return err;
};



typeErrorFormatter:
Called when a field is not of the correct data type.
Receives the name of the property, and the type it should be.
Returns the error in the format you need. ```javascript var typeErrorFormatter= function(key, type) { var err = {}; err[key] = 'should be of type '+type; return err; }; ``` ## Data types Jay-Schema supports validation for the following types:
type: Number (Accepts and transforms numbers send as strings,)
type: String (Accepts and transform numbers to string)
type: Date (Accepts strings, if the string is a valid date it will transform it)
type: Boolean (Accepts strings 'true' and 'false' and transforms them)
type: Array
type: Object
type: Function

This validation will only check if the field's value is of the correct type. If you need a custom validation you can use the validator option.

**Note: All data types accept null as a valid value

Validator

You can use the validator property as a function or as an enum array.
If the validation does not pass the function should return false, otherwise it should return the validated property value.

fixedSizedNumberStrong: {
  type: Number,
  validator: function(val){
    if(val < 0 && val > 9) return false;
    return val;
  }
}
fixedSizedNumberAdjustment: {
  type: Number,
  validator: function(val){
    if(val < 0 && val > 9) val = 0;
    return val;
  }
}

If type is specified in the field and the validator returns a diferent data type the validation will fail. You can however use validator without specifying any data type.

You can use other properties in a following validation.

aNumber: {
    type: Number,
    default:0
},
aPositiveNumber:{
  type: Number,
  validator: function(val){
    if((val - this.aNumber) < 0) return false;
    return val;
  }
}

Jay-Schema doesn't currently support async default or validator functions.

To use validator as enum just place the array of accepted values

category: {
    type: String,
    validator:['shoes','shirts','pants']
}

If the value is not in the array the validation will not pass

Default

You can use the default property as a constant value or as a function

aNumber: {
  type: Number,
  default: 100
}
randomNumber: {
  type: Number,
  default: function(){
    return Math.random() * (100 - 10) + 10;
  }
}

If type is specified in the field and the default returns a diferent data type the validation will fail. You can however use default without specifying any data type.

You can use other properties in a following default.

items: {
    type: Array,
    required:true
},
itemsLength:{
  type: Number,
  default: function(){
    return this.items.length;
  }
}

Jay-Schema doesn't currently support async default or validator functions.

Schema Creation

Schema options:

var schemaOptions = {
  requiredErrorFormatter: function(key) {
    return key + ' is required';
  },
  typeErrorFormatter: function(key) {
    return key + ' is not of correct data type';
  }
};

If not options are given default error formaters return the name of the property.

Schema fields support the following properties

required (Boolean, default false): if true the object to validate must contain this property. Exception with omitUndefined option, see below.

default: can be a fix value or a function that returns a value. See details above.

validator: can be and array of valued or a function. See details above.

type: the data type the object's property should be. See details above.

var schema = {
  name: {
    required: true,
  },
  lastname: {
    required: true
  },
  createdAt: {
    type: Date,
    required: true,
    default: function() {
      return new Date();
    }
  },
  deletedAt: {
    type: Date,
    default: null
  }
};

Creating new Schema:

var schema = new Jay-Schema(schema, schemaOptions);

Available methods

Jay-Schema is promise oriented all functions starting with p the capital letter will return a promise

+path Fetches a copy of the schema property Receives: Property key (string) Returns Object

+eachPath Iterates over each schema property, with the defined fuction Receives: Iterator (function): receives value and key Returns undefined

+requiredPaths Returns an array of keys of all the schema properties thar are required Receives: None Returns Array of property names

+pMakeStruct Makes struct based on the schema provided

Receives: Object to validate (if it has properties that are not defined on the schema they will be ignored and not included in the result)

Options Object: omitUndefined(Boolean, default false), if omit undefined is true all schema properties that are undefined in the object to validate will no subjected to validation and will be ignored in the result

Extention Object(default empty object), object to validate will be extendend with this BEFORE it's validated, so properties should also be defined in schema and they will be validated

Returns promise, see below

schema.pMakeStruct({
  name: 'Jose',
  lastname: 'Rodriguez'
}).then(function(struct){
    //Struct will be 
    //{
      //name: 'Jose',
      //lastname: 'Rodriguez',
      //createdAt: Date Object,
      //deletedAt: null
    //}
    console.log(struct);
  },function(err){

    //Array of all validation errors
    console.log(err);
    });

Tests

npm test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Release History

  • 1.0.1 Fixed number validation for null

About

Jay-Schema is and object validation module that can be used stand alone or as a database schema with Jay-Mongo. Jay-Schema+Jay-Mongo were built as alternative to Mongoose.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published