#Welcome to the Validator! This is a functional styled validation engine. It is an attempt to create a functional, easy to use, validation library that has a very small implementation footprint. It is the goal of this library to remove as much ceremony as possible and allow for rich validation operations.
Given the following customer object:
var cust = {
name: 'john',
age: 19,
shirtSize: 'large',
};
Next, using the following schema validation:
var schema = {
name: all([required(), minLen(4), maxLen(12)]),
age: all([min(21), max(55)]),
shirtSize: all([within(['small', 'medium', 'large'])])
};
Finally, you run the validation engine:
var val = validate(schema);
var result = val(cust);
console.log(result);
You would get the following in the result object:
{ name: {
name: 'name',
value: 'john',
result: [ ]
},
age: {
name: 'age',
value: 19,
result: [ 'Age must be greater than 21' ]
},
shirtSize: {
name: 'shirtSize',
value: 'large',
result: [ ]
}
}
As you can see, the engine tries to create intelligent validation messages when a failure happens. However, you can override this at any time. Here is an example of a schema that overrides the age error message:
var schema = {
name: all([required(), minLen(4), maxLen(12)]),
age: all([min(21, "Sorry, you are not the legal age!"), max(55)]),
shirtSize: all([within(['small', 'medium', 'large'])])
};
var val = validate(schema);
var result = val(cust);
console.log(result);
You now get the following error message:
{ name: {
name: 'name',
value: 'john',
result: [ ]
},
age: {
name: 'age',
value: 19,
result: [ 'Sorry, you are not the legal age!' ]
},
shirtSize: {
name: 'shirtSize',
value: 'large',
result: [ ]
}
}
##Documentation Please refer to the following documentation for more information:
The samples provided use BabelJS as the transpiler. These need to be installed in order for the samples to work. Please use the following steps to ensure you environment is ready:
-
Ensure that NodeJS is installed. This provides the platform on which the build tooling runs.
-
From the project folder, execute the following command:
npm install
To run the examples, you simply need to press Ctrl+B on Windows or Cmd+B on Mac. You will see the output of transpiling the code in the output window in Sublime.
NOTE: You will need to perform the Setup steps in order to configure Sublime Text to support this.
##Setup The following are the steps required to configure Sublime Text to automatically build and test your validations:
##Dependencies This library has a dependency on the following:
- RamdaJS - A practical functional library for Javascript programmers.
- BabelJS - Babel is a JavaScript compiler.
We use RamdaJS as our functional library to help us create our validation engine as well as our validation functions. We are using BabelJS to allow for transpiling our ES6 code back to ES5 with a special emphasis on fat arrow (=>) syntax.
##Limitations This library does not try to support nested-validations. In fact, it is recommended that you produce a schema for each level you wish to validate.