Package Exports
- valid8
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (valid8) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
valid8
valid8 is a Javascript data validator for use with user input from web forms, APIs, etc.
Requirements
Installation
npm install valid8Run Tests
mochaUsage
var Valid8 = require('valid8');
var valid8 = new Valid8('form', {
name: 'string',
username: {
type: 'string',
max: 20,
required: true
},
email: 'emailString',
age: {
type: 'int',
min: 18,
required: true
},
gpa: {
type: 'decimal',
},
gender: {
type: 'enum',
options: ['male', 'female']
}
});
var result = valid8.areFieldsValid({
name: 'Aiham',
username: 'averylonglonglongusername',
email: 'invalid email',
age: 17,
gpa: '1.23',
gender: 'foobar'
});
/* result contains:
{
username: 'long',
email: 'format',
age: 'small',
gender: 'option'
}
*/Constructor
new Valid8([testType], [fields])testType- eitherstrictorform(see below)fields- An object containing the field definitions
Instance Methods
isFieldValid(name, value)name- Name of the field to be validatedvalue- Value to be validated- Returns
trueif the field is valid - Returns an error response if invalid (see below)
areFieldsValid(values, [fieldWhiteList])values- An object containing the values for each field to be validatedfieldWhiteList- An array of field names to be validated. For when you only want to validate a subset of the fields- Returns
trueif all values are valid - Returns an object containing the name of each invalid field and the corresponding error response (see below)
Constants
Test Types
Valid8.testTypes.strict- Checks value and data type. (Default)Valid8.testTypes.form- Checks value even if data is provided in strings. Good for web forms
Field Types
Valid8.fieldTypes.stringValid8.fieldTypes.emailStringValid8.fieldTypes.dateStringValid8.fieldTypes.intValid8.fieldTypes.decimalValid8.fieldTypes.enum
Error Responses
Valid8.errors.missingValid8.errors.longValid8.errors.shortValid8.errors.largeValid8.errors.smallValid8.errors.formatValid8.errors.dataTypeValid8.errors.option