Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastest-validator

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastest-validator - npm Package Compare versions

Comparing version 0.6.12 to 0.6.13

11

CHANGELOG.md
--------------------------------------------------
<a name="0.6.13"></a>
# 0.6.13 (2019-01-22)
## Changes
- add error message for `url` rule.
- add `numeric` attribute to `string` rule.
- add `alpha`, `alphanum` & `alphadash` attributes to `string` rule.
- add `index.d.ts` file.
- fix multiple validator with different messages issue.
--------------------------------------------------
<a name="0.6.12"></a>

@@ -3,0 +14,0 @@ # 0.6.12 (2018-12-04)

74

dist/index.js

@@ -56,2 +56,6 @@ (function (global, factory) {

stringEnum: "The '{field}' field does not match any of the allowed values!",
stringNumeric: "The '{field}' field must be a numeric string",
stringAlpha: "The '{field}' field must be an alphabetic string",
stringAlphanum: "The '{field}' field must be an alphanumeric string",
stringAlphadash: "The '{field}' field must be an alphadash string",

@@ -87,2 +91,4 @@ number: "The '{field}' field must be a number!",

url: "The '{field}' field must be a valid URL!",
enumValue: "The '{field} field value '{expected}' does not match any of the allowed values!",

@@ -279,2 +285,7 @@ };

var NUMERIC_PATTERN = /^-?[0-9]\d*(\.\d+)?$/;
var ALPHA_PATTERN = /^[a-zA-Z]+$/;
var ALPHANUM_PATTERN = /^[a-zA-Z0-9]+$/;
var ALPHADASH_PATTERN = /^[a-zA-Z0-9_\-]+$/;
var string = function checkString(value, schema) {

@@ -285,9 +296,2 @@ if (typeof value !== "string") {

/* TODO: charset
alpha: /^[a-zA-Z]+$/
alphaNum: /^[a-zA-Z0-9]+$/
alphaDash: /^[a-zA-Z0-9_\-]+$/
*/
var valueLength = value.length;

@@ -311,4 +315,6 @@

if (schema.pattern != null && !schema.pattern.test(value)) {
return this.makeError("stringPattern", schema.pattern );
if (schema.pattern != null) {
var pattern = typeof schema.pattern == "string" ? new RegExp(schema.pattern, schema.patternFlags) : schema.pattern;
if (!pattern.test(value))
{ return this.makeError("stringPattern", pattern ); }
}

@@ -324,2 +330,18 @@

if (schema.numeric === true && !NUMERIC_PATTERN.test(value) ) {
return this.makeError("stringNumeric", "A numeric string", value);
}
if(schema.alpha === true && !ALPHA_PATTERN.test(value)) {
return this.makeError("stringAlpha", "An alphabetic string", value);
}
if(schema.alphanum === true && !ALPHANUM_PATTERN.test(value)) {
return this.makeError("stringAlphanum", "An alphanumeric string", value);
}
if(schema.alphadash === true && !ALPHADASH_PATTERN.test(value)) {
return this.makeError("stringAlphadash", "An alphadash string", value);
}
return true;

@@ -373,15 +395,15 @@ };

switch (character) {
case "\"":
case "'":
case "\\":
return "\\" + character;
case "\"":
case "'":
case "\\":
return "\\" + character;
// Four possible LineTerminator characters need to be escaped:
case "\n":
return "\\n";
case "\r":
return "\\r";
case "\u2028":
return "\\u2028";
case "\u2029":
return "\\u2029";
case "\n":
return "\\n";
case "\r":
return "\\r";
case "\u2028":
return "\\u2028";
case "\u2029":
return "\\u2029";
}

@@ -398,3 +420,3 @@ });

this.opts = {
messages: messages
messages: deepExtend_1({}, messages)
};

@@ -439,4 +461,4 @@

this.cache.clear();
return function(value) {
return self.checkSchemaType(value, rules, undefined, null);
return function(value, path, parent) {
return self.checkSchemaType(value, rules, path, parent || null);
};

@@ -447,4 +469,4 @@ }

this.cache.clear();
return function(value) {
return self.checkSchemaObject(value, rule, undefined, null);
return function(value, path, parent) {
return self.checkSchemaObject(value, rule, path, parent || null);
};

@@ -451,0 +473,0 @@ };

@@ -1,1 +0,1 @@

"use strict";function r(){function e(e){return"string"!=typeof e?this.makeError("string"):!!S.test(e)||this.makeError("url")}function t(e,t){if("string"!=typeof e)return this.makeError("string");var r=e.length;return!1===t.empty&&0===r?this.makeError("stringEmpty"):null!=t.min&&r<t.min?this.makeError("stringMin",t.min,r):null!=t.max&&r>t.max?this.makeError("stringMax",t.max,r):null!=t.length&&r!==t.length?this.makeError("stringLength",t.length,r):null==t.pattern||t.pattern.test(e)?null!=t.contains&&-1===e.indexOf(t.contains)?this.makeError("stringContains",t.contains):null==t.enum||-1!==t.enum.indexOf(e)||this.makeError("stringEnum",t.enum):this.makeError("stringPattern",t.pattern)}function r(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)||this.makeError("object")}function n(e,t){return!0===t.convert&&"number"!=typeof e&&(e=Number(e)),"number"!=typeof e||isNaN(e)||!isFinite(e)?this.makeError("number"):null!=t.min&&e<t.min?this.makeError("numberMin",t.min,e):null!=t.max&&e>t.max?this.makeError("numberMax",t.max,e):null!=t.equal&&e!==t.equal?this.makeError("numberEqual",t.equal,e):null!=t.notEqual&&e===t.notEqual?this.makeError("numberNotEqual",t.notEqual):!0===t.integer&&0!=e%1?this.makeError("numberInteger",e):!0===t.positive&&0>=e?this.makeError("numberPositive",e):!(!0===t.negative&&0<=e)||this.makeError("numberNegative",e)}function i(e){return"function"==typeof e||this.makeError("function")}function a(e){return null===e||void 0===e||this.makeError("forbidden")}function o(e,t){return null==t.values||-1!==t.values.indexOf(e)||this.makeError("enumValue",t.values,e)}function u(e,t){return"string"!=typeof e?this.makeError("string"):!!("precise"==t.mode?g:E).test(e)||this.makeError("email")}function l(e,t){return!0!==t.convert||e instanceof Date||(e=new Date(e)),e instanceof Date&&!isNaN(e.getTime())||this.makeError("date")}function s(e,t){return t.check.call(this,e,t)}function f(e,t){return!0===t.convert&&"boolean"!=typeof e&&(1===e||0===e||"true"===e||"false"===e||"1"===e||"0"===e||"on"===e||"off"===e)||("boolean"==typeof e||this.makeError("boolean"))}function c(e,t){if(!Array.isArray(e))return this.makeError("array");var r=e.length;if(!1===t.empty&&0===r)return this.makeError("arrayEmpty");if(null!=t.min&&r<t.min)return this.makeError("arrayMin",t.min,r);if(null!=t.max&&r>t.max)return this.makeError("arrayMax",t.max,r);if(null!=t.length&&r!==t.length)return this.makeError("arrayLength",t.length,r);if(null!=t.contains&&-1===e.indexOf(t.contains))return this.makeError("arrayContains",t.contains);if(null!=t.enum)for(r=0;r<e.length;r++)if(-1===t.enum.indexOf(e[r]))return this.makeError("arrayEnum",e[r],t.enum);return!0}function h(){return!0}function p(e,t){t=t||[];for(var r=0;r<e.length;++r)Array.isArray(e[r])?p(e[r],t):t.push(e[r]);return t}function m(e,t){for(var r in t)"object"==typeof t[r]&&null!==t[r]?(e[r]=e[r]||{},m(e[r],t[r])):e[r]=t[r];return e}function d(e){return e.replace(x,function(e){switch(e){case'"':case"'":case"\\":return"\\"+e;case"\n":return"\\n";case"\r":return"\\r";case"\u2028":return"\\u2028";case"\u2029":return"\\u2029"}})}function y(p){this.opts={messages:v},p&&b(this.opts,p),this.messages=this.opts.messages,this.rules={any:h,array:c,boolean:f,custom:s,date:l,email:u,enum:o,forbidden:a,function:i,number:n,object:r,string:t,url:e},this.cache=new Map}var b=m,v={required:"The '{field}' field is required!",string:"The '{field}' field must be a string!",stringEmpty:"The '{field}' field must not be empty!",stringMin:"The '{field}' field length must be greater than or equal to {expected} characters long!",stringMax:"The '{field}' field length must be less than or equal to {expected} characters long!",stringLength:"The '{field}' field length must be {expected} characters long!",stringPattern:"The '{field}' field fails to match the required pattern!",stringContains:"The '{field}' field must contain the '{expected}' text!",stringEnum:"The '{field}' field does not match any of the allowed values!",number:"The '{field}' field must be a number!",numberMin:"The '{field}' field must be greater than or equal to {expected}!",numberMax:"The '{field}' field must be less than or equal to {expected}!",numberEqual:"The '{field}' field must be equal with {expected}!",numberNotEqual:"The '{field}' field can't be equal with {expected}!",numberInteger:"The '{field}' field must be an integer!",numberPositive:"The '{field}' field must be a positive number!",numberNegative:"The '{field}' field must be a negative number!",array:"The '{field}' field must be an array!",arrayEmpty:"The '{field}' field must not be an empty array!",arrayMin:"The '{field}' field must contain at least {expected} items!",arrayMax:"The '{field}' field must contain less than or equal to {expected} items!",arrayLength:"The '{field}' field must contain {expected} items!",arrayContains:"The '{field}' field must contain the '{expected}' item!",arrayEnum:"The '{field} field value '{expected}' does not match any of the allowed values!",boolean:"The '{field}' field must be a boolean!",function:"The '{field}' field must be a function!",date:"The '{field}' field must be a Date!",dateMin:"The '{field}' field must be greater than or equal to {expected}!",dateMax:"The '{field}' field must be less than or equal to {expected}!",forbidden:"The '{field}' field is forbidden!",email:"The '{field}' field must be a valid e-mail!",enumValue:"The '{field} field value '{expected}' does not match any of the allowed values!"},g=/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,E=/^\S+@\S+\.\S+$/,S=/^https?:\/\/\S+/,k=/^[_$a-zA-Z][_$a-zA-Z0-9]*$/,x=/["'\\\n\r\u2028\u2029]/g;return y.prototype.validate=function(e,t){return this.compile(t)(e)},y.prototype.compile=function(e){var t=this;if(Array.isArray(e)){if(0==e.length)throw Error("If the schema is an Array, must contain at least one element!");var r=this.compileSchemaType(e);return this.cache.clear(),function(e){return t.checkSchemaType(e,r,void 0,null)}}var n=this.compileSchemaObject(e);return this.cache.clear(),function(e){return t.checkSchemaObject(e,n,void 0,null)}},y.prototype.compileSchemaObject=function(e){var t=this;if(null===e||"object"!=typeof e||Array.isArray(e))throw Error("Invalid schema!");var r=this.cache.get(e);if(r)return r.cycle=!0,r;r={cycle:!1,properties:null,compiledObjectFunction:null,objectStack:[]},this.cache.set(e,r),r.properties=Object.keys(e).map(function(r){return{name:r,compiledType:t.compileSchemaType(e[r])}});var n=[];n.push("let res;"),n.push("let propertyPath;"),n.push("const errors = [];");for(var i=0;i<r.properties.length;i++){var a=r.properties[i],o=d(a.name),u=k.test(o)?"value."+o:'value["'+o+'"]';n.push('propertyPath = (path !== undefined ? path + ".'+o+'" : "'+o+'");'),Array.isArray(a.compiledType)?n.push("res = this.checkSchemaType("+u+", properties["+i+"].compiledType, propertyPath, value);"):n.push("res = this.checkSchemaRule("+u+", properties["+i+"].compiledType, propertyPath, value);"),n.push("if (res !== true) {"),n.push("\tthis.handleResult(errors, propertyPath, res);"),n.push("}")}return n.push("return errors.length === 0 ? true : errors;"),r.compiledObjectFunction=new Function("value","properties","path","parent",n.join("\n")),r},y.prototype.compileSchemaType=function(e){var t=this;return Array.isArray(e)?(e=p(e.map(function(e){return t.compileSchemaType(e)})),1==e.length?e[0]:e):this.compileSchemaRule(e)},y.prototype.compileSchemaRule=function(e){"string"==typeof e&&(e={type:e});var t=this.rules[e.type];if(!t)throw Error("Invalid '"+e.type+"' type in validator schema!");var r=null,n=null;return"object"===e.type&&e.props?(r=this.compileSchemaObject(e.props),n=this.checkSchemaObject):"array"===e.type&&e.items&&(r=this.compileSchemaType(e.items),n=this.checkSchemaArray),{schemaRule:e,ruleFunction:t,dataFunction:n,dataParameter:r}},y.prototype.checkSchemaObject=function(e,t,r,n){return t.cycle?-1!==t.objectStack.indexOf(e)||(t.objectStack.push(e),e=this.checkSchemaObjectInner(e,t,r,n),t.objectStack.pop(),e):this.checkSchemaObjectInner(e,t,r,n)},y.prototype.checkSchemaObjectInner=function(e,t,r,n){return t.compiledObjectFunction.call(this,e,t.properties,r,n)},y.prototype.checkSchemaType=function(e,t,r,n){if(Array.isArray(t)){for(var i=[],a=t.length,o=0;o<a;o++){var u=this.checkSchemaRule(e,t[o],r,n);if(!0===u)return!0;this.handleResult(i,r,u)}return i}return this.checkSchemaRule(e,t,r,n)},y.prototype.checkSchemaArray=function(e,t,r,n){for(var i=[],a=e.length,o=0;o<a;o++){var u=(void 0!==r?r:"")+"["+o+"]",l=this.checkSchemaType(e[o],t,u,e,n);!0!==l&&this.handleResult(i,u,l)}return 0===i.length||i},y.prototype.checkSchemaRule=function(e,t,r,n){var i=t.schemaRule;return void 0===e||null===e?"forbidden"===i.type||!0===i.optional||(e=[],this.handleResult(e,r,this.makeError("required")),e):(i=t.ruleFunction.call(this,e,i,r,n),!0!==i?(e=[],this.handleResult(e,r,i),e):null===t.dataFunction||t.dataFunction.call(this,e,t.dataParameter,r,n))},y.prototype.handleResult=function(e,t,r){var n=this;(Array.isArray(r)?r:[r]).forEach(function(r){r.field||(r.field=t),r.message||(r.message=n.resolveMessage(r)),e.push(r)})},y.prototype.makeError=function(e,t,r){return{type:e,expected:t,actual:r}},y.prototype.resolveMessage=function(e){var t=this.messages[e.type];if(null!=t){var r=null!=e.expected?e.expected:"",n=null!=e.actual?e.actual:"";return t.replace(/\{field\}/g,e.field).replace(/\{expected\}/g,r).replace(/\{actual\}/g,n)}},y.prototype.add=function(e,t){this.rules[e]=t},y}var f=f||{};f.scope={},f.getGlobal=function(e){return"undefined"!=typeof window&&window===e?e:"undefined"!=typeof global&&null!=global?global:e},f.global=f.getGlobal(this),f.checkEs6ConformanceViaProxy=function(){try{var e={},t=Object.create(new f.global.Proxy(e,{get:function(r,n,i){return r==e&&"q"==n&&i==t}}));return!0===t.q}catch(e){return!1}},f.USE_PROXY_FOR_ES6_CONFORMANCE_CHECKS=!1,f.ES6_CONFORMANCE=f.USE_PROXY_FOR_ES6_CONFORMANCE_CHECKS&&f.checkEs6ConformanceViaProxy(),f.ASSUME_ES5=!1,f.ASSUME_NO_NATIVE_MAP=!1,f.ASSUME_NO_NATIVE_SET=!1,f.defineProperty=f.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(e,t,r){e!=Array.prototype&&e!=Object.prototype&&(e[t]=r.value)},f.SYMBOL_PREFIX="jscomp_symbol_",f.initSymbol=function(){f.initSymbol=function(){},f.global.Symbol||(f.global.Symbol=f.Symbol)},f.Symbol=function(){var e=0;return function(t){return f.SYMBOL_PREFIX+(t||"")+e++}}(),f.initSymbolIterator=function(){f.initSymbol();var e=f.global.Symbol.iterator;e||(e=f.global.Symbol.iterator=f.global.Symbol("iterator")),"function"!=typeof Array.prototype[e]&&f.defineProperty(Array.prototype,e,{configurable:!0,writable:!0,value:function(){return f.arrayIterator(this)}}),f.initSymbolIterator=function(){}},f.arrayIterator=function(e){var t=0;return f.iteratorPrototype(function(){return t<e.length?{done:!1,value:e[t++]}:{done:!0}})},f.iteratorPrototype=function(e){return f.initSymbolIterator(),e={next:e},e[f.global.Symbol.iterator]=function(){return this},e},f.makeIterator=function(e){f.initSymbolIterator();var t=e[Symbol.iterator];return t?t.call(e):f.arrayIterator(e)},f.owns=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},f.polyfill=function(e,t){if(t){var r=f.global;e=e.split(".");for(var n=0;n<e.length-1;n++){var i=e[n];i in r||(r[i]={}),r=r[i]}(t=t(n=r[e=e[e.length-1]]))!=n&&null!=t&&f.defineProperty(r,e,{configurable:!0,writable:!0,value:t})}},f.polyfill("WeakMap",function(e){function t(e){if(this.id_=(a+=Math.random()+1).toString(),e){f.initSymbol(),f.initSymbolIterator(),e=f.makeIterator(e);for(var t;!(t=e.next()).done;)t=t.value,this.set(t[0],t[1])}}function r(e){f.owns(e,i)||f.defineProperty(e,i,{value:{}})}function n(e){var t=Object[e];t&&(Object[e]=function(e){return r(e),t(e)})}if(f.USE_PROXY_FOR_ES6_CONFORMANCE_CHECKS){if(e&&f.ES6_CONFORMANCE)return e}else if(function(){if(!e||!Object.seal)return!1;try{var t=Object.seal({}),r=Object.seal({}),n=new e([[t,2],[r,3]]);return 2==n.get(t)&&3==n.get(r)&&(n.delete(t),n.set(r,4),!n.has(t)&&4==n.get(r))}catch(e){return!1}}())return e;var i="$jscomp_hidden_"+Math.random();n("freeze"),n("preventExtensions"),n("seal");var a=0;return t.prototype.set=function(e,t){if(r(e),!f.owns(e,i))throw Error("WeakMap key fail: "+e);return e[i][this.id_]=t,this},t.prototype.get=function(e){return f.owns(e,i)?e[i][this.id_]:void 0},t.prototype.has=function(e){return f.owns(e,i)&&f.owns(e[i],this.id_)},t.prototype.delete=function(e){return!(!f.owns(e,i)||!f.owns(e[i],this.id_))&&delete e[i][this.id_]},t},"es6","es3"),f.MapEntry=function(){},f.polyfill("Map",function(e){function t(){var e={};return e.previous=e.next=e.head=e}function r(e,t){var r=e.head_;return f.iteratorPrototype(function(){if(r){for(;r.head!=e.head_;)r=r.previous;for(;r.next!=r.head;)return r=r.next,{done:!1,value:t(r)};r=null}return{done:!0,value:void 0}})}function n(e,t){var r=t&&typeof t;"object"==r||"function"==r?a.has(t)?r=a.get(t):(r=""+ ++o,a.set(t,r)):r="p_"+t;var n=e.data_[r];if(n&&f.owns(e.data_,r))for(e=0;e<n.length;e++){var i=n[e];if(t!==t&&i.key!==i.key||t===i.key)return{id:r,list:n,index:e,entry:i}}return{id:r,list:n,index:-1,entry:void 0}}function i(e){if(this.data_={},this.head_=t(),this.size=0,e){e=f.makeIterator(e);for(var r;!(r=e.next()).done;)r=r.value,this.set(r[0],r[1])}}if(f.USE_PROXY_FOR_ES6_CONFORMANCE_CHECKS){if(e&&f.ES6_CONFORMANCE)return e}else if(function(){if(f.ASSUME_NO_NATIVE_MAP||!e||"function"!=typeof e||!e.prototype.entries||"function"!=typeof Object.seal)return!1;try{var t=Object.seal({x:4}),r=new e(f.makeIterator([[t,"s"]]));if("s"!=r.get(t)||1!=r.size||r.get({x:4})||r.set({x:4},"t")!=r||2!=r.size)return!1;var n=r.entries(),i=n.next();return!i.done&&i.value[0]==t&&"s"==i.value[1]&&!((i=n.next()).done||4!=i.value[0].x||"t"!=i.value[1]||!n.next().done)}catch(e){return!1}}())return e;f.initSymbol(),f.initSymbolIterator();var a=new WeakMap;i.prototype.set=function(e,t){var r=n(this,e);return r.list||(r.list=this.data_[r.id]=[]),r.entry?r.entry.value=t:(r.entry={next:this.head_,previous:this.head_.previous,head:this.head_,key:e,value:t},r.list.push(r.entry),this.head_.previous.next=r.entry,this.head_.previous=r.entry,this.size++),this},i.prototype.delete=function(e){return!(!(e=n(this,e)).entry||!e.list)&&(e.list.splice(e.index,1),e.list.length||delete this.data_[e.id],e.entry.previous.next=e.entry.next,e.entry.next.previous=e.entry.previous,e.entry.head=null,this.size--,!0)},i.prototype.clear=function(){this.data_={},this.head_=this.head_.previous=t(),this.size=0},i.prototype.has=function(e){return!!n(this,e).entry},i.prototype.get=function(e){return(e=n(this,e).entry)&&e.value},i.prototype.entries=function(){return r(this,function(e){return[e.key,e.value]})},i.prototype.keys=function(){return r(this,function(e){return e.key})},i.prototype.values=function(){return r(this,function(e){return e.value})},i.prototype.forEach=function(e,t){for(var r,n=this.entries();!(r=n.next()).done;)r=r.value,e.call(t,r[1],r[0],this)},i.prototype[Symbol.iterator]=i.prototype.entries;var o=0;return i},"es6","es3"),f.iteratorFromArray=function(e,t){f.initSymbolIterator(),e instanceof String&&(e+="");var r=0,n={next:function(){if(r<e.length){var i=r++;return{value:t(i,e[i]),done:!1}}return n.next=function(){return{done:!0,value:void 0}},n.next()}};return n[Symbol.iterator]=function(){return n},n},f.polyfill("Array.prototype.keys",function(e){return e||function(){return f.iteratorFromArray(this,function(e){return e})}},"es6","es3"),"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):this.FastestValidator=r();
"use strict";function r(){function e(e){return"string"!=typeof e?this.makeError("string"):!!A.test(e)||this.makeError("url")}function t(e,t){if("string"!=typeof e)return this.makeError("string");var r=e.length;return!1===t.empty&&0===r?this.makeError("stringEmpty"):null!=t.min&&r<t.min?this.makeError("stringMin",t.min,r):null!=t.max&&r>t.max?this.makeError("stringMax",t.max,r):null!=t.length&&r!==t.length?this.makeError("stringLength",t.length,r):null==t.pattern||(r="string"==typeof t.pattern?new RegExp(t.pattern,t.patternFlags):t.pattern).test(e)?null!=t.contains&&-1===e.indexOf(t.contains)?this.makeError("stringContains",t.contains):null!=t.enum&&-1===t.enum.indexOf(e)?this.makeError("stringEnum",t.enum):!0!==t.numeric||S.test(e)?!0!==t.alpha||k.test(e)?!0!==t.alphanum||x.test(e)?!(!0===t.alphadash&&!_.test(e))||this.makeError("stringAlphadash","An alphadash string",e):this.makeError("stringAlphanum","An alphanumeric string",e):this.makeError("stringAlpha","An alphabetic string",e):this.makeError("stringNumeric","A numeric string",e):this.makeError("stringPattern",r)}function r(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)||this.makeError("object")}function n(e,t){return!0===t.convert&&"number"!=typeof e&&(e=Number(e)),"number"!=typeof e||isNaN(e)||!isFinite(e)?this.makeError("number"):null!=t.min&&e<t.min?this.makeError("numberMin",t.min,e):null!=t.max&&e>t.max?this.makeError("numberMax",t.max,e):null!=t.equal&&e!==t.equal?this.makeError("numberEqual",t.equal,e):null!=t.notEqual&&e===t.notEqual?this.makeError("numberNotEqual",t.notEqual):!0===t.integer&&0!=e%1?this.makeError("numberInteger",e):!0===t.positive&&0>=e?this.makeError("numberPositive",e):!(!0===t.negative&&0<=e)||this.makeError("numberNegative",e)}function i(e){return"function"==typeof e||this.makeError("function")}function a(e){return null===e||void 0===e||this.makeError("forbidden")}function o(e,t){return null==t.values||-1!==t.values.indexOf(e)||this.makeError("enumValue",t.values,e)}function u(e,t){return"string"!=typeof e?this.makeError("string"):!!("precise"==t.mode?v:E).test(e)||this.makeError("email")}function l(e,t){return!0!==t.convert||e instanceof Date||(e=new Date(e)),e instanceof Date&&!isNaN(e.getTime())||this.makeError("date")}function s(e,t){return t.check.call(this,e,t)}function f(e,t){return!0===t.convert&&"boolean"!=typeof e&&(1===e||0===e||"true"===e||"false"===e||"1"===e||"0"===e||"on"===e||"off"===e)||("boolean"==typeof e||this.makeError("boolean"))}function c(e,t){if(!Array.isArray(e))return this.makeError("array");var r=e.length;if(!1===t.empty&&0===r)return this.makeError("arrayEmpty");if(null!=t.min&&r<t.min)return this.makeError("arrayMin",t.min,r);if(null!=t.max&&r>t.max)return this.makeError("arrayMax",t.max,r);if(null!=t.length&&r!==t.length)return this.makeError("arrayLength",t.length,r);if(null!=t.contains&&-1===e.indexOf(t.contains))return this.makeError("arrayContains",t.contains);if(null!=t.enum)for(r=0;r<e.length;r++)if(-1===t.enum.indexOf(e[r]))return this.makeError("arrayEnum",e[r],t.enum);return!0}function h(){return!0}function p(e,t){t=t||[];for(var r=0;r<e.length;++r)Array.isArray(e[r])?p(e[r],t):t.push(e[r]);return t}function m(e,t){for(var r in t)"object"==typeof t[r]&&null!==t[r]?(e[r]=e[r]||{},m(e[r],t[r])):e[r]=t[r];return e}function d(e){return e.replace(T,function(e){switch(e){case'"':case"'":case"\\":return"\\"+e;case"\n":return"\\n";case"\r":return"\\r";case"\u2028":return"\\u2028";case"\u2029":return"\\u2029"}})}function y(p){this.opts={messages:b({},g)},p&&b(this.opts,p),this.messages=this.opts.messages,this.rules={any:h,array:c,boolean:f,custom:s,date:l,email:u,enum:o,forbidden:a,function:i,number:n,object:r,string:t,url:e},this.cache=new Map}var b=m,g={required:"The '{field}' field is required!",string:"The '{field}' field must be a string!",stringEmpty:"The '{field}' field must not be empty!",stringMin:"The '{field}' field length must be greater than or equal to {expected} characters long!",stringMax:"The '{field}' field length must be less than or equal to {expected} characters long!",stringLength:"The '{field}' field length must be {expected} characters long!",stringPattern:"The '{field}' field fails to match the required pattern!",stringContains:"The '{field}' field must contain the '{expected}' text!",stringEnum:"The '{field}' field does not match any of the allowed values!",stringNumeric:"The '{field}' field must be a numeric string",stringAlpha:"The '{field}' field must be an alphabetic string",stringAlphanum:"The '{field}' field must be an alphanumeric string",stringAlphadash:"The '{field}' field must be an alphadash string",number:"The '{field}' field must be a number!",numberMin:"The '{field}' field must be greater than or equal to {expected}!",numberMax:"The '{field}' field must be less than or equal to {expected}!",numberEqual:"The '{field}' field must be equal with {expected}!",numberNotEqual:"The '{field}' field can't be equal with {expected}!",numberInteger:"The '{field}' field must be an integer!",numberPositive:"The '{field}' field must be a positive number!",numberNegative:"The '{field}' field must be a negative number!",array:"The '{field}' field must be an array!",arrayEmpty:"The '{field}' field must not be an empty array!",arrayMin:"The '{field}' field must contain at least {expected} items!",arrayMax:"The '{field}' field must contain less than or equal to {expected} items!",arrayLength:"The '{field}' field must contain {expected} items!",arrayContains:"The '{field}' field must contain the '{expected}' item!",arrayEnum:"The '{field} field value '{expected}' does not match any of the allowed values!",boolean:"The '{field}' field must be a boolean!",function:"The '{field}' field must be a function!",date:"The '{field}' field must be a Date!",dateMin:"The '{field}' field must be greater than or equal to {expected}!",dateMax:"The '{field}' field must be less than or equal to {expected}!",forbidden:"The '{field}' field is forbidden!",email:"The '{field}' field must be a valid e-mail!",url:"The '{field}' field must be a valid URL!",enumValue:"The '{field} field value '{expected}' does not match any of the allowed values!"},v=/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,E=/^\S+@\S+\.\S+$/,S=/^-?[0-9]\d*(\.\d+)?$/,k=/^[a-zA-Z]+$/,x=/^[a-zA-Z0-9]+$/,_=/^[a-zA-Z0-9_\-]+$/,A=/^https?:\/\/\S+/,O=/^[_$a-zA-Z][_$a-zA-Z0-9]*$/,T=/["'\\\n\r\u2028\u2029]/g;return y.prototype.validate=function(e,t){return this.compile(t)(e)},y.prototype.compile=function(e){var t=this;if(Array.isArray(e)){if(0==e.length)throw Error("If the schema is an Array, must contain at least one element!");var r=this.compileSchemaType(e);return this.cache.clear(),function(e,n,i){return t.checkSchemaType(e,r,n,i||null)}}var n=this.compileSchemaObject(e);return this.cache.clear(),function(e,r,i){return t.checkSchemaObject(e,n,r,i||null)}},y.prototype.compileSchemaObject=function(e){var t=this;if(null===e||"object"!=typeof e||Array.isArray(e))throw Error("Invalid schema!");var r=this.cache.get(e);if(r)return r.cycle=!0,r;r={cycle:!1,properties:null,compiledObjectFunction:null,objectStack:[]},this.cache.set(e,r),r.properties=Object.keys(e).map(function(r){return{name:r,compiledType:t.compileSchemaType(e[r])}});var n=[];n.push("let res;"),n.push("let propertyPath;"),n.push("const errors = [];");for(var i=0;i<r.properties.length;i++){var a=r.properties[i],o=d(a.name),u=O.test(o)?"value."+o:'value["'+o+'"]';n.push('propertyPath = (path !== undefined ? path + ".'+o+'" : "'+o+'");'),Array.isArray(a.compiledType)?n.push("res = this.checkSchemaType("+u+", properties["+i+"].compiledType, propertyPath, value);"):n.push("res = this.checkSchemaRule("+u+", properties["+i+"].compiledType, propertyPath, value);"),n.push("if (res !== true) {"),n.push("\tthis.handleResult(errors, propertyPath, res);"),n.push("}")}return n.push("return errors.length === 0 ? true : errors;"),r.compiledObjectFunction=new Function("value","properties","path","parent",n.join("\n")),r},y.prototype.compileSchemaType=function(e){var t=this;return Array.isArray(e)?(e=p(e.map(function(e){return t.compileSchemaType(e)})),1==e.length?e[0]:e):this.compileSchemaRule(e)},y.prototype.compileSchemaRule=function(e){"string"==typeof e&&(e={type:e});var t=this.rules[e.type];if(!t)throw Error("Invalid '"+e.type+"' type in validator schema!");var r=null,n=null;return"object"===e.type&&e.props?(r=this.compileSchemaObject(e.props),n=this.checkSchemaObject):"array"===e.type&&e.items&&(r=this.compileSchemaType(e.items),n=this.checkSchemaArray),{schemaRule:e,ruleFunction:t,dataFunction:n,dataParameter:r}},y.prototype.checkSchemaObject=function(e,t,r,n){return t.cycle?-1!==t.objectStack.indexOf(e)||(t.objectStack.push(e),e=this.checkSchemaObjectInner(e,t,r,n),t.objectStack.pop(),e):this.checkSchemaObjectInner(e,t,r,n)},y.prototype.checkSchemaObjectInner=function(e,t,r,n){return t.compiledObjectFunction.call(this,e,t.properties,r,n)},y.prototype.checkSchemaType=function(e,t,r,n){if(Array.isArray(t)){for(var i=[],a=t.length,o=0;o<a;o++){var u=this.checkSchemaRule(e,t[o],r,n);if(!0===u)return!0;this.handleResult(i,r,u)}return i}return this.checkSchemaRule(e,t,r,n)},y.prototype.checkSchemaArray=function(e,t,r,n){for(var i=[],a=e.length,o=0;o<a;o++){var u=(void 0!==r?r:"")+"["+o+"]",l=this.checkSchemaType(e[o],t,u,e,n);!0!==l&&this.handleResult(i,u,l)}return 0===i.length||i},y.prototype.checkSchemaRule=function(e,t,r,n){var i=t.schemaRule;return void 0===e||null===e?"forbidden"===i.type||!0===i.optional||(e=[],this.handleResult(e,r,this.makeError("required")),e):(i=t.ruleFunction.call(this,e,i,r,n),!0!==i?(e=[],this.handleResult(e,r,i),e):null===t.dataFunction||t.dataFunction.call(this,e,t.dataParameter,r,n))},y.prototype.handleResult=function(e,t,r){var n=this;(Array.isArray(r)?r:[r]).forEach(function(r){r.field||(r.field=t),r.message||(r.message=n.resolveMessage(r)),e.push(r)})},y.prototype.makeError=function(e,t,r){return{type:e,expected:t,actual:r}},y.prototype.resolveMessage=function(e){var t=this.messages[e.type];if(null!=t){var r=null!=e.expected?e.expected:"",n=null!=e.actual?e.actual:"";return t.replace(/\{field\}/g,e.field).replace(/\{expected\}/g,r).replace(/\{actual\}/g,n)}},y.prototype.add=function(e,t){this.rules[e]=t},y}var f=f||{};f.scope={},f.getGlobal=function(e){return"undefined"!=typeof window&&window===e?e:"undefined"!=typeof global&&null!=global?global:e},f.global=f.getGlobal(this),f.checkEs6ConformanceViaProxy=function(){try{var e={},t=Object.create(new f.global.Proxy(e,{get:function(r,n,i){return r==e&&"q"==n&&i==t}}));return!0===t.q}catch(e){return!1}},f.USE_PROXY_FOR_ES6_CONFORMANCE_CHECKS=!1,f.ES6_CONFORMANCE=f.USE_PROXY_FOR_ES6_CONFORMANCE_CHECKS&&f.checkEs6ConformanceViaProxy(),f.ASSUME_ES5=!1,f.ASSUME_NO_NATIVE_MAP=!1,f.ASSUME_NO_NATIVE_SET=!1,f.defineProperty=f.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(e,t,r){e!=Array.prototype&&e!=Object.prototype&&(e[t]=r.value)},f.SYMBOL_PREFIX="jscomp_symbol_",f.initSymbol=function(){f.initSymbol=function(){},f.global.Symbol||(f.global.Symbol=f.Symbol)},f.Symbol=function(){var e=0;return function(t){return f.SYMBOL_PREFIX+(t||"")+e++}}(),f.initSymbolIterator=function(){f.initSymbol();var e=f.global.Symbol.iterator;e||(e=f.global.Symbol.iterator=f.global.Symbol("iterator")),"function"!=typeof Array.prototype[e]&&f.defineProperty(Array.prototype,e,{configurable:!0,writable:!0,value:function(){return f.arrayIterator(this)}}),f.initSymbolIterator=function(){}},f.arrayIterator=function(e){var t=0;return f.iteratorPrototype(function(){return t<e.length?{done:!1,value:e[t++]}:{done:!0}})},f.iteratorPrototype=function(e){return f.initSymbolIterator(),e={next:e},e[f.global.Symbol.iterator]=function(){return this},e},f.makeIterator=function(e){f.initSymbolIterator();var t=e[Symbol.iterator];return t?t.call(e):f.arrayIterator(e)},f.owns=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},f.polyfill=function(e,t){if(t){var r=f.global;e=e.split(".");for(var n=0;n<e.length-1;n++){var i=e[n];i in r||(r[i]={}),r=r[i]}(t=t(n=r[e=e[e.length-1]]))!=n&&null!=t&&f.defineProperty(r,e,{configurable:!0,writable:!0,value:t})}},f.polyfill("WeakMap",function(e){function t(e){if(this.id_=(a+=Math.random()+1).toString(),e){f.initSymbol(),f.initSymbolIterator(),e=f.makeIterator(e);for(var t;!(t=e.next()).done;)t=t.value,this.set(t[0],t[1])}}function r(e){f.owns(e,i)||f.defineProperty(e,i,{value:{}})}function n(e){var t=Object[e];t&&(Object[e]=function(e){return r(e),t(e)})}if(f.USE_PROXY_FOR_ES6_CONFORMANCE_CHECKS){if(e&&f.ES6_CONFORMANCE)return e}else if(function(){if(!e||!Object.seal)return!1;try{var t=Object.seal({}),r=Object.seal({}),n=new e([[t,2],[r,3]]);return 2==n.get(t)&&3==n.get(r)&&(n.delete(t),n.set(r,4),!n.has(t)&&4==n.get(r))}catch(e){return!1}}())return e;var i="$jscomp_hidden_"+Math.random();n("freeze"),n("preventExtensions"),n("seal");var a=0;return t.prototype.set=function(e,t){if(r(e),!f.owns(e,i))throw Error("WeakMap key fail: "+e);return e[i][this.id_]=t,this},t.prototype.get=function(e){return f.owns(e,i)?e[i][this.id_]:void 0},t.prototype.has=function(e){return f.owns(e,i)&&f.owns(e[i],this.id_)},t.prototype.delete=function(e){return!(!f.owns(e,i)||!f.owns(e[i],this.id_))&&delete e[i][this.id_]},t},"es6","es3"),f.MapEntry=function(){},f.polyfill("Map",function(e){function t(){var e={};return e.previous=e.next=e.head=e}function r(e,t){var r=e.head_;return f.iteratorPrototype(function(){if(r){for(;r.head!=e.head_;)r=r.previous;for(;r.next!=r.head;)return r=r.next,{done:!1,value:t(r)};r=null}return{done:!0,value:void 0}})}function n(e,t){var r=t&&typeof t;"object"==r||"function"==r?a.has(t)?r=a.get(t):(r=""+ ++o,a.set(t,r)):r="p_"+t;var n=e.data_[r];if(n&&f.owns(e.data_,r))for(e=0;e<n.length;e++){var i=n[e];if(t!==t&&i.key!==i.key||t===i.key)return{id:r,list:n,index:e,entry:i}}return{id:r,list:n,index:-1,entry:void 0}}function i(e){if(this.data_={},this.head_=t(),this.size=0,e){e=f.makeIterator(e);for(var r;!(r=e.next()).done;)r=r.value,this.set(r[0],r[1])}}if(f.USE_PROXY_FOR_ES6_CONFORMANCE_CHECKS){if(e&&f.ES6_CONFORMANCE)return e}else if(function(){if(f.ASSUME_NO_NATIVE_MAP||!e||"function"!=typeof e||!e.prototype.entries||"function"!=typeof Object.seal)return!1;try{var t=Object.seal({x:4}),r=new e(f.makeIterator([[t,"s"]]));if("s"!=r.get(t)||1!=r.size||r.get({x:4})||r.set({x:4},"t")!=r||2!=r.size)return!1;var n=r.entries(),i=n.next();return!i.done&&i.value[0]==t&&"s"==i.value[1]&&!((i=n.next()).done||4!=i.value[0].x||"t"!=i.value[1]||!n.next().done)}catch(e){return!1}}())return e;f.initSymbol(),f.initSymbolIterator();var a=new WeakMap;i.prototype.set=function(e,t){var r=n(this,e);return r.list||(r.list=this.data_[r.id]=[]),r.entry?r.entry.value=t:(r.entry={next:this.head_,previous:this.head_.previous,head:this.head_,key:e,value:t},r.list.push(r.entry),this.head_.previous.next=r.entry,this.head_.previous=r.entry,this.size++),this},i.prototype.delete=function(e){return!(!(e=n(this,e)).entry||!e.list)&&(e.list.splice(e.index,1),e.list.length||delete this.data_[e.id],e.entry.previous.next=e.entry.next,e.entry.next.previous=e.entry.previous,e.entry.head=null,this.size--,!0)},i.prototype.clear=function(){this.data_={},this.head_=this.head_.previous=t(),this.size=0},i.prototype.has=function(e){return!!n(this,e).entry},i.prototype.get=function(e){return(e=n(this,e).entry)&&e.value},i.prototype.entries=function(){return r(this,function(e){return[e.key,e.value]})},i.prototype.keys=function(){return r(this,function(e){return e.key})},i.prototype.values=function(){return r(this,function(e){return e.value})},i.prototype.forEach=function(e,t){for(var r,n=this.entries();!(r=n.next()).done;)r=r.value,e.call(t,r[1],r[0],this)},i.prototype[Symbol.iterator]=i.prototype.entries;var o=0;return i},"es6","es3"),f.iteratorFromArray=function(e,t){f.initSymbolIterator(),e instanceof String&&(e+="");var r=0,n={next:function(){if(r<e.length){var i=r++;return{value:t(i,e[i]),done:!1}}return n.next=function(){return{done:!0,value:void 0}},n.next()}};return n[Symbol.iterator]=function(){return n},n},f.polyfill("Array.prototype.keys",function(e){return e||function(){return f.iteratorFromArray(this,function(e){return e})}},"es6","es3"),"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):this.FastestValidator=r();

@@ -6,3 +6,3 @@ "use strict";

messages: {
stringMin: "A(z) '{name}' mező túl rövid. Minimum: {0}, Jelenleg: {1}"
stringMin: "A(z) '{field}' mező túl rövid. Minimum: {expected}, Jelenleg: {actual}"
}

@@ -9,0 +9,0 @@ });

@@ -14,2 +14,6 @@ "use strict";

stringEnum: "The '{field}' field does not match any of the allowed values!",
stringNumeric: "The '{field}' field must be a numeric string",
stringAlpha: "The '{field}' field must be an alphabetic string",
stringAlphanum: "The '{field}' field must be an alphanumeric string",
stringAlphadash: "The '{field}' field must be an alphadash string",

@@ -45,3 +49,5 @@ number: "The '{field}' field must be a number!",

url: "The '{field}' field must be a valid URL!",
enumValue: "The '{field} field value '{expected}' does not match any of the allowed values!",
};
"use strict";
const NUMERIC_PATTERN = /^-?[0-9]\d*(\.\d+)?$/;
const ALPHA_PATTERN = /^[a-zA-Z]+$/;
const ALPHANUM_PATTERN = /^[a-zA-Z0-9]+$/;
const ALPHADASH_PATTERN = /^[a-zA-Z0-9_\-]+$/;
module.exports = function checkString(value, schema) {

@@ -8,9 +13,2 @@ if (typeof value !== "string") {

/* TODO: charset
alpha: /^[a-zA-Z]+$/
alphaNum: /^[a-zA-Z0-9]+$/
alphaDash: /^[a-zA-Z0-9_\-]+$/
*/
const valueLength = value.length;

@@ -34,4 +32,6 @@

if (schema.pattern != null && !schema.pattern.test(value)) {
return this.makeError("stringPattern", schema.pattern );
if (schema.pattern != null) {
const pattern = typeof schema.pattern == "string" ? new RegExp(schema.pattern, schema.patternFlags) : schema.pattern;
if (!pattern.test(value))
return this.makeError("stringPattern", pattern );
}

@@ -47,3 +47,19 @@

if (schema.numeric === true && !NUMERIC_PATTERN.test(value) ) {
return this.makeError("stringNumeric", "A numeric string", value);
}
if(schema.alpha === true && !ALPHA_PATTERN.test(value)) {
return this.makeError("stringAlpha", "An alphabetic string", value);
}
if(schema.alphanum === true && !ALPHANUM_PATTERN.test(value)) {
return this.makeError("stringAlphanum", "An alphanumeric string", value);
}
if(schema.alphadash === true && !ALPHADASH_PATTERN.test(value)) {
return this.makeError("stringAlphadash", "An alphadash string", value);
}
return true;
};

@@ -36,15 +36,15 @@ "use strict";

switch (character) {
case "\"":
case "'":
case "\\":
return "\\" + character;
case "\"":
case "'":
case "\\":
return "\\" + character;
// Four possible LineTerminator characters need to be escaped:
case "\n":
return "\\n";
case "\r":
return "\\r";
case "\u2028":
return "\\u2028";
case "\u2029":
return "\\u2029";
case "\n":
return "\\n";
case "\r":
return "\\r";
case "\u2028":
return "\\u2028";
case "\u2029":
return "\\u2029";
}

@@ -61,3 +61,3 @@ });

this.opts = {
messages: defaultMessages
messages: deepExtend({}, defaultMessages)
};

@@ -102,4 +102,4 @@

this.cache.clear();
return function(value) {
return self.checkSchemaType(value, rules, undefined, null);
return function(value, path, parent) {
return self.checkSchemaType(value, rules, path, parent || null);
};

@@ -110,4 +110,4 @@ }

this.cache.clear();
return function(value) {
return self.checkSchemaObject(value, rule, undefined, null);
return function(value, path, parent) {
return self.checkSchemaObject(value, rule, path, parent || null);
};

@@ -114,0 +114,0 @@ };

{
"name": "fastest-validator",
"version": "0.6.12",
"version": "0.6.13",
"description": "The fastest JS validator library for NodeJS",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -436,2 +436,6 @@ ![Photos from @ikukevk](https://user-images.githubusercontent.com/306521/30183963-9c722dca-941c-11e7-9e83-c78377ad7f9d.jpg)

`enum` | `null` | The value must be an element of the `enum` array.
`alpha` | `null` | The value must be an alphabetic string.
`numeric` | `null` | The value must be a numeric string.
`alphanum` | `null` | The value must be an alphanumeric string.
`alphadash` | `null` | The value must be an alphabetic string that contains dashes.

@@ -438,0 +442,0 @@

Sorry, the diff of this file is not supported yet

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