Socket
Socket
Sign inDemoInstall

@hookstate/validation

Package Overview
Dependencies
4
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.0.1 to 4.0.0-rc2

dist/Placeholder.test.d.ts

2

dist/index.d.ts

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

export * from './Validation';
export * from './validation';
//# sourceMappingURL=index.d.ts.map

@@ -1,176 +0,123 @@

var PluginID = Symbol('Validate');
var emptyErrors = [];
var ValidationPluginInstance = /** @class */ (function () {
function ValidationPluginInstance() {
this.storeRules = {};
}
ValidationPluginInstance.prototype.getRulesAndNested = function (path) {
var result = this.storeRules;
path.forEach(function (p) {
if (typeof p === 'number') {
p = '*'; // limitation: support only validation for each element of array
function validation() {
return function () { return ({
onCreate: function () {
var storeRules = {};
var hidden = Symbol('hidden');
var emptyErrors = [];
function getRulesAndNested(path) {
var result = storeRules;
path.forEach(function (p) {
if (typeof p === 'number') {
p = '*'; // limitation: support only validation for each element of array
}
result = result && (result[p]);
});
return [result && result[hidden] ? Array.from(result[hidden].values()) : [],
result ? Object.keys(result) : []];
}
result = result && (result[p]);
});
return [result && result[PluginID] ? Array.from(result[PluginID].values()) : [],
result ? Object.keys(result) : []];
};
ValidationPluginInstance.prototype.addRule = function (path, r) {
var result = this.storeRules;
path.forEach(function (p, i) {
if (typeof p === 'number') {
p = '*'; // limitation: support only validation for each element of array
}
result[p] = result[p] || {};
result = result[p];
});
var existingRules = result[PluginID];
var newRuleFunction = r.rule.toString();
if (existingRules) {
if (existingRules.has(newRuleFunction)) {
return;
}
existingRules.set(newRuleFunction, r);
return;
}
var newMap = new Map();
newMap.set(newRuleFunction, r);
result[PluginID] = newMap;
};
ValidationPluginInstance.prototype.getErrors = function (l, depth, filter, first) {
var result = [];
var consistentResult = function () { return result.length === 0 ? emptyErrors : result; };
if (depth === 0) {
return consistentResult();
}
var _a = this.getRulesAndNested(l.path), existingRules = _a[0], nestedRulesKeys = _a[1];
for (var i = 0; i < existingRules.length; i += 1) {
var r = existingRules[i];
if (!r.rule(l.value)) {
var err = {
path: l.path,
message: typeof r.message === 'function' ? r.message(l.value) : r.message,
severity: r.severity
};
if (!filter || filter(err)) {
result.push(err);
if (first) {
return result;
function addRule(path, r) {
var result = storeRules;
path.forEach(function (p, i) {
if (typeof p === 'number') {
p = '*'; // limitation: support only validation for each element of array
}
result[p] = result[p] || {};
result = result[p];
});
var existingRules = result[hidden];
var newRuleFunction = r.rule.toString();
if (existingRules) {
if (existingRules.has(newRuleFunction)) {
return;
}
existingRules.set(newRuleFunction, r);
return;
}
var newMap = new Map();
newMap.set(newRuleFunction, r);
result[hidden] = newMap;
}
}
if (depth === 1) {
return consistentResult();
}
if (nestedRulesKeys.length === 0) {
// console.log('getResults nested rules 0 length', result)
return consistentResult();
}
var nestedInst = l;
if (nestedInst.keys === undefined) {
// console.log('getResults no nested inst', result)
return consistentResult();
}
if (Array.isArray(nestedInst)) {
if (nestedRulesKeys.includes('*')) {
for (var i = 0; i < nestedInst.length; i += 1) {
var n = nestedInst[i];
result = result.concat(Validation(n)
.errors(filter, depth - 1, first));
if (first && result.length > 0) {
return result;
function getErrors(l, depth, filter, first) {
var result = [];
var consistentResult = function () { return result.length === 0 ? emptyErrors : result; };
if (depth === 0) {
return consistentResult();
}
var _a = getRulesAndNested(l.path), existingRules = _a[0], nestedRulesKeys = _a[1];
for (var i = 0; i < existingRules.length; i += 1) {
var r = existingRules[i];
if (!r.rule(l.value)) {
var err = {
path: l.path,
message: typeof r.message === 'function' ? r.message(l.value) : r.message,
severity: r.severity
};
if (!filter || filter(err)) {
result.push(err);
if (first) {
return result;
}
}
}
}
}
// validation for individual array elements is not supported, it is covered by foreach above
// for (let i = 0; i < nestedRulesKeys.length; i += 1) {
// const k = nestedRulesKeys[i];
// // Validation rule exists,
// // but the corresponding nested link may not be created,
// // (because it may not be inferred automatically)
// // because the original array value cas miss the corresponding index
// // The design choice is to skip validation in this case.
// // A client can define per array level validation rule,
// // where existance of the index can be cheched.
// if (nestedInst[k] !== undefined) {
// result = result.concat((nestedInst[k] as State<StateValueAtPath, ValidationExtensions>)
// .extended.errors(filter, depth - 1, first));
// if (first && result.length > 0) {
// return result;
// }
// }
// }
}
else {
for (var i = 0; i < nestedRulesKeys.length; i += 1) {
var k = nestedRulesKeys[i];
// Validation rule exists,
// but the corresponding nested link may not be created,
// (because it may not be inferred automatically)
// because the original array value cas miss the corresponding index
// The design choice is to skip validation in this case.
// A client can define per array level validation rule,
// where existance of the index can be cheched.
if (nestedInst[k] !== undefined) {
result = result.concat(Validation(nestedInst[k])
.errors(filter, depth - 1, first));
if (first && result.length > 0) {
return result;
if (depth === 1) {
return consistentResult();
}
if (nestedRulesKeys.length === 0) {
// console.log('getResults nested rules 0 length', result)
return consistentResult();
}
var nestedInst = l;
if (nestedInst.keys === undefined) {
// console.log('getResults no nested inst', result)
return consistentResult();
}
if (Array.isArray(nestedInst)) {
if (nestedRulesKeys.includes('*')) {
for (var i = 0; i < nestedInst.length; i += 1) {
var n = nestedInst[i];
result = result.concat(n.errors(filter, depth - 1, first));
if (first && result.length > 0) {
return result;
}
}
}
// validation for individual array elements is not supported, it is covered by foreach above
}
else {
for (var i = 0; i < nestedRulesKeys.length; i += 1) {
var k = nestedRulesKeys[i];
// Validation rule exists,
// but the corresponding nested link may not be created,
// (because it may not be inferred automatically)
// because the original array value cas miss the corresponding index
// The design choice is to skip validation in this case.
// A client can define per array level validation rule,
// where existance of the index can be cheched.
if (nestedInst[k] !== undefined) {
result = result.concat(nestedInst[k].errors(filter, depth - 1, first));
if (first && result.length > 0) {
return result;
}
}
}
}
return consistentResult();
}
}
return consistentResult();
};
return ValidationPluginInstance;
}());
function Validation($this) {
if ($this) {
var state_1 = $this;
var plugin = state_1.attach(PluginID)[0];
if (plugin instanceof Error) {
throw plugin;
}
var instance = plugin;
var inst_1 = instance;
return {
validate: function (r, m, s) {
inst_1.addRule(state_1.path, {
return {
validate: function (state) { return function (r, m, s) { return addRule(state.path, {
rule: r,
message: m,
severity: s || 'error'
});
},
validShallow: function () {
return inst_1.getErrors(state_1, 1, undefined, true).length === 0;
},
valid: function () {
return inst_1.getErrors(state_1, Number.MAX_SAFE_INTEGER, undefined, true).length === 0;
},
invalidShallow: function () {
return inst_1.getErrors(state_1, 1, undefined, true).length !== 0;
},
invalid: function () {
return inst_1.getErrors(state_1, Number.MAX_SAFE_INTEGER, undefined, true).length !== 0;
},
errors: function (filter, depth, first) {
return inst_1.getErrors(state_1, depth === undefined ? Number.MAX_SAFE_INTEGER : depth, filter, first);
},
firstError: function (filter, depth) {
var r = inst_1.getErrors(state_1, depth === undefined ? Number.MAX_SAFE_INTEGER : depth, filter, true);
if (r.length === 0) {
return {};
}
return r[0];
},
};
}
return {
id: PluginID,
init: function () { return new ValidationPluginInstance(); }
};
}); }; },
valid: function (s) { return function (options) { var _a; return getErrors(s, (_a = options === null || options === void 0 ? void 0 : options.depth) !== null && _a !== void 0 ? _a : Number.MAX_SAFE_INTEGER, undefined, true).length === 0; }; },
invalid: function (s) { return function (options) { var _a; return getErrors(s, (_a = options === null || options === void 0 ? void 0 : options.depth) !== null && _a !== void 0 ? _a : Number.MAX_SAFE_INTEGER, undefined, true).length !== 0; }; },
firstError: function (s) { return function (filter, depth) { return getErrors(s, depth !== null && depth !== void 0 ? depth : Number.MAX_SAFE_INTEGER, filter, true)[0]; }; },
errors: function (s) { return function (filter, depth, first) { return getErrors(s, depth !== null && depth !== void 0 ? depth : Number.MAX_SAFE_INTEGER, filter, first); }; },
};
}
}); };
}
export { Validation };
export { validation };
//# sourceMappingURL=index.es.js.map

@@ -5,177 +5,124 @@ 'use strict';

var PluginID = Symbol('Validate');
var emptyErrors = [];
var ValidationPluginInstance = /** @class */ (function () {
function ValidationPluginInstance() {
this.storeRules = {};
}
ValidationPluginInstance.prototype.getRulesAndNested = function (path) {
var result = this.storeRules;
path.forEach(function (p) {
if (typeof p === 'number') {
p = '*'; // limitation: support only validation for each element of array
function validation() {
return function () { return ({
onCreate: function () {
var storeRules = {};
var hidden = Symbol('hidden');
var emptyErrors = [];
function getRulesAndNested(path) {
var result = storeRules;
path.forEach(function (p) {
if (typeof p === 'number') {
p = '*'; // limitation: support only validation for each element of array
}
result = result && (result[p]);
});
return [result && result[hidden] ? Array.from(result[hidden].values()) : [],
result ? Object.keys(result) : []];
}
result = result && (result[p]);
});
return [result && result[PluginID] ? Array.from(result[PluginID].values()) : [],
result ? Object.keys(result) : []];
};
ValidationPluginInstance.prototype.addRule = function (path, r) {
var result = this.storeRules;
path.forEach(function (p, i) {
if (typeof p === 'number') {
p = '*'; // limitation: support only validation for each element of array
}
result[p] = result[p] || {};
result = result[p];
});
var existingRules = result[PluginID];
var newRuleFunction = r.rule.toString();
if (existingRules) {
if (existingRules.has(newRuleFunction)) {
return;
}
existingRules.set(newRuleFunction, r);
return;
}
var newMap = new Map();
newMap.set(newRuleFunction, r);
result[PluginID] = newMap;
};
ValidationPluginInstance.prototype.getErrors = function (l, depth, filter, first) {
var result = [];
var consistentResult = function () { return result.length === 0 ? emptyErrors : result; };
if (depth === 0) {
return consistentResult();
}
var _a = this.getRulesAndNested(l.path), existingRules = _a[0], nestedRulesKeys = _a[1];
for (var i = 0; i < existingRules.length; i += 1) {
var r = existingRules[i];
if (!r.rule(l.value)) {
var err = {
path: l.path,
message: typeof r.message === 'function' ? r.message(l.value) : r.message,
severity: r.severity
};
if (!filter || filter(err)) {
result.push(err);
if (first) {
return result;
function addRule(path, r) {
var result = storeRules;
path.forEach(function (p, i) {
if (typeof p === 'number') {
p = '*'; // limitation: support only validation for each element of array
}
result[p] = result[p] || {};
result = result[p];
});
var existingRules = result[hidden];
var newRuleFunction = r.rule.toString();
if (existingRules) {
if (existingRules.has(newRuleFunction)) {
return;
}
existingRules.set(newRuleFunction, r);
return;
}
var newMap = new Map();
newMap.set(newRuleFunction, r);
result[hidden] = newMap;
}
}
if (depth === 1) {
return consistentResult();
}
if (nestedRulesKeys.length === 0) {
// console.log('getResults nested rules 0 length', result)
return consistentResult();
}
var nestedInst = l;
if (nestedInst.keys === undefined) {
// console.log('getResults no nested inst', result)
return consistentResult();
}
if (Array.isArray(nestedInst)) {
if (nestedRulesKeys.includes('*')) {
for (var i = 0; i < nestedInst.length; i += 1) {
var n = nestedInst[i];
result = result.concat(Validation(n)
.errors(filter, depth - 1, first));
if (first && result.length > 0) {
return result;
function getErrors(l, depth, filter, first) {
var result = [];
var consistentResult = function () { return result.length === 0 ? emptyErrors : result; };
if (depth === 0) {
return consistentResult();
}
var _a = getRulesAndNested(l.path), existingRules = _a[0], nestedRulesKeys = _a[1];
for (var i = 0; i < existingRules.length; i += 1) {
var r = existingRules[i];
if (!r.rule(l.value)) {
var err = {
path: l.path,
message: typeof r.message === 'function' ? r.message(l.value) : r.message,
severity: r.severity
};
if (!filter || filter(err)) {
result.push(err);
if (first) {
return result;
}
}
}
}
}
// validation for individual array elements is not supported, it is covered by foreach above
// for (let i = 0; i < nestedRulesKeys.length; i += 1) {
// const k = nestedRulesKeys[i];
// // Validation rule exists,
// // but the corresponding nested link may not be created,
// // (because it may not be inferred automatically)
// // because the original array value cas miss the corresponding index
// // The design choice is to skip validation in this case.
// // A client can define per array level validation rule,
// // where existance of the index can be cheched.
// if (nestedInst[k] !== undefined) {
// result = result.concat((nestedInst[k] as State<StateValueAtPath, ValidationExtensions>)
// .extended.errors(filter, depth - 1, first));
// if (first && result.length > 0) {
// return result;
// }
// }
// }
}
else {
for (var i = 0; i < nestedRulesKeys.length; i += 1) {
var k = nestedRulesKeys[i];
// Validation rule exists,
// but the corresponding nested link may not be created,
// (because it may not be inferred automatically)
// because the original array value cas miss the corresponding index
// The design choice is to skip validation in this case.
// A client can define per array level validation rule,
// where existance of the index can be cheched.
if (nestedInst[k] !== undefined) {
result = result.concat(Validation(nestedInst[k])
.errors(filter, depth - 1, first));
if (first && result.length > 0) {
return result;
if (depth === 1) {
return consistentResult();
}
if (nestedRulesKeys.length === 0) {
// console.log('getResults nested rules 0 length', result)
return consistentResult();
}
var nestedInst = l;
if (nestedInst.keys === undefined) {
// console.log('getResults no nested inst', result)
return consistentResult();
}
if (Array.isArray(nestedInst)) {
if (nestedRulesKeys.includes('*')) {
for (var i = 0; i < nestedInst.length; i += 1) {
var n = nestedInst[i];
result = result.concat(n.errors(filter, depth - 1, first));
if (first && result.length > 0) {
return result;
}
}
}
// validation for individual array elements is not supported, it is covered by foreach above
}
else {
for (var i = 0; i < nestedRulesKeys.length; i += 1) {
var k = nestedRulesKeys[i];
// Validation rule exists,
// but the corresponding nested link may not be created,
// (because it may not be inferred automatically)
// because the original array value cas miss the corresponding index
// The design choice is to skip validation in this case.
// A client can define per array level validation rule,
// where existance of the index can be cheched.
if (nestedInst[k] !== undefined) {
result = result.concat(nestedInst[k].errors(filter, depth - 1, first));
if (first && result.length > 0) {
return result;
}
}
}
}
return consistentResult();
}
}
return consistentResult();
};
return ValidationPluginInstance;
}());
function Validation($this) {
if ($this) {
var state_1 = $this;
var plugin = state_1.attach(PluginID)[0];
if (plugin instanceof Error) {
throw plugin;
}
var instance = plugin;
var inst_1 = instance;
return {
validate: function (r, m, s) {
inst_1.addRule(state_1.path, {
return {
validate: function (state) { return function (r, m, s) { return addRule(state.path, {
rule: r,
message: m,
severity: s || 'error'
});
},
validShallow: function () {
return inst_1.getErrors(state_1, 1, undefined, true).length === 0;
},
valid: function () {
return inst_1.getErrors(state_1, Number.MAX_SAFE_INTEGER, undefined, true).length === 0;
},
invalidShallow: function () {
return inst_1.getErrors(state_1, 1, undefined, true).length !== 0;
},
invalid: function () {
return inst_1.getErrors(state_1, Number.MAX_SAFE_INTEGER, undefined, true).length !== 0;
},
errors: function (filter, depth, first) {
return inst_1.getErrors(state_1, depth === undefined ? Number.MAX_SAFE_INTEGER : depth, filter, first);
},
firstError: function (filter, depth) {
var r = inst_1.getErrors(state_1, depth === undefined ? Number.MAX_SAFE_INTEGER : depth, filter, true);
if (r.length === 0) {
return {};
}
return r[0];
},
};
}
return {
id: PluginID,
init: function () { return new ValidationPluginInstance(); }
};
}); }; },
valid: function (s) { return function (options) { var _a; return getErrors(s, (_a = options === null || options === void 0 ? void 0 : options.depth) !== null && _a !== void 0 ? _a : Number.MAX_SAFE_INTEGER, undefined, true).length === 0; }; },
invalid: function (s) { return function (options) { var _a; return getErrors(s, (_a = options === null || options === void 0 ? void 0 : options.depth) !== null && _a !== void 0 ? _a : Number.MAX_SAFE_INTEGER, undefined, true).length !== 0; }; },
firstError: function (s) { return function (filter, depth) { return getErrors(s, depth !== null && depth !== void 0 ? depth : Number.MAX_SAFE_INTEGER, filter, true)[0]; }; },
errors: function (s) { return function (filter, depth, first) { return getErrors(s, depth !== null && depth !== void 0 ? depth : Number.MAX_SAFE_INTEGER, filter, first); }; },
};
}
}); };
}
exports.Validation = Validation;
exports.validation = validation;
//# sourceMappingURL=index.js.map
{
"name": "@hookstate/validation",
"version": "3.0.1",
"version": "4.0.0-rc2",
"description": "Plugin for @hookstate/core to enable validation of data state.",

@@ -23,7 +23,7 @@ "license": "MIT",

"build:w": "rollup -c -w",
"build:docs": "typedoc --plugin typedoc-plugin-markdown --hideBreadcrumbs --tsconfig ./tsconfig.typedoc.json --exclude \"dist/**.js\" --gitRevision master --includeDeclarations --excludeExternals --excludeNotExported --categorizeByGroup false --readme none --hideGenerator --mode file --out dist/docs && concat-md --decrease-title-levels --dir-name-as-title dist/docs > dist/typedoc.md && rimraf dist/docs && replace '></a>' '/>' dist/typedoc.md && replace 'Ƭ [*][*]([A-Za-z0-9]+)[*][*]: [*](.*)[*]' 'Ƭ **$1**: *`$2`*' dist/typedoc.md && replace 'Ƭ [*][*]State[*][*]: [*](.*)[*]' 'Ƭ **State**: *[StateMixin](#interfacesstatemixinmd) & `S extends object` ? `{ readonly [K in keyof Required<S>]: State<S[K]> }` : [StateMethods](#interfacesstatemethodsmd)*' dist/typedoc.md && replace '[(]statemethods.md#\\[self\\][)]' '(#self)' dist/typedoc.md && replace '[(]statemixin.md#\\[self\\][)]' '(#self-1)' dist/typedoc.md && replace '[(]statemixindestroy.md#\\[self\\][)]' '(#self-2)' dist/typedoc.md && replace '# @hookstate/core' '' dist/typedoc.md && replace '<a name=\"readmemd\"/>' '\n---\nid: typedoc-hookstate-core\ntitle: API @hookstate/core\n---' dist/typedoc.md && replace '\n\n(---)' '$1' dist/typedoc.md && mv dist/typedoc.md ../docs/index/docs/typedoc-hookstate-core.md",
"clean": "rimraf dist",
"test": "cross-env CI=1 jest --env=jsdom",
"test": "jest --env=jsdom",
"test:ci": "jest --env=jsdom",
"test:w": "jest --env=jsdom --watch",
"release": "npm publish",
"release": "npm publish --access public",
"update:deps": "ncu -u"

@@ -33,31 +33,35 @@ },

"peerDependencies": {
"@hookstate/core": "^3.0.0 || ^4.0.0"
"@hookstate/core": "^4.0.0-rc17"
},
"devDependencies": {
"@babel/core": "7.18.2",
"@babel/runtime": "7.18.3",
"@babel/core": "7.18.6",
"@babel/runtime": "7.18.6",
"@hookstate/core": "workspace:*",
"@rollup/plugin-babel": "5.3.1",
"@rollup/plugin-commonjs": "22.0.0",
"@rollup/plugin-commonjs": "22.0.1",
"@rollup/plugin-node-resolve": "13.3.0",
"@rollup/plugin-url": "7.0.0",
"@testing-library/react": "13.2.0",
"@testing-library/react-hooks": "8.0.0",
"@types/jest": "27.5.1",
"@testing-library/react": "13.3.0",
"@testing-library/react-hooks": "8.0.1",
"@types/jest": "28.1.4",
"@types/lodash.clonedeep": "4.5.7",
"@types/lodash.isequal": "4.5.6",
"@types/node": "^17.0.35",
"@types/react": "18.0.9",
"@types/react-dom": "18.0.5",
"@types/node": "^18.0.3",
"@types/react": "18.0.15",
"@types/react-dom": "18.0.6",
"codecov": "3.8.3",
"coverage": "0.4.1",
"cross-env": "7.0.3",
"npm-check-updates": "13.0.3",
"react": "18.1.0",
"react-dom": "18.1.0",
"react-test-renderer": "18.1.0",
"jest": "28.1.2",
"jest-environment-jsdom": "28.1.2",
"npm-check-updates": "15.2.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-test-renderer": "18.2.0",
"rimraf": "3.0.2",
"rollup": "2.75.0",
"rollup": "2.76.0",
"rollup-plugin-peer-deps-external": "2.2.4",
"rollup-plugin-typescript2": "0.31.2",
"rollup-plugin-typescript2": "0.32.1",
"tslib": "^2.4.0",
"typescript": "4.7.2"
"typescript": "4.7.4"
},

@@ -64,0 +68,0 @@ "files": [

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc