@joystick.js/node-canary 0.0.0-canary.411 → 0.0.0-canary.413

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 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
+ 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 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[a,l]=e[i];console.log({field:a,rules:l});const c=`${o?`${o}.${a}`:a}`,_=g({queue:t,rules:l,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,a])=>{const l=v.rules[i];if(l&&!e.path.includes(".$.")){const c=await l(a,e.input_value,e.path,m);if(c&&!c.valid)for(let _=0;_<c.errors.length;_+=1){const b=c.errors[_];o.push(b)}}}))),o};var E=m;export{E as default,y as handle_get_input_value};
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 a=0;a<s?.length;a+=1){const[c,l]=s[a],i=m({queue:t,rules:l,input:e,path:c,types_only_schema:n});t=[...t,i]}}if(n){const s=m({queue:t,rules:r,input:e,path:o,types_only_schema:n});t=[...t,s]}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([a,c])=>{const l=y.rules[a];if(l&&!s.path.includes(".$.")){const i=await l(c,s.input_value,s.path,v);if(i&&!i.valid)for(let f=0;f<i.errors.length;f+=1){const g=i.errors[f];o.push(g)}}}))),o};var E=v;export{E as default,j as handle_get_input_value};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@joystick.js/node-canary",
3
3
  "type": "module",
4
- "version": "0.0.0-canary.411",
4
+ "version": "0.0.0-canary.413",
5
5
  "description": "The Node.js framework for Joystick.",
6
6
  "main": "./dist/index.js",
7
7
  "scripts": {
@@ -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
- // 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);
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,
@@ -97,12 +86,11 @@ const add_to_validation_queue = (
97
86
  }
98
87
 
99
88
  if (types_only_schema) {
100
- const path = parent_path;
101
89
  const validation_task = add_validation_task({
102
90
  queue,
103
91
  rules: schema,
104
92
  input,
105
- path: field,
93
+ path: parent_path,
106
94
  types_only_schema,
107
95
  });
108
96