Socket
Socket
Sign inDemoInstall

schema-typed

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

schema-typed - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

CHANGELOG.md

12

lib/ArrayType.js

@@ -35,3 +35,3 @@ 'use strict';

_Type.prototype.pushCheck.call(_this, function (v) {
_Type.prototype.pushRule.call(_this, function (v) {
return Array.isArray(v);

@@ -43,3 +43,3 @@ }, errorMessage);

ArrayType.prototype.rangeLength = function rangeLength(minLength, maxLength, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return value.length >= minLength && value.length <= maxLength;

@@ -51,3 +51,3 @@ }, errorMessage);

ArrayType.prototype.minLength = function minLength(_minLength, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return value.length >= _minLength;

@@ -59,3 +59,3 @@ }, errorMessage);

ArrayType.prototype.maxLength = function maxLength(_maxLength, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return value.length <= _maxLength;

@@ -67,3 +67,3 @@ }, errorMessage);

ArrayType.prototype.unrepeatable = function unrepeatable(errorMessage) {
_Type.prototype.pushCheck.call(this, function (items) {
_Type.prototype.pushRule.call(this, function (items) {
var hash = {};

@@ -92,3 +92,3 @@ /* eslint-disable */

ArrayType.prototype.of = function of(type, errorMessage) {
_Type.prototype.pushCheck.call(this, function (items) {
_Type.prototype.pushRule.call(this, function (items) {
var valids = items.map(function (value) {

@@ -95,0 +95,0 @@ return type.check(value);

@@ -35,3 +35,3 @@ 'use strict';

_Type.prototype.pushCheck.call(_this, function (v) {
_Type.prototype.pushRule.call(_this, function (v) {
return typeof v === 'boolean';

@@ -38,0 +38,0 @@ }, errorMessage);

@@ -35,3 +35,3 @@ 'use strict';

_Type.prototype.pushCheck.call(_this, function (value) {
_Type.prototype.pushRule.call(_this, function (value) {
return !/Invalid|NaN/.test(new Date(value));

@@ -43,3 +43,3 @@ }, errorMessage);

DateType.prototype.range = function range(min, max, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return new Date(value) >= new Date(min) && new Date(value) <= new Date(max);

@@ -51,3 +51,3 @@ }, errorMessage);

DateType.prototype.min = function min(_min, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return new Date(value) >= new Date(_min);

@@ -59,3 +59,3 @@ }, errorMessage);

DateType.prototype.max = function max(_max, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return new Date(value) <= new Date(_max);

@@ -62,0 +62,0 @@ }, errorMessage);

@@ -39,3 +39,3 @@ 'use strict';

_Type.prototype.pushCheck.call(_this, function (value) {
_Type.prototype.pushRule.call(_this, function (value) {
return (/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value)

@@ -48,3 +48,3 @@ );

NumberType.prototype.isInteger = function isInteger(errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return (/^-?\d+$/.test(value)

@@ -57,3 +57,3 @@ );

NumberType.prototype.pattern = function pattern(regexp, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return regexp.test(value);

@@ -65,3 +65,3 @@ }, errorMessage);

NumberType.prototype.isOneOf = function isOneOf(numLst, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return numLst.includes(FN(value));

@@ -73,3 +73,3 @@ }, errorMessage);

NumberType.prototype.range = function range(min, max, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return FN(value) >= min && FN(value) <= max;

@@ -81,3 +81,3 @@ }, errorMessage);

NumberType.prototype.min = function min(_min, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return FN(value) >= _min;

@@ -89,3 +89,3 @@ }, errorMessage);

NumberType.prototype.max = function max(_max, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return FN(value) <= _max;

@@ -92,0 +92,0 @@ }, errorMessage);

@@ -37,3 +37,3 @@ 'use strict';

_Type.prototype.pushCheck.call(_this, function (v) {
_Type.prototype.pushRule.call(_this, function (v) {
return (typeof v === 'undefined' ? 'undefined' : _typeof(v)) === 'object';

@@ -54,3 +54,3 @@ }, errorMessage);

ObjectType.prototype.shape = function shape(types) {
_Type.prototype.pushCheck.call(this, function (values) {
_Type.prototype.pushRule.call(this, function (values) {
var valids = Object.entries(types).map(function (item) {

@@ -57,0 +57,0 @@ var key = item[0];

@@ -34,3 +34,4 @@ 'use strict';

if (!fieldChecker) {
return { hasError: false }; // fieldValue can be anything if no schema defined
// fieldValue can be anything if no schema defined
return { hasError: false };
}

@@ -50,2 +51,31 @@ return fieldChecker.check(fieldValue, data);

Schema.prototype.checkForFieldAsync = function checkForFieldAsync(fieldName, fieldValue, data) {
var fieldChecker = this.schema[fieldName];
if (!fieldChecker) {
// fieldValue can be anything if no schema defined
return Promise.resolve({ hasError: false });
}
return fieldChecker.checkAsync(fieldValue, data);
};
Schema.prototype.checkAsync = function checkAsync(data) {
var _this2 = this;
var checkResult = {};
var promises = [];
var keys = [];
Object.keys(this.schema).forEach(function (key) {
keys.push(key);
promises.push(_this2.checkForFieldAsync(key, data[key], data));
});
return Promise.all(promises).then(function (values) {
for (var i = 0; i < values.length; i += 1) {
checkResult[keys[i]] = values[i];
}
return checkResult;
});
};
return Schema;

@@ -52,0 +82,0 @@ }();

@@ -39,3 +39,3 @@ 'use strict';

_Type.prototype.pushCheck.call(_this, function (v) {
_Type.prototype.pushRule.call(_this, function (v) {
return typeof v === 'string';

@@ -47,3 +47,3 @@ }, errorMessage);

StringType.prototype.containsLetter = function containsLetter(errorMessage) {
_Type.prototype.pushCheck.call(this, function (v) {
_Type.prototype.pushRule.call(this, function (v) {
return (/[a-zA-Z]/.test(v)

@@ -56,3 +56,3 @@ );

StringType.prototype.containsUppercaseLetter = function containsUppercaseLetter(errorMessage) {
_Type.prototype.pushCheck.call(this, function (v) {
_Type.prototype.pushRule.call(this, function (v) {
return (/[A-Z]/.test(v)

@@ -65,3 +65,3 @@ );

StringType.prototype.containsLowercaseLetter = function containsLowercaseLetter(errorMessage) {
_Type.prototype.pushCheck.call(this, function (v) {
_Type.prototype.pushRule.call(this, function (v) {
return (/[a-z]/.test(v)

@@ -74,3 +74,3 @@ );

StringType.prototype.containsLetterOnly = function containsLetterOnly(errorMessage) {
_Type.prototype.pushCheck.call(this, function (v) {
_Type.prototype.pushRule.call(this, function (v) {
return (/^[a-zA-Z]+$/.test(v)

@@ -83,3 +83,3 @@ );

StringType.prototype.containsNumber = function containsNumber(errorMessage) {
_Type.prototype.pushCheck.call(this, function (v) {
_Type.prototype.pushRule.call(this, function (v) {
return (/[0-9]/.test(v)

@@ -92,3 +92,3 @@ );

StringType.prototype.isOneOf = function isOneOf(strArr, errorMessage) {
_Type.prototype.pushCheck.call(this, function (v) {
_Type.prototype.pushRule.call(this, function (v) {
return ~strArr.indexOf(v);

@@ -102,3 +102,3 @@ }, errorMessage);

var regexp = /^(([^<>()\[\]\\.,;:\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,}))$/;
_Type.prototype.pushCheck.call(this, function (v) {
_Type.prototype.pushRule.call(this, function (v) {
return regexp.test(v);

@@ -111,3 +111,3 @@ }, errorMessage);

var regexp = new RegExp('^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$', 'i');
_Type.prototype.pushCheck.call(this, function (v) {
_Type.prototype.pushRule.call(this, function (v) {
return regexp.test(v);

@@ -120,3 +120,3 @@ }, errorMessage);

var regexp = /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i;
_Type.prototype.pushCheck.call(this, function (v) {
_Type.prototype.pushRule.call(this, function (v) {
return regexp.test(v);

@@ -128,3 +128,3 @@ }, errorMessage);

StringType.prototype.pattern = function pattern(regexp, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return regexp.test(value);

@@ -136,3 +136,3 @@ }, errorMessage);

StringType.prototype.rangeLength = function rangeLength(minLength, maxLength, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return value.length >= minLength && value.length <= maxLength;

@@ -144,3 +144,3 @@ }, errorMessage);

StringType.prototype.minLength = function minLength(_minLength, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return [].concat(_toConsumableArray(value)).length >= _minLength;

@@ -152,3 +152,3 @@ }, errorMessage);

StringType.prototype.maxLength = function maxLength(_maxLength, errorMessage) {
_Type.prototype.pushCheck.call(this, function (value) {
_Type.prototype.pushRule.call(this, function (value) {
return [].concat(_toConsumableArray(value)).length <= _maxLength;

@@ -155,0 +155,0 @@ }, errorMessage);

@@ -29,3 +29,3 @@ 'use strict';

function getCheck(data) {
function createValidator(data) {
return function (value, rules) {

@@ -39,3 +39,3 @@ for (var i = 0; i < rules.length; i += 1) {

if (typeof checkResult === 'boolean' && !checkResult) {
if (checkResult === false) {
return { hasError: true, errorMessage: errorMessage };

@@ -51,2 +51,30 @@ } else if ((typeof checkResult === 'undefined' ? 'undefined' : _typeof(checkResult)) === 'object') {

function createValidatorAsync(data) {
function check(errorMessage) {
return function (checkResult) {
if (checkResult === false) {
return { hasError: true, errorMessage: errorMessage };
} else if ((typeof checkResultAsync === 'undefined' ? 'undefined' : _typeof(checkResultAsync)) === 'object') {
return checkResult;
}
return null;
};
}
return function (value, rules) {
var promises = rules.map(function (rule) {
var onValid = rule.onValid,
errorMessage = rule.errorMessage;
return Promise.resolve(onValid(value, data)).then(check(errorMessage));
});
return Promise.all(promises).then(function (results) {
return results.find(function (item) {
return item && item.hasError;
});
});
};
}
var Type = function () {

@@ -61,2 +89,3 @@ function Type(name) {

this.rules = [];
this.priorityRules = []; // Priority check rule
}

@@ -69,18 +98,6 @@

var checkValue = getCheck(data);
var rules = [];
var customRules = [];
var checkStatus = null;
var validator = createValidator(data);
var checkStatus = validator(value, this.priorityRules);
this.rules.forEach(function (item) {
if (item.customRule) {
customRules.push(item);
} else {
rules.push(item);
}
});
checkStatus = checkValue(value, customRules);
if (checkStatus !== null) {
if (checkStatus) {
return checkStatus;

@@ -93,22 +110,46 @@ }

checkStatus = checkValue(value, rules);
return validator(value, this.rules) || { hasError: false };
};
if (checkStatus !== null) {
return checkStatus;
Type.prototype.checkAsync = function checkAsync(value, data) {
var _this = this;
if (this.required && !checkRequired(value, this.trim)) {
return Promise.resolve({ hasError: true, errorMessage: this.requiredMessage });
}
return { hasError: false };
var validator = createValidatorAsync(data);
return new Promise(function (resolve) {
return validator(value, _this.priorityRules).then(function (checkStatus) {
if (checkStatus) {
resolve(checkStatus);
}
}).then(function () {
if (!_this.required && isEmpty(value)) {
resolve({ hasError: false });
}
}).then(function () {
return validator(value, _this.rules);
}).then(function (checkStatus) {
if (checkStatus) {
resolve(checkStatus);
}
return { hasError: false };
});
});
};
Type.prototype.pushCheck = function pushCheck(onValid, errorMessage, customRule) {
Type.prototype.pushRule = function pushRule(onValid, errorMessage, priority) {
errorMessage = errorMessage || this.rules[0].errorMessage;
this.rules.push({
onValid: onValid,
errorMessage: errorMessage,
customRule: customRule
});
if (priority) {
this.priorityRules.push({ onValid: onValid, errorMessage: errorMessage });
} else {
this.rules.push({ onValid: onValid, errorMessage: errorMessage });
}
};
Type.prototype.addRule = function addRule(onValid, errorMessage, priority) {
this.pushCheck(onValid, errorMessage, priority);
this.pushRule(onValid, errorMessage, priority);
return this;

@@ -115,0 +156,0 @@ };

{
"name": "schema-typed",
"version": "1.1.0",
"version": "1.2.0",
"description": "Schema for data modeling & validation",

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

@@ -117,2 +117,42 @@ # schema-typed

## Custom verification - Asynchronous check
For example, verify that the mailbox is duplicated
```js
function asyncCheckEmail(email) {
return new Promise(resolve => {
setTimeout(() => {
if (email === 'foo@domain.com') {
resolve(false);
} else {
resolve(true);
}
}, 500);
});
}
const model = SchemaModel({
email: StringType()
.isEmail('Please input the correct email address')
.addRule((value, data) => {
return asyncCheckEmail(value);
}, 'Email address already exists')
.isRequired('This field cannot be empty')
});
model.checkAsync({ email: 'foo@domain.com' }).then(result => {
console.log(result);
});
/**
{
email: {
hasError: true,
errorMessage: 'Email address already exists'
}
};
**/
```
## Validate nested objects

@@ -226,2 +266,26 @@

- checkAsync(data: Object)
```js
const model = SchemaModel({
username: StringType()
.isRequired('This field required')
.addRule(value => {
return new Promise(resolve => {
// Asynchronous processing logic
});
}, 'Username already exists'),
email: StringType().isEmail('Please input the correct email address')
});
model
.checkAsync({
username: 'root',
email: 'root@email.com'
})
.then(result => {
// Data verification result
});
```
- checkForField(fieldName: string, fieldValue: any, data: Object)

@@ -238,2 +302,21 @@

- checkForFieldAsync(fieldName: string, fieldValue: any, data: Object)
```js
const model = SchemaModel({
username: StringType()
.isRequired('This field required')
.addRule(value => {
return new Promise(resolve => {
// Asynchronous processing logic
});
}, 'Username already exists'),
email: StringType().isEmail('Please input the correct email address')
});
model.checkForFieldAsync('username', 'root').then(result => {
// Data verification result
});
```
### StringType(errorMessage: string)

@@ -240,0 +323,0 @@

@@ -10,7 +10,7 @@ import Type from './Type';

super('array');
super.pushCheck(v => Array.isArray(v), errorMessage);
super.pushRule(v => Array.isArray(v), errorMessage);
}
rangeLength(minLength, maxLength, errorMessage) {
super.pushCheck(value => value.length >= minLength && value.length <= maxLength, errorMessage);
super.pushRule(value => value.length >= minLength && value.length <= maxLength, errorMessage);
return this;

@@ -20,3 +20,3 @@ }

minLength(minLength, errorMessage) {
super.pushCheck(value => value.length >= minLength, errorMessage);
super.pushRule(value => value.length >= minLength, errorMessage);
return this;

@@ -26,3 +26,3 @@ }

maxLength(maxLength, errorMessage) {
super.pushCheck(value => value.length <= maxLength, errorMessage);
super.pushRule(value => value.length <= maxLength, errorMessage);
return this;

@@ -32,3 +32,3 @@ }

unrepeatable(errorMessage) {
super.pushCheck(items => {
super.pushRule(items => {
let hash = {};

@@ -55,3 +55,3 @@ /* eslint-disable */

of(type, errorMessage) {
super.pushCheck(items => {
super.pushRule(items => {
let valids = items.map(value => type.check(value));

@@ -58,0 +58,0 @@ let errors = valids.filter(item => item.hasError) || [];

@@ -10,3 +10,3 @@ import Type from './Type';

super('boolean');
super.pushCheck(v => typeof v === 'boolean', errorMessage);
super.pushRule(v => typeof v === 'boolean', errorMessage);
}

@@ -13,0 +13,0 @@ }

@@ -10,7 +10,7 @@ import Type from './Type';

super('date');
super.pushCheck(value => !/Invalid|NaN/.test(new Date(value)), errorMessage);
super.pushRule(value => !/Invalid|NaN/.test(new Date(value)), errorMessage);
}
range(min, max, errorMessage) {
super.pushCheck(
super.pushRule(
value => new Date(value) >= new Date(min) && new Date(value) <= new Date(max),

@@ -23,3 +23,3 @@ errorMessage

min(min, errorMessage) {
super.pushCheck(value => new Date(value) >= new Date(min), errorMessage);
super.pushRule(value => new Date(value) >= new Date(min), errorMessage);
return this;

@@ -29,3 +29,3 @@ }

max(max, errorMessage) {
super.pushCheck(value => new Date(value) <= new Date(max), errorMessage);
super.pushRule(value => new Date(value) <= new Date(max), errorMessage);
return this;

@@ -32,0 +32,0 @@ }

@@ -14,7 +14,7 @@ import Type from './Type';

super('number');
super.pushCheck(value => /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value), errorMessage);
super.pushRule(value => /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value), errorMessage);
}
isInteger(errorMessage) {
super.pushCheck(value => /^-?\d+$/.test(value), errorMessage);
super.pushRule(value => /^-?\d+$/.test(value), errorMessage);
return this;

@@ -24,3 +24,3 @@ }

pattern(regexp, errorMessage) {
super.pushCheck(value => regexp.test(value), errorMessage);
super.pushRule(value => regexp.test(value), errorMessage);
return this;

@@ -30,3 +30,3 @@ }

isOneOf(numLst, errorMessage) {
super.pushCheck(value => numLst.includes(FN(value)), errorMessage);
super.pushRule(value => numLst.includes(FN(value)), errorMessage);
return this;

@@ -36,3 +36,3 @@ }

range(min, max, errorMessage) {
super.pushCheck(value => FN(value) >= min && FN(value) <= max, errorMessage);
super.pushRule(value => FN(value) >= min && FN(value) <= max, errorMessage);
return this;

@@ -42,3 +42,3 @@ }

min(min, errorMessage) {
super.pushCheck(value => FN(value) >= min, errorMessage);
super.pushRule(value => FN(value) >= min, errorMessage);
return this;

@@ -48,3 +48,3 @@ }

max(max, errorMessage) {
super.pushCheck(value => FN(value) <= max, errorMessage);
super.pushRule(value => FN(value) <= max, errorMessage);
return this;

@@ -51,0 +51,0 @@ }

@@ -10,3 +10,3 @@ import Type from './Type';

super('object');
super.pushCheck(v => typeof v === 'object', errorMessage);
super.pushRule(v => typeof v === 'object', errorMessage);
}

@@ -22,3 +22,3 @@

shape(types) {
super.pushCheck(values => {
super.pushRule(values => {
let valids = Object.entries(types).map(item => {

@@ -25,0 +25,0 @@ let key = item[0];

@@ -17,5 +17,6 @@ import StringType from './StringType';

checkForField(fieldName, fieldValue, data) {
let fieldChecker = this.schema[fieldName];
const fieldChecker = this.schema[fieldName];
if (!fieldChecker) {
return { hasError: false }; // fieldValue can be anything if no schema defined
// fieldValue can be anything if no schema defined
return { hasError: false };
}

@@ -26,3 +27,3 @@ return fieldChecker.check(fieldValue, data);

check(data) {
let checkResult = {};
const checkResult = {};
Object.keys(this.schema).forEach(key => {

@@ -33,2 +34,29 @@ checkResult[key] = this.checkForField(key, data[key], data);

}
checkForFieldAsync(fieldName, fieldValue, data) {
const fieldChecker = this.schema[fieldName];
if (!fieldChecker) {
// fieldValue can be anything if no schema defined
return Promise.resolve({ hasError: false });
}
return fieldChecker.checkAsync(fieldValue, data);
}
checkAsync(data) {
const checkResult = {};
const promises = [];
const keys = [];
Object.keys(this.schema).forEach(key => {
keys.push(key);
promises.push(this.checkForFieldAsync(key, data[key], data));
});
return Promise.all(promises).then(values => {
for (let i = 0; i < values.length; i += 1) {
checkResult[keys[i]] = values[i];
}
return checkResult;
});
}
}

@@ -35,0 +63,0 @@

@@ -12,7 +12,7 @@ import Type from './Type';

super('string');
super.pushCheck(v => typeof v === 'string', errorMessage);
super.pushRule(v => typeof v === 'string', errorMessage);
}
containsLetter(errorMessage) {
super.pushCheck(v => /[a-zA-Z]/.test(v), errorMessage);
super.pushRule(v => /[a-zA-Z]/.test(v), errorMessage);
return this;

@@ -22,3 +22,3 @@ }

containsUppercaseLetter(errorMessage) {
super.pushCheck(v => /[A-Z]/.test(v), errorMessage);
super.pushRule(v => /[A-Z]/.test(v), errorMessage);
return this;

@@ -28,3 +28,3 @@ }

containsLowercaseLetter(errorMessage) {
super.pushCheck(v => /[a-z]/.test(v), errorMessage);
super.pushRule(v => /[a-z]/.test(v), errorMessage);
return this;

@@ -34,3 +34,3 @@ }

containsLetterOnly(errorMessage) {
super.pushCheck(v => /^[a-zA-Z]+$/.test(v), errorMessage);
super.pushRule(v => /^[a-zA-Z]+$/.test(v), errorMessage);
return this;

@@ -40,3 +40,3 @@ }

containsNumber(errorMessage) {
super.pushCheck(v => /[0-9]/.test(v), errorMessage);
super.pushRule(v => /[0-9]/.test(v), errorMessage);
return this;

@@ -46,3 +46,3 @@ }

isOneOf(strArr, errorMessage) {
super.pushCheck(v => ~strArr.indexOf(v), errorMessage);
super.pushRule(v => ~strArr.indexOf(v), errorMessage);
return this;

@@ -54,3 +54,3 @@ }

const regexp = /^(([^<>()\[\]\\.,;:\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,}))$/;
super.pushCheck(v => regexp.test(v), errorMessage);
super.pushRule(v => regexp.test(v), errorMessage);
return this;

@@ -64,3 +64,3 @@ }

);
super.pushCheck(v => regexp.test(v), errorMessage);
super.pushRule(v => regexp.test(v), errorMessage);
return this;

@@ -70,7 +70,7 @@ }

const regexp = /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i;
super.pushCheck(v => regexp.test(v), errorMessage);
super.pushRule(v => regexp.test(v), errorMessage);
return this;
}
pattern(regexp, errorMessage) {
super.pushCheck(value => regexp.test(value), errorMessage);
super.pushRule(value => regexp.test(value), errorMessage);
return this;

@@ -80,3 +80,3 @@ }

rangeLength(minLength, maxLength, errorMessage) {
super.pushCheck(value => value.length >= minLength && value.length <= maxLength, errorMessage);
super.pushRule(value => value.length >= minLength && value.length <= maxLength, errorMessage);
return this;

@@ -86,3 +86,3 @@ }

minLength(minLength, errorMessage) {
super.pushCheck(value => [...value].length >= minLength, errorMessage);
super.pushRule(value => [...value].length >= minLength, errorMessage);
return this;

@@ -92,3 +92,3 @@ }

maxLength(maxLength, errorMessage) {
super.pushCheck(value => [...value].length <= maxLength, errorMessage);
super.pushRule(value => [...value].length <= maxLength, errorMessage);
return this;

@@ -95,0 +95,0 @@ }

@@ -19,9 +19,9 @@ function isEmpty(value) {

function getCheck(data) {
function createValidator(data) {
return (value, rules) => {
for (let i = 0; i < rules.length; i += 1) {
let { onValid, errorMessage } = rules[i];
let checkResult = onValid(value, data);
const { onValid, errorMessage } = rules[i];
const checkResult = onValid(value, data);
if (typeof checkResult === 'boolean' && !checkResult) {
if (checkResult === false) {
return { hasError: true, errorMessage };

@@ -37,2 +37,24 @@ } else if (typeof checkResult === 'object') {

function createValidatorAsync(data) {
function check(errorMessage) {
return checkResult => {
if (checkResult === false) {
return { hasError: true, errorMessage };
} else if (typeof checkResultAsync === 'object') {
return checkResult;
}
return null;
};
}
return (value, rules) => {
const promises = rules.map(rule => {
const { onValid, errorMessage } = rule;
return Promise.resolve(onValid(value, data)).then(check(errorMessage));
});
return Promise.all(promises).then(results => results.find(item => item && item.hasError));
};
}
class Type {

@@ -45,4 +67,4 @@ constructor(name) {

this.rules = [];
this.priorityRules = []; // Priority check rule
}
check(value, data) {

@@ -53,18 +75,6 @@ if (this.required && !checkRequired(value, this.trim)) {

const checkValue = getCheck(data);
let rules = [];
let customRules = [];
let checkStatus = null;
const validator = createValidator(data);
const checkStatus = validator(value, this.priorityRules);
this.rules.forEach(item => {
if (item.customRule) {
customRules.push(item);
} else {
rules.push(item);
}
});
checkStatus = checkValue(value, customRules);
if (checkStatus !== null) {
if (checkStatus) {
return checkStatus;

@@ -77,20 +87,44 @@ }

checkStatus = checkValue(value, rules);
return validator(value, this.rules) || { hasError: false };
}
if (checkStatus !== null) {
return checkStatus;
checkAsync(value, data) {
if (this.required && !checkRequired(value, this.trim)) {
return Promise.resolve({ hasError: true, errorMessage: this.requiredMessage });
}
return { hasError: false };
const validator = createValidatorAsync(data);
return new Promise(resolve =>
validator(value, this.priorityRules)
.then(checkStatus => {
if (checkStatus) {
resolve(checkStatus);
}
})
.then(() => {
if (!this.required && isEmpty(value)) {
resolve({ hasError: false });
}
})
.then(() => validator(value, this.rules))
.then(checkStatus => {
if (checkStatus) {
resolve(checkStatus);
}
return { hasError: false };
})
);
}
pushCheck(onValid, errorMessage, customRule) {
pushRule(onValid, errorMessage, priority) {
errorMessage = errorMessage || this.rules[0].errorMessage;
this.rules.push({
onValid,
errorMessage,
customRule
});
if (priority) {
this.priorityRules.push({ onValid, errorMessage });
} else {
this.rules.push({ onValid, errorMessage });
}
}
addRule(onValid, errorMessage, priority) {
this.pushCheck(onValid, errorMessage, priority);
this.pushRule(onValid, errorMessage, priority);
return this;

@@ -97,0 +131,0 @@ }

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