aright-lexicon
(babelute) DSL for describing validation rules with expressive compact chained API.
Think about it as a Super JSON-Schema DSL.
- Easy errors i18n.
- Easy dialecting.
- Really fast because it doesn't need schema parsing and interpretation.
- Could be used to define active schema (which perform transformation on validation).
- Could be translated/transformed to many output kinds (html, assertions, function signature with meta-programming, ...)
Under heavy work. Doc are not up-to-date. Only dev branch for the moment.
Usage
> npm i babelute aright-lexicon
Examples
const v = arightv; const rule = v object'child' v ; rule;// => return true
v; // return truev; // return error report
Full API
is* Family
.isString(), isNumber(), .isBoolean(), .isObject(), .isArray(), .isFunction(), .isNull()
v; //return true
.type('string', 'object', ...)
Sugar for .or(v.isString(), v.isObject(), ...)
.instanceOf(Class)
const rule = v result = rule; // return true
properties validation
- .boolean(propName, rule)
- .number(propName, rule)
- .string(propName, rule)
- .function(propName, rule)
- .null(propName, rule)
- .object(propName, rule)
- .array(propName, rule)
v; //return true
value constraints
.minLength(5), .maxLength(3), .minimum(7), .maximum(9), .enum(['foo', 12]), .equal('my value')
Any value is required by default. Only undefined will be seen as missing.
v; // return true v; // return error report //...
value format
Validate value against regExp
v; // return true
To define custom format :
arightformatsmyFormat = /abc/gi;v; // return true
As predefined format there is only email for the moment...
v; // return true
array and items
Both work together :
const o = collection:'foo' 'bar' 'zoo'; v; // return true
.not and .or
const rule = v result = rule && rule; // return true
const rule = v result = rule && rule; // return true
validation
Any value could be validated by calling .$validate( valueToTest ) on any aright rule.
It returns true if rule is satisfied or an error report as follow if something fails :
const rule = v object'child' v ; const result = rule; /* result == { "valid": false, "map": { "email": { "value": "aaa@bbb", "errors": [ "format failed" ] }, "index": { "value": 1, "errors": [ "equality failed (should be : 24)" ] }, "flag": { "value": "hello", "errors": [ "should be a boolean" ] }, "collection.0": { "value": 1, "errors": [ "should be a string" ] }, "child.title": { "value": null, "errors": [ "missing property" ] }, "test": { "value": 3, "errors": [ "should be a boolean" ] } }, "value": { "email": "aaa@bbb", "index": 1, "flag": "hello", "collection": [ 1 ], "child": null, "test": 3 }}*/
custom format (RegExp)
.onValidation(handler)
Cutom validation handling.
const rule = v;
creating dialects
// handler that act on choosen propertyarightValidatorprototype{ return this;}; v;
i18n
Take a look to aright/i18n/fr.js
to have an idea on how customise
arighti18ndatafr = ;arighti18ncurrentLanguage = 'fr';// aright errors messages will now be in french
Licence
The MIT License
Copyright 2016-2017 (c) Gilles Coomans
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.