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 0.2.0 to 0.2.1

12

lib/ArrayType.js

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

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

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

ArrayType.prototype.rangeLength = function rangeLength(minLength, maxLength, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
_Type.prototype.pushCheck.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.addRule.call(this, function (value) {
_Type.prototype.pushCheck.call(this, function (value) {
return value.length >= _minLength;

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

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

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

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

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

ArrayType.prototype.of = function of(type, errorMessage) {
_Type.prototype.addRule.call(this, function (items) {
_Type.prototype.pushCheck.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.addRule.call(_this, function (v) {
_Type.prototype.pushCheck.call(_this, function (v) {
return typeof v === 'boolean';

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

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

_Type.prototype.addRule.call(_this, function (value) {
_Type.prototype.pushCheck.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.addRule.call(this, function (value) {
_Type.prototype.pushCheck.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.addRule.call(this, function (value) {
_Type.prototype.pushCheck.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.addRule.call(this, function (value) {
_Type.prototype.pushCheck.call(this, function (value) {
return new Date(value) <= new Date(_max);

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

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

_Type.prototype.addRule.call(_this, function (value) {
_Type.prototype.pushCheck.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.addRule.call(this, function (value) {
_Type.prototype.pushCheck.call(this, function (value) {
return (/^-?\d+$/.test(value)

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

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

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

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

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

NumberType.prototype.range = function range(min, max, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
_Type.prototype.pushCheck.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.addRule.call(this, function (value) {
_Type.prototype.pushCheck.call(this, function (value) {
return FN(value) >= _min;

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

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

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

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

_Type.prototype.addRule.call(_this, function (v) {
_Type.prototype.pushCheck.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.addRule.call(this, function (values) {
_Type.prototype.pushCheck.call(this, function (values) {
var valids = Object.entries(types).map(function (item) {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

StringType.prototype.isOneOf = function isOneOf(strArr, errorMessage) {
_Type.prototype.addRule.call(this, function (v) {
_Type.prototype.pushCheck.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.addRule.call(this, function (v) {
_Type.prototype.pushCheck.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.addRule.call(this, function (v) {
_Type.prototype.pushCheck.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.addRule.call(this, function (v) {
_Type.prototype.pushCheck.call(this, function (v) {
return regexp.test(v);

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

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

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

StringType.prototype.rangeLength = function rangeLength(minLength, maxLength, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
_Type.prototype.pushCheck.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.addRule.call(this, function (value) {
_Type.prototype.pushCheck.call(this, function (value) {
return [].concat(_toConsumableArray(value)).length >= _minLength;

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

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

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

@@ -29,2 +29,22 @@ 'use strict';

function getCheck(data) {
return function (value, rules) {
for (var i = 0; i < rules.length; i += 1) {
var _rules$i = rules[i],
onValid = _rules$i.onValid,
errorMessage = _rules$i.errorMessage;
var checkResult = onValid(value, data);
if (typeof checkResult === 'boolean' && !checkResult) {
return { hasError: true, errorMessage: errorMessage };
} else if ((typeof checkResult === 'undefined' ? 'undefined' : _typeof(checkResult)) === 'object') {
return checkResult;
}
}
return null;
};
}
var Type = function () {

@@ -45,30 +65,43 @@ function Type(name) {

for (var i = 0; i < this.rules.length; i += 1) {
var _rules$i = this.rules[i],
onValid = _rules$i.onValid,
errorMessage = _rules$i.errorMessage;
var checkValue = getCheck(data);
var rules = [];
var customRules = [];
var checkStatus = null;
if (!this.required && isEmpty(value)) {
return { hasError: false };
this.rules.forEach(function (item) {
if (item.customRule) {
customRules.push(item);
} else {
rules.push(item);
}
});
var checkStatus = onValid(value, data);
checkStatus = checkValue(value, customRules);
if (checkStatus !== null) {
return checkStatus;
}
if (typeof checkStatus === 'boolean' && !checkStatus) {
return { hasError: true, errorMessage: errorMessage };
} else if ((typeof checkStatus === 'undefined' ? 'undefined' : _typeof(checkStatus)) === 'object') {
return checkStatus;
}
if (!this.required && isEmpty(value)) {
return { hasError: false };
}
checkStatus = checkValue(value, rules);
if (checkStatus !== null) {
return checkStatus;
}
return { hasError: false };
};
Type.prototype.addRule = function addRule(onValid, errorMessage) {
Type.prototype.pushCheck = function pushCheck(onValid, errorMessage, customRule) {
errorMessage = errorMessage || this.rules[0].errorMessage;
this.rules.push({
onValid: onValid,
errorMessage: errorMessage
errorMessage: errorMessage,
customRule: customRule
});
};
Type.prototype.addRule = function addRule(onValid, errorMessage) {
this.pushCheck(onValid, errorMessage, true);
return this;

@@ -75,0 +108,0 @@ };

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

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

@@ -127,3 +127,3 @@ # schema-typed

email: StringType().isEmail('Should be an email'),
age: numberType().min(18, 'Age should be greater than 18 years old')
age: NumberType().min(18, 'Age should be greater than 18 years old')
});

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

'info.email': StringType().isEmail('Should be an email'),
'info.age': numberType().min(18, 'Age should be greater than 18 years old')
'info.age': NumberType().min(18, 'Age should be greater than 18 years old')
});

@@ -315,3 +315,3 @@

```js
StringType().minLength(30, 'The maximum is only 30 characters.');
StringType().maxLength(30, 'The maximum is only 30 characters.');
```

@@ -510,2 +510,10 @@

## ⚠️ Notes
Check priority:
- 1.isRequired
- 2.addRule
- 3.Predefined rules (if there is no isRequired, value is empty, the rule is not executed)
[readm-cn]: https://github.com/rsuite/schema-typed/blob/master/README_zh.md

@@ -512,0 +520,0 @@ [npm-badge]: https://img.shields.io/npm/v/schema-typed.svg

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

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

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

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

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

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

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

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

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

of(type, errorMessage) {
super.addRule(items => {
super.pushCheck(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.addRule(v => typeof v === 'boolean', errorMessage);
super.pushCheck(v => typeof v === 'boolean', errorMessage);
}

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

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

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

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

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

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

max(max, errorMessage) {
super.addRule(value => new Date(value) <= new Date(max), errorMessage);
super.pushCheck(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.addRule(value => /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value), errorMessage);
super.pushCheck(value => /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value), errorMessage);
}
isInteger(errorMessage) {
super.addRule(value => /^-?\d+$/.test(value), errorMessage);
super.pushCheck(value => /^-?\d+$/.test(value), errorMessage);
return this;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

isOneOf(strArr, errorMessage) {
super.addRule(v => ~strArr.indexOf(v), errorMessage);
super.pushCheck(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.addRule(v => regexp.test(v), errorMessage);
super.pushCheck(v => regexp.test(v), errorMessage);
return this;

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

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

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

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

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

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

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

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

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

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

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

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

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

@@ -33,27 +50,41 @@ constructor(name) {

for (let i = 0; i < this.rules.length; i += 1) {
let { onValid, errorMessage } = this.rules[i];
const checkValue = getCheck(data);
let rules = [];
let customRules = [];
let checkStatus = null;
if (!this.required && isEmpty(value)) {
return { hasError: false };
this.rules.forEach(item => {
if (item.customRule) {
customRules.push(item);
} else {
rules.push(item);
}
});
let checkStatus = onValid(value, data);
checkStatus = checkValue(value, customRules);
if (checkStatus !== null) {
return checkStatus;
}
if (typeof checkStatus === 'boolean' && !checkStatus) {
return { hasError: true, errorMessage };
} else if (typeof checkStatus === 'object') {
return checkStatus;
}
if (!this.required && isEmpty(value)) {
return { hasError: false };
}
checkStatus = checkValue(value, rules);
if (checkStatus !== null) {
return checkStatus;
}
return { hasError: false };
}
addRule(onValid, errorMessage) {
pushCheck(onValid, errorMessage, customRule) {
errorMessage = errorMessage || this.rules[0].errorMessage;
this.rules.push({
onValid,
errorMessage
errorMessage,
customRule
});
}
addRule(onValid, errorMessage) {
this.pushCheck(onValid, errorMessage, true);
return this;

@@ -60,0 +91,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