El Validator
![License](https://img.shields.io/npm/l/elvalidator)
El Validator is a JSON Object validator and sanitizer. It's a great little tool for validating user input and sanitizing.
It's heavily inspired from the familiar Mongoose schema format with a few more features. (Note: El Validator doesn't attempt to strictly match the Mongoose validator)
- 0 dependencies
- Easy to use
- Lightweight (8.51Kb minified)
Installation
$ npm install elvalidator --save
Or in the browser:
<script type="text/javascript" src="https://unpkg.com/elvalidator/elvalidator.min.js"></script>
Usage
import ElValidator from 'elvalidator';
let validator = new ElValidator({
name : { type: String, required:true, trim:true, minlength:3 },
age : { type: Number, required:true, integer:true, min:18, max:100 },
agreedTelemetry : { type: Boolean, default:false },
tags : [
{ type: String, minlength:3, lowercase:true, trim:true, match: /^[a-z0-9]+$/ }
],
settings : {
darkMode : { type: Boolean, default:false },
codeEditor : { type: String, required:false, default:'atom', enum:['atom', 'vstudio', 'notepad++'] },
}
}, {
strictMode : true,
throwUnkownFields : false,
accumulateErrors : false,
});
var sanitizedData = await validator.validate({
name : 'Yassine',
age : 27,
tags : ['PROgrammer', 'javascript '],
settings : {
darkMode : false,
},
other : 'unknown field',
});
console.log(sanitizedData);
Schema Reference
String
{
field : {
type : String,
name : "Field Name",
required : false,
default : '',
lowercase : false,
uppercase : false,
trim : false,
minLength : 3,
maxLength : 15,
enum : ['hello', 'world'],
match : /^(hello|world)$/i,
validator : async (value) => {
if(!value)
throw new Error('Invalid value');
return value;
},
}
}
Number
{
field : {
type : Number,
name : "Field Name",
required : false,
default : 0,
integer : false,
min : 3,
max : 15,
validator : async (value) => {
if(!value)
throw new Error('Invalid value');
return value;
},
}
}
Boolean
{
field : {
type : Boolean,
name : "Field Name",
required : false,
default : false,
validator : async (value) => {
if(!value)
throw new Error('Invalid value');
return value;
},
}
}
Array
{
field : [
{ type: String }
],
field : {
type : [
{ type: String }
],
name : "Field Name",
required : false,
default : [],
minEntries : 0,
maxEntries : 10,
uniqueValues: false,
validator : async (value) => {
if(!value)
throw new Error('Invalid value');
return value;
},
}
}
Object
This type allows you to accept Objects with arbitrary fields and no particular validation.
{
field : {
type : Object,
name : "Field Name",
required : false,
default : {},
validator : async (value) => {
if(!value)
throw new Error('Invalid value');
return value;
},
}
}
Sub-Schema
{
field : {
subField1 : { type: String },
subField2 : { type: Number },
subField3 : {
type : { type: String },
subSubField2 : { type: Number },
},
}
}
$or operator
{
field : {
$or : [
{ type: String, },
{ type: Number, min:0, max:10},
{ type: Number, min:100, max:1000, integer: true},
],
default : '',
required : true,
}
}
Built Ins
A few validators are available to make your life easier.
{
url : ElValidator.Builtins.Url,
domainName : ElValidator.Builtins.DomainName,
email : ElValidator.Builtins.Email,
youtubeVideo : ElValidator.Builtins.YoutubeVideo,
vimeoVideo : ElValidator.Builtins.VimeoVideo,
}
{
url : 'https://www.example.com/index.html?query=param',
domainName : 'www.example.com',
email : 'user@example.com',
youtubeVideo : 'https://www.youtube.com/watch?v=UNG7vNchvVQ',
vimeoVideo : 'https://vimeo.com/347119375',
}