Socket
Socket
Sign inDemoInstall

@hookstate/validation

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hookstate/validation - npm Package Compare versions

Comparing version 0.10.4 to 1.0.0-alpha.0

315

dist/index.es.js

@@ -8,165 +8,184 @@ var ValidationSeverity;

var emptyErrors = [];
function Validation(attachRule, message, severity) {
return function () {
var storeRules = {};
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[PluginID] ? Array.from(result[PluginID].values()) : [],
result ? Object.keys(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[PluginID];
var newRuleFunction = r.rule.toString();
if (existingRules) {
if (existingRules.has(newRuleFunction)) {
return;
}
existingRules.set(newRuleFunction, r);
var ValidationPluginInstance = /** @class */ (function () {
function ValidationPluginInstance(attachRule, message, severity) {
this.attachRule = attachRule;
this.message = message;
this.severity = severity;
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
}
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;
}
var newMap = new Map();
newMap.set(newRuleFunction, r);
result[PluginID] = newMap;
existingRules.set(newRuleFunction, r);
return;
}
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;
}
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;
}
}
}
if (depth === 1) {
return consistentResult();
}
if (nestedRulesKeys.length === 0) {
// console.log('getResults nested rules 0 length', result)
return consistentResult();
}
var nestedInst = l.nested;
if (nestedInst === 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
.extended.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.nested;
if (nestedInst === 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;
}
}
// 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 StateLink<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(nestedInst[k]
.extended.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
// 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 StateLink<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;
}
}
}
return consistentResult();
}
return consistentResult();
};
Object.defineProperty(ValidationPluginInstance.prototype, "config", {
get: function () {
if (this.attachRule !== undefined && this.message !== undefined) {
return {
rule: this.attachRule,
message: this.message,
severity: this.severity || ValidationSeverity.ERROR
};
}
return undefined;
},
enumerable: true,
configurable: true
});
ValidationPluginInstance.prototype.onAttach = function (path, withArgument) {
var config = withArgument.config;
if (config) {
this.addRule(path, config);
}
};
return ValidationPluginInstance;
}());
function Validation(attachRuleOrSelf, message, severity) {
if (attachRuleOrSelf && typeof attachRuleOrSelf !== 'function') {
var self_1 = attachRuleOrSelf;
var _a = self_1.with(PluginID), l_1 = _a[0], instance = _a[1];
var inst_1 = instance;
return {
validShallow: function () {
return inst_1.getErrors(l_1, 1, undefined, true).length === 0;
},
valid: function () {
return inst_1.getErrors(l_1, Number.MAX_SAFE_INTEGER, undefined, true).length === 0;
},
invalidShallow: function () {
return inst_1.getErrors(l_1, 1, undefined, true).length !== 0;
},
invalid: function () {
return inst_1.getErrors(l_1, Number.MAX_SAFE_INTEGER, undefined, true).length !== 0;
},
errors: function (filter, depth, first) {
return inst_1.getErrors(l_1, depth === undefined ? Number.MAX_SAFE_INTEGER : depth, filter, first);
},
firstError: function (filter, depth) {
var r = inst_1.getErrors(l_1, depth === undefined ? Number.MAX_SAFE_INTEGER : depth, filter, true);
if (r.length === 0) {
return {};
}
return r[0];
},
};
}
return function () {
return {
id: PluginID,
instanceFactory: function () { return ({
get config() {
if (attachRule !== undefined && message !== undefined) {
return { rule: attachRule, message: message, severity: severity || ValidationSeverity.ERROR };
}
return undefined;
},
onAttach: function (path, plugin) {
var config = plugin.config;
if (config) {
addRule(path, config);
}
},
extensions: ['valid', 'validShallow', 'invalid', 'invalidShallow', 'errors', 'firstError'],
extensionsFactory: function (l) { return ({
get validShallow() {
return getErrors(l, 1, undefined, true).length === 0;
},
get valid() {
return getErrors(l, Number.MAX_SAFE_INTEGER, undefined, true).length === 0;
},
get invalidShallow() {
return getErrors(l, 1, undefined, true).length !== 0;
},
get invalid() {
return getErrors(l, Number.MAX_SAFE_INTEGER, undefined, true).length !== 0;
},
errors: function (filter, depth, first) {
return getErrors(l, depth === undefined ? Number.MAX_SAFE_INTEGER : depth, filter, first);
},
firstError: function (filter, depth) {
var r = getErrors(l, depth === undefined ? Number.MAX_SAFE_INTEGER : depth, filter, true);
if (r.length === 0) {
return {};
}
return r[0];
},
}); }
}); }
instanceFactory: function () { return new ValidationPluginInstance(attachRuleOrSelf, message, severity); }
};

@@ -173,0 +192,0 @@ };

@@ -11,165 +11,184 @@ 'use strict';

var emptyErrors = [];
function Validation(attachRule, message, severity) {
return function () {
var storeRules = {};
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[PluginID] ? Array.from(result[PluginID].values()) : [],
result ? Object.keys(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[PluginID];
var newRuleFunction = r.rule.toString();
if (existingRules) {
if (existingRules.has(newRuleFunction)) {
return;
}
existingRules.set(newRuleFunction, r);
var ValidationPluginInstance = /** @class */ (function () {
function ValidationPluginInstance(attachRule, message, severity) {
this.attachRule = attachRule;
this.message = message;
this.severity = severity;
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
}
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;
}
var newMap = new Map();
newMap.set(newRuleFunction, r);
result[PluginID] = newMap;
existingRules.set(newRuleFunction, r);
return;
}
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;
}
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;
}
}
}
if (depth === 1) {
return consistentResult();
}
if (nestedRulesKeys.length === 0) {
// console.log('getResults nested rules 0 length', result)
return consistentResult();
}
var nestedInst = l.nested;
if (nestedInst === 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
.extended.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.nested;
if (nestedInst === 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;
}
}
// 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 StateLink<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(nestedInst[k]
.extended.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
// 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 StateLink<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;
}
}
}
return consistentResult();
}
return consistentResult();
};
Object.defineProperty(ValidationPluginInstance.prototype, "config", {
get: function () {
if (this.attachRule !== undefined && this.message !== undefined) {
return {
rule: this.attachRule,
message: this.message,
severity: this.severity || exports.ValidationSeverity.ERROR
};
}
return undefined;
},
enumerable: true,
configurable: true
});
ValidationPluginInstance.prototype.onAttach = function (path, withArgument) {
var config = withArgument.config;
if (config) {
this.addRule(path, config);
}
};
return ValidationPluginInstance;
}());
function Validation(attachRuleOrSelf, message, severity) {
if (attachRuleOrSelf && typeof attachRuleOrSelf !== 'function') {
var self_1 = attachRuleOrSelf;
var _a = self_1.with(PluginID), l_1 = _a[0], instance = _a[1];
var inst_1 = instance;
return {
validShallow: function () {
return inst_1.getErrors(l_1, 1, undefined, true).length === 0;
},
valid: function () {
return inst_1.getErrors(l_1, Number.MAX_SAFE_INTEGER, undefined, true).length === 0;
},
invalidShallow: function () {
return inst_1.getErrors(l_1, 1, undefined, true).length !== 0;
},
invalid: function () {
return inst_1.getErrors(l_1, Number.MAX_SAFE_INTEGER, undefined, true).length !== 0;
},
errors: function (filter, depth, first) {
return inst_1.getErrors(l_1, depth === undefined ? Number.MAX_SAFE_INTEGER : depth, filter, first);
},
firstError: function (filter, depth) {
var r = inst_1.getErrors(l_1, depth === undefined ? Number.MAX_SAFE_INTEGER : depth, filter, true);
if (r.length === 0) {
return {};
}
return r[0];
},
};
}
return function () {
return {
id: PluginID,
instanceFactory: function () { return ({
get config() {
if (attachRule !== undefined && message !== undefined) {
return { rule: attachRule, message: message, severity: severity || exports.ValidationSeverity.ERROR };
}
return undefined;
},
onAttach: function (path, plugin) {
var config = plugin.config;
if (config) {
addRule(path, config);
}
},
extensions: ['valid', 'validShallow', 'invalid', 'invalidShallow', 'errors', 'firstError'],
extensionsFactory: function (l) { return ({
get validShallow() {
return getErrors(l, 1, undefined, true).length === 0;
},
get valid() {
return getErrors(l, Number.MAX_SAFE_INTEGER, undefined, true).length === 0;
},
get invalidShallow() {
return getErrors(l, 1, undefined, true).length !== 0;
},
get invalid() {
return getErrors(l, Number.MAX_SAFE_INTEGER, undefined, true).length !== 0;
},
errors: function (filter, depth, first) {
return getErrors(l, depth === undefined ? Number.MAX_SAFE_INTEGER : depth, filter, first);
},
firstError: function (filter, depth) {
var r = getErrors(l, depth === undefined ? Number.MAX_SAFE_INTEGER : depth, filter, true);
if (r.length === 0) {
return {};
}
return r[0];
},
}); }
}); }
instanceFactory: function () { return new ValidationPluginInstance(attachRuleOrSelf, message, severity); }
};

@@ -176,0 +195,0 @@ };

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

import { Plugin, PluginTypeMarker, Path, StateValueAtPath } from '@hookstate/core';
import { Plugin, Path, StateLink, StateValueAtPath } from '@hookstate/core';
export declare enum ValidationSeverity {

@@ -17,11 +17,12 @@ WARNING = 1,

export interface ValidationExtensions {
readonly validShallow: boolean;
readonly valid: boolean;
readonly invalidShallow: boolean;
readonly invalid: boolean;
readonly validShallow: () => boolean;
readonly valid: () => boolean;
readonly invalidShallow: () => boolean;
readonly invalid: () => boolean;
firstError(filter?: (e: ValidationError) => boolean, depth?: number): Partial<ValidationError>;
errors(filter?: (e: ValidationError) => boolean, depth?: number, first?: boolean): ReadonlyArray<ValidationError>;
}
export declare function Validation<S, E extends {}>(): ((unused: PluginTypeMarker<S, E>) => Plugin<E, ValidationExtensions>);
export declare function Validation<S, E extends {}>(attachRule: (value: S) => boolean, message: string | ((value: S) => string)): ((unused: PluginTypeMarker<S, E>) => Plugin<E, ValidationExtensions>);
export declare function Validation<S, E extends {}>(attachRule: (value: S) => boolean, message: string | ((value: S) => string), severity: ValidationSeverity): ((unused: PluginTypeMarker<S, E>) => Plugin<E, ValidationExtensions>);
export declare function Validation(): (() => Plugin);
export declare function Validation<S>(attachRule: (value: S) => boolean, message: string | ((value: S) => string)): (() => Plugin);
export declare function Validation<S>(attachRule: (value: S) => boolean, message: string | ((value: S) => string), severity: ValidationSeverity): (() => Plugin);
export declare function Validation<S>(self: StateLink<S>): ValidationExtensions;
{
"name": "@hookstate/validation",
"version": "0.10.4",
"version": "1.0.0-alpha.0",
"description": "Plugin for @hookstate/core to enable validation of data state.",

@@ -37,6 +37,6 @@ "license": "MIT",

"peerDependencies": {
"@hookstate/core": "^0.10.0"
"@hookstate/core": "1.0.0-alpha.0"
},
"devDependencies": {
"@hookstate/core": "^0.10.0",
"@hookstate/core": "1.0.0-alpha.0",
"@testing-library/react": "8.0.5",

@@ -43,0 +43,0 @@ "@testing-library/react-hooks": "1.1.0",

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc