1.0.7 • Published 9 years ago

ia-schema v1.0.7

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

IA-Schema

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

Installation

npm install ia-schema

Basic Usage

var IASchema = require('ia-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

IA-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;
};
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;
  }
}

IA 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;
  }
}

IA 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 IASchema(schema, schemaOptions);

Available methods

IA 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

  • 0.0.1 Initial release
  • 0.0.4 Fixed error delivery to string
  • 0.0.5 Optimized required detection, allow use of object data in default and validator functions, improved Date type validation
  • 1.0.0 If Default and validator are functions they can use "this" to reference object Validator can be an array of accepted values (enum) pMakeStruct parameters are now (data, options,extention) options omitUndefined(boolean) for pMakeStruct added
  • 1.0.1 Readme Fix
  • 1.0.4 This scope in default and validator fix
  • 1.0.7 Node version fixed
1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.1

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago