UNPKG

@cesium133/forgjs

Version:

forgJs is a javascript lightweight object validator.

47 lines (39 loc) 1.08 kB
/** * These are the basic validation functions that are common to multiple types */ const { isString, } = require('../util'); /** * This object combines all validation functions related to numbers */ const NUMBER = { min: (val, min) => val - min >= 0, max: (val, max) => val - max <= 0, equal: (val, equal) => val === equal, type: val => Number(val) === val, }; /** * This object combines all validation functions related to strings */ const STRING = { minLength: (val, min) => val.length - min >= 0, maxLength: (val, max) => val.length - max <= 0, equal: (val, equal) => val === equal, match: (val, regex) => regex.test(val), notEmpty: (val, itShouldNotBeEmpty) => (val.length !== 0) === itShouldNotBeEmpty, isEmpty: (val, itShouldBeEmpty) => (val.length === 0) === itShouldBeEmpty, type: isString, }; /** * This object combines all validation functions related to booleans */ const BOOLEAN = { type: val => val === true || val === false, toBe: (val, bool) => val === bool, }; module.exports = { BOOLEAN, STRING, NUMBER, };