UNPKG

aright-lexicon

Version:

babelute DSL for Object and Types validations

68 lines (59 loc) 1.21 kB
/** * @author Gilles Coomans * @licence MIT * @copyright 2016-2017 Gilles Coomans */ import babelute from 'babelute'; const arightLexicon = babelute.createLexicon('aright'); arightLexicon .addAtoms([ 'is', 'has', 'strict', 'not', 'switch', 'minLength', 'maxLength', 'minimum', 'maximum', 'format', 'enum', 'item', 'equal', 'instanceOf', 'array', 'isArray', 'null' ]) .addCompounds(() => { return { between(min, max) { return this.minimum(min).maximum(max); }, isNull() { return this.equal(null); }, or(...rules) { return this._append('aright', 'or', rules); }, email() { return this.isString().format('email').minLength(6); } }; }) .addCompounds(() => { const methods = {}; ['object', 'string', 'function', 'boolean', 'number', 'boolean'] .forEach((type) => { const upperName = type[0].toUpperCase() + type.substring(1); methods[`is${ upperName }`] = function() { return this.is(type); }; methods[type] = function(name, babelute) { return this.has(name, type, babelute); }; }); return methods; }); export default arightLexicon; //______________________________________________________