New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

elvalidator

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elvalidator

Validate and Sanitize JSON objects.

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-90.24%
Maintainers
1
Weekly downloads
 
Created
Source

ElValidator

Version NPM Downloads License

ElValidator 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: ElValidator doesn't attempt to strictly match the Mongoose validator)

  • 0 dependencies
  • Easy to use
  • Lightweight (8.7Kb 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';

// Set up the validator object
//let validator = new ElValidator(schema, options);
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 },

	// Array example:
	tags				: [
		{ type: String, minlength:3, lowercase:true, trim:true, match: /^[a-z0-9]+$/  }
	],

	// Object example:
	settings	: {
		darkMode	: { type: Boolean, default:false },
		codeEditor	: { type: String, required:false, default:'atom', enum:['atom', 'vstudio', 'notepad++'] },
	}
}, {
	strictMode			: true, // When disabled, the validator will go easy on the errors. (i.e. a number is provided instead of a string the number will be converted to a string instead of throwing an error)
	throwUnkownFields	: false,// Throw error when an unknown field is present
	accumulateErrors	: false,// Validate everything and then show big error message (vs. throw error as soon as an issue is detected)
});

// Validate/Sanitize a data object
// If there's an error, it will be thrown. You can thus use a try ... catch block to process the errors.
var sanitizedData = await validator.validate({
	name				: 'Yassine',
	age					: 27,

	tags				: ['PROgrammer', 'javascript '],
	settings			: {
		darkMode	: false,
	},
	other				: 'unknown field',
});

console.log(sanitizedData);

/*
Outputs:
{
  name: 'Yassine',
  age: 27,
  agreedTelemetry: false,
  tags: [ 'programmer', 'javascript' ],
  settings: { darkMode: false, codeEditor: 'atom' }
}

*/

Schema Reference

String

{
	field	: {
		type		: String,
		name		: "Field Name", // For cleaner error messages
		required	: false, // Is this a mandatory field?
		default		: '', // If provided, the required field is not considered

		lowercase	: false, // Force string to lower case
		uppercase	: false, // Force string to UPPER CASE
		trim		: false, // Trim text (remove whitespace at the start and end of the string)
		minlength	: 3, // Minimum length of the string
		maxlength	: 15, // Maximum length of the string
		enum		: ['hello', 'world'], // Array with valid values for this field
		match		: /^(hello|world)$/i, // String must match with this regex

		validator	: async (value) => { // Manually validate/sanitize the value
			if(!value)
				throw new Error('Invalid value');
			return value;
		},
	}
}

Number

{
	field	: {
		type		: Number,
		name		: "Field Name", // For cleaner error messages
		required	: false, // Is this a mandatory field?
		default		: 0, // If provided, the required field is not considered

		integer		: false, // Force number to be an integer
		min			: 3, // Minimum value of the number
		max			: 15, // Maximum value of the number

		validator	: async (value) => { // Manually validate/sanitize the value
			if(!value)
				throw new Error('Invalid value');
			return value;
		},
	}
}

Boolean

{
	field	: {
		type		: Boolean,
		name		: "Field Name", // For cleaner error messages
		required	: false, // Is this a mandatory field?
		default		: false, // If provided, the required field is not considered

		validator	: async (value) => { // Manually validate/sanitize the value
			if(!value)
				throw new Error('Invalid value');
			return value;
		},
	}
}

Array

{
	// With default options
	field	: [
		{ type: String } // Define the schema for the values this array should allow, in this case it accepts strings
	],

	// OR with options
	field	: {
		type		: [
			{ type: String } // Define the schema for the values this array should allow, in this case it accepts strings
		],
		name		: "Field Name", // For cleaner error messages
		required	: false, // Is this a mandatory field?
		default		: [], // If provided, the required field is not considered

		minEntries	: 0, // Minimum number of entries this array can hold
		maxEntries	: 10, // Maximum number of entries this array can hold
		uniqueValues: false, // Whether to filter out the values of the array to only return non-repeating values

		validator	: async (value) => { // Manually validate/sanitize the 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", // For cleaner error messages
		required	: false, // Is this a mandatory field?
		default		: {}, // If provided, the required field is not considered

		validator	: async (value) => { // Manually validate/sanitize the value
			if(!value)
				throw new Error('Invalid value');
			return value;
		},
	}
}

Sub-Schema

{
	field	: {
		subField1	: { type: String },
		subField2	: { type: Number },
		subField3	: {
			type			: { type: String }, // Here type is a sub-sub-field and accepts strings
			subSubField2	: { type: Number },
		},
	}
}

$or operator

{
	field	: {
		$or	: [ // You can define different possible types for this particular field using the $or operator

			{ type: String, }, // field can either be a string,
			{ type: Number, min:0, max:10}, // a number between 0 and 10
			{ type: Number, min:100, max:1000, integer: true}, // or an integer between 100 and 1000
		],
		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,
}

// will validate for:

{
	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',
}

Keywords

FAQs

Package last updated on 06 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc