new EnumNode(){EnumNode}
The EnumNode class represents a value that can be anything
Returns:
EnumNode instance.Methods
-
addEnums(enums){EnumNode}
-
Add the array of validator nodes the value must match, can be a string|number|boolean|array|null
Name Type Description enums
array array of validator nodes the value must match, can be a string|number|boolean|validation node|array
Example
// Create an Enum validation that validates the value assert = require('assert'), Compiler = require('vitesse').Compiler, EnumNode = require('vitesse').EnumNode; var enums = [null, 'dog', true, [1, 2, 3]]; var schema = new EnumNode(null, null, {typeCheck:true}) .addEnums(enums); var compiler = new Compiler({}); // Compile the AST var func = compiler.compile(schema, {}); // Validate {} var results = func.validate({}); assert.equal(1, results.length); assert.equal('field does not match enumeration [null,"dog",true,[1,2,3]]', results[0].message); assert.deepEqual(['object'], results[0].path); assert.ok(results[0].rule === schema); // Validate [1, 2, 3] var results = func.validate([1, 2, 3]); assert.equal(0, results.length);
-
generate(context)
-
Generate the code for this node
Name Type Description context
object the generation context for this node
-
path(){array}
-
Return the current object path
Returns:
array containing the path to this node
-
setTypeCheck(typeCheck){BooleanNode}
-
Type check value
Name Type Description typeCheck
boolean type check value