@joystick.js/node-canary 0.0.0-canary.411 → 0.0.0-canary.412
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
import i from"../../lib/types.js";import m from"./validate_type.js";import
|
|
1
|
+
import i from"../../lib/types.js";import m from"./validate_type.js";import"./is_array_path.js";const l=(r,e,t)=>r.includes(e)?{valid:!0,errors:[]}:{valid:!1,errors:[`Field ${t} only allows the following values: ${r.join(", ")}.`]},n=(r,e,t)=>i.is_array(e)&&e?.length<=r?{valid:!0,errors:[]}:{valid:!1,errors:[`Field ${t} length must be less than or equal to ${r}.`]},d=(r,e,t)=>i.is_array(e)&&e?.length>=r?{valid:!0,errors:[]}:{valid:!1,errors:[`Field ${t} length must be greater than or equal to ${r}.`]},v={types:["any","array","boolean","float","integer","number","object","string"],rules:{allowed_values:l,allowedValues:l,element:async(r,e,t,s)=>{if(e&&(i.is_object(r)||i.is_string(r))&&i.is_array(e)){const a=await Promise.all(e.flatMap(async(o,f)=>(await s(o,i.is_string(r)?{type:r}:r,`${t}.${f}`)).flatMap(c=>c)));return{valid:a.length===0,errors:a.flatMap(o=>o)}}return{valid:!0,errors:[]}},fields:async(r,e,t,s)=>{if(i.is_object(r)&&i.is_object(e)){const a=await s(e,r,t);return{valid:a.length===0,errors:[...a]}}return{valid:!1,errors:[`Field ${t} must be of type object.`]}},max:(r,e,t)=>e<=r?{valid:!0,errors:[]}:{valid:!1,errors:[`Field ${t} must be less than or equal to ${r}.`]},max_length:n,maxLength:n,min:(r,e,t)=>e>=r?{valid:!0,errors:[]}:{valid:!1,errors:[`Field ${t} must be greater than or equal to ${r}.`]},min_length:d,minLength:d,optional:(r,e,t)=>(r===!1?!!e:!0)?{valid:!0,errors:[]}:{valid:!1,errors:[`Field ${t} is required.`]},regex:(r,e,t)=>new RegExp(r).test(e)?{valid:!0,errors:[]}:{valid:!1,errors:[`Field ${t} must conform to regex: ${r}.`]},required:(r,e,t)=>(r===!1?!0:!!e)?{valid:!0,errors:[]}:{valid:!1,errors:[`Field ${t} is required.`]},type:(r,e,t)=>{const s=m(r,e);return e&&!s?{valid:!1,errors:[`Field ${t} must be of type ${r}.`]}:{valid:!0,errors:[]}}}};var b=v;export{b as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import d from"./get_value_from_object.js";import y from"./input_validators.js";import b from"./is_array_path.js";import _ from"../../lib/types.js";const j=(t=null,r="",e=!1)=>{if(r&&!_.is_string(r))throw new Error("path must be passed as a string");if(e||!e&&!_.is_object(t))return t;if(r.includes(".$.")){const[o]=r.split(".$.");return d(t,o)}return d(t,r)},w=(t="")=>{if(!t)return"";if(t&&!_.is_string(t))throw new Error("array_path must be a type of string");return t.split(".").pop()},m=({queue:t,rules:r,input:e,path:o,types_only_schema:n})=>{r&&r.type==="object"&&(t=p(t,r.properties,e,o)),r&&r.type==="array"&&_.is_object(r.element)&&(t=p(t,r.element,e,`${o}.$`));const s=b(o)?w(o):o;return{path:o,rules:r,input_value:j(e,s,n)}},p=(t=[],r={},e={},o="")=>{const n=!!(r.type&&y.types.includes(r.type));if(!n){const s=Object.entries(r||{});for(let i=0;i<s?.length;i+=1){const[c,l]=s[i],a=m({queue:t,rules:l,input:e,path:c,types_only_schema:n});t=[...t,a]}}if(n){const s=o,i=m({queue:t,rules:r,input:e,path:field,types_only_schema:n});t=[...t,i]}return t},v=async(t={},r={},e="")=>{const o=[],n=p([],r,t,e);return await Promise.all(n.flatMap(s=>Object.entries(s.rules||{}).flatMap(async([i,c])=>{const l=y.rules[i];if(l&&!s.path.includes(".$.")){const a=await l(c,s.input_value,s.path,v);if(a&&!a.valid)for(let f=0;f<a.errors.length;f+=1){const g=a.errors[f];o.push(g)}}}))),o};var E=v;export{E as default,j as handle_get_input_value};
|
package/package.json
CHANGED
|
@@ -110,22 +110,7 @@ const input_validators = {
|
|
|
110
110
|
},
|
|
111
111
|
fields: async (rule_value, input_value, parent_path, validate_input) => {
|
|
112
112
|
if (types.is_object(rule_value) && types.is_object(input_value)) {
|
|
113
|
-
|
|
114
|
-
// object path. Based on the schema structure for the type "object", fields: {} rules,
|
|
115
|
-
// and how we run this all recursively, if we're dealing with an object path, we need to
|
|
116
|
-
// "tack back on" wrap the parent field around the input_value (child). This ensures that
|
|
117
|
-
// the value path actually exists when this runs recursively (and the validator works).
|
|
118
|
-
const input = is_array_path(parent_path)
|
|
119
|
-
? input_value
|
|
120
|
-
: input_value;
|
|
121
|
-
|
|
122
|
-
console.log('FIELDS', {
|
|
123
|
-
input,
|
|
124
|
-
rule_value,
|
|
125
|
-
parent_path,
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
const errors = await validate_input(input, rule_value, parent_path);
|
|
113
|
+
const errors = await validate_input(input_value, rule_value, parent_path);
|
|
129
114
|
|
|
130
115
|
return {
|
|
131
116
|
valid: errors.length === 0,
|
|
@@ -49,14 +49,6 @@ const add_validation_task = ({ queue, rules, input, path, types_only_schema }) =
|
|
|
49
49
|
|
|
50
50
|
const path_to_value = is_array_path(path) ? get_array_path_key(path) : path;
|
|
51
51
|
|
|
52
|
-
console.log({
|
|
53
|
-
path,
|
|
54
|
-
path_to_value,
|
|
55
|
-
input,
|
|
56
|
-
input_value: handle_get_input_value(input, path_to_value, types_only_schema),
|
|
57
|
-
rules,
|
|
58
|
-
})
|
|
59
|
-
|
|
60
52
|
return {
|
|
61
53
|
path,
|
|
62
54
|
rules,
|
|
@@ -81,9 +73,6 @@ const add_to_validation_queue = (
|
|
|
81
73
|
|
|
82
74
|
for (let i = 0; i < schema_definitions?.length; i += 1) {
|
|
83
75
|
const [field, rules] = schema_definitions[i];
|
|
84
|
-
console.log({ field, rules });
|
|
85
|
-
// TODO: Prefixing parent_path is WRONG under certain conditions. Which?
|
|
86
|
-
const path = `${parent_path ? `${parent_path}.${field}` : field}`;
|
|
87
76
|
const validation_task = add_validation_task({
|
|
88
77
|
queue,
|
|
89
78
|
rules,
|