@joystick.js/node-canary 0.0.0-canary.409 → 0.0.0-canary.410
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"./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(
|
|
1
|
+
import i from"../../lib/types.js";import m from"./validate_type.js";import g from"./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=(g(t),e);console.log("FIELDS",{input:a,rule_value:r,parent_path:t});const o=await s(a,r,t);return{valid:o.length===0,errors:[...o]}}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 h=v;export{h as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import p from"./get_value_from_object.js";import v from"./input_validators.js";import $ from"./is_array_path.js";import f from"../../lib/types.js";const y=(t=null,r="",s=!1)=>{if(r&&!f.is_string(r))throw new Error("path must be passed as a string");if(s||!s&&!f.is_object(t))return t;if(r.includes(".$.")){const[o]=r.split(".$.");return p(t,o)}return p(t,r)},j=(t="")=>{if(!t)return"";if(t&&!f.is_string(t))throw new Error("array_path must be a type of string");return t.split(".").pop()},g=({queue:t,rules:r,input:s,path:o,types_only_schema:n})=>{r&&r.type==="object"&&(t=d(t,r.properties,s,o)),r&&r.type==="array"&&f.is_object(r.element)&&(t=d(t,r.element,s,`${o}.$`));const e=$(o)?j(o):o;return console.log({path:o,path_to_value:e,input:s,input_value:y(s,e,n),rules:r}),{path:o,rules:r,input_value:y(s,e,n)}},d=(t=[],r={},s={},o="")=>{const n=!!(r.type&&v.types.includes(r.type));if(!n){const e=Object.entries(r||{});for(let i=0;i<e?.length;i+=1){const[l,c]=e[i];console.log({field:l,rules:c});const a=`${o?`${o}.${l}`:l}`,_=g({queue:t,rules:c,input:s,path:a,types_only_schema:n});t=[...t,_]}}if(n){const e=o,i=g({queue:t,rules:r,input:s,path:field,types_only_schema:n});t=[...t,i]}return t},m=async(t={},r={},s="")=>{const o=[],n=d([],r,t,s);return await Promise.all(n.flatMap(e=>Object.entries(e.rules||{}).flatMap(async([i,l])=>{const c=v.rules[i];if(c&&!e.path.includes(".$.")){const a=await c(l,e.input_value,e.path,m);if(a&&!a.valid)for(let _=0;_<a.errors.length;_+=1){const b=a.errors[_];o.push(b)}}}))),o};var M=m;export{M as default,y as handle_get_input_value};
|
package/package.json
CHANGED
|
@@ -110,7 +110,22 @@ 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
|
-
|
|
113
|
+
// NOTE: Complex. If we're NOT dealing with an array path, we're dealing with a normal
|
|
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);
|
|
114
129
|
|
|
115
130
|
return {
|
|
116
131
|
valid: errors.length === 0,
|
|
@@ -49,6 +49,14 @@ 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
|
+
|
|
52
60
|
return {
|
|
53
61
|
path,
|
|
54
62
|
rules,
|
|
@@ -73,11 +81,14 @@ const add_to_validation_queue = (
|
|
|
73
81
|
|
|
74
82
|
for (let i = 0; i < schema_definitions?.length; i += 1) {
|
|
75
83
|
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}`;
|
|
76
87
|
const validation_task = add_validation_task({
|
|
77
88
|
queue,
|
|
78
89
|
rules,
|
|
79
90
|
input,
|
|
80
|
-
path
|
|
91
|
+
path,
|
|
81
92
|
types_only_schema,
|
|
82
93
|
});
|
|
83
94
|
|
|
@@ -86,11 +97,12 @@ const add_to_validation_queue = (
|
|
|
86
97
|
}
|
|
87
98
|
|
|
88
99
|
if (types_only_schema) {
|
|
100
|
+
const path = parent_path;
|
|
89
101
|
const validation_task = add_validation_task({
|
|
90
102
|
queue,
|
|
91
103
|
rules: schema,
|
|
92
104
|
input,
|
|
93
|
-
path:
|
|
105
|
+
path: field,
|
|
94
106
|
types_only_schema,
|
|
95
107
|
});
|
|
96
108
|
|
|
@@ -101,6 +113,8 @@ const add_to_validation_queue = (
|
|
|
101
113
|
};
|
|
102
114
|
|
|
103
115
|
const validate_input = async (input = {}, schema = {}, parent_path = '') => {
|
|
116
|
+
// console.log(input);
|
|
117
|
+
|
|
104
118
|
const validation_errors = [];
|
|
105
119
|
const queue = add_to_validation_queue([], schema, input, parent_path);
|
|
106
120
|
|