formats is a collection of validators.
$ npm install formats
First you need to add a reference to formats in your application.
const formats = require('formats');
Basically, to validate a value you need to call the appropriate function on the formats
object, e.g. string
. The result is a validator function that you can re-use multiple times.
const stringValidator = formats.string();
console.log(stringValidator('foobar')); // => true
Some validators are customizable. For that provide an options
object when requesting the validator.
const stringValidator = formats.string({ minLength: 7 });
console.log(stringValidator('foobar')); // => false
From time to time you may be interested in the actual value instead of a boolean (or a default value in case of an invalid value). In these cases, specify the default
option.
const stringValidator = formats.string({ minLength: 7, default: 'formats' });
console.log(stringValidator('foobarbaz')); // => 'foobarbaz'
console.log(stringValidator('foobar')); // => 'formats'
Although not explicitly described below, the default
option is available for every validator.
Validates that a value is of type string
that contains only alphanumeric characters.
minLength
: Validates that a value is at leastn
characters long.maxLength
: Validates that a value is at mostn
characters long.
const validator = formats.alphanumeric({
minLength: 5,
maxLength: 23
});
Validates that a value is of type boolean
.
const validator = formats.boolean();
Validates that a value is of type Date
.
min
: Validates that a value is later thann
.max
: Validates that a value is earlier thann
.
const validator = formats.date({
min: new Date(2015, 0, 1),
max: new Date(2015, 11, 31)
});
Validates that a value is an email address, according to the W3C HTML5 specification.
const validator = formats.email();
Validates that a value is of type function
.
const validator = formats.function();
Validates that a value is an ip address.
version
: Validates that a value is a version4
or version6
address.
const validator = formats.ip({
version: 4
});
Validates that a value is a MAC address, according to the IEEE 802 standard.
const validator = formats.mac();
Validates that a value is of type number
.
isInteger
: Validates that a value is an integer.min
: Validates that a value is at leastn
.max
: Validates that a value is at mostn
.
const validator = formats.number({
isInteger: true,
min: 5,
max: 23
});
Validates that a value is of type object
.
isOptional
: Validates that a value may benull
orundefined
.schema
: Validates that a value fulfills a schema.isSchemaRelaxed
: Validates that a value may contain additional properties that are not described by the schema.
const validator = formats.object({
isOptional: false,
schema: {
foo: formats.number(),
bar: formats.string()
},
isSchemaRelaxed: false
});
Validates that a value matches a regular expression.
expression
: Validates that the value matches a regular expression.
const validator = formats.regex({
expression: /^foo$/
});
Validates that a value is of type string
.
minLength
: Validates that a value is at leastn
characters long.maxLength
: Validates that a value is at mostn
characters long.
const validator = formats.string({
minLength: 5,
maxLength: 23
});
Validates that a value is a uuid.
const validator = formats.uuid();
If you want to validate a value, but there is no matching built-in validator, you may use a custom validator.
A custom validator is a function that returns a validator function that returns true
if the specified value is valid, and false
otherwise (or the value and the default value, respectively, if the default
option is given). Once you have defined the custom validator, you can use it by providing it to the custom
function.
Additionally, it is advised to protect against unknown properties to avoid typos when creating the validators. For this, use throwOnUnknownProperties
.
const getReturnValue = require('formats').getReturnValue,
throwOnUnknownProperties = require('formats').throwOnUnknownProperties;
const range = function (options) {
options = options || {};
options.min = options.min || Number.NEGATIVE_INFINITY;
options.max = options.max || Number.POSITIVE_INFINITY;
throwOnUnknownProperties(options, [ 'min', 'max', 'default' ]);
return function (value) {
const returnValue = getReturnValue(value, options);
if (typeof value !== 'number') {
return returnValue.false;
}
if (value < options.min) {
return returnValue.false;
}
if (value > options.max) {
return returnValue.false;
}
return returnValue.true;
};
};
const rangeValidator = formats.custom(range({ min: 5, max: 23 }));
console.log(rangeValidator(42)); // => false
If you directly want to validate a value and skip the creation of a validator function, use the appropriate is*
function, e.g. isString
. These functions take the value as first parameter, and options as second.
console.log(formats.isString('foobar', { minLength: 7 })); // => false
You may also use the default
option as described above with the is* validator functions.
console.log(formats.isString('foobarbaz', {
minLength: 7,
default: 'formats'
})); // => 'foobarbaz'
console.log(formats.isString('foobar', {
minLength: 7,
default: 'formats'
})); // => 'formats'
To build this module use roboter.
$ bot
The MIT License (MIT) Copyright (c) 2014-2018 the native web.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.