@joystick.js/node-canary 0.0.0-canary.375 → 0.0.0-canary.376

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,2 +1,2 @@
1
1
  import p from"./format_api_error.js";import f from"./get_output.js";import h from"../../lib/get_sanitized_context.js";import z from"./sanitize_api_response.js";import d from"../../test/track_function_call.js";import n from"../../lib/types.js";import m from"./validate_input.js";const _=async(o="",t={},r=null,s=null,a={},u={})=>{const l=t?.sanitize===!1,i=(t?.sanitize||u?.sanitize)===!0;d(`node.api.getters.${o}`,[r,h(a)]);const e=await t?.get(r,a),c=s?f(e,s):e;return!l&&i?z(c):c},w=async(o="",t={},r={},s={})=>{d(`node.api.getters.${o}.authorized`,[r,h(s)]);const a=await t?.authorized(r,s);if(typeof a=="boolean")return a;if(n.is_object(a)&&!n.is_array(a)&&!n.is_undefined(a?.authorized))return a?.authorized?!0:a?.message},y=(o=[],t="")=>{console.log(`Input validation for getter "${t}" failed with the following errors:
2
- `);for(let r=0;r<o?.length;r+=1){const s=o[r];console.log(`${r+1}. ${s}`)}},$=(o={},t={})=>m(o,t),v=async({get_name:o,get_options:t,getter_definition:r,request_context:s,api_schema_options:a})=>{const u=r?.input&&Object.keys(r?.input)?.length>0,l=n.is_function(r?.authorized);if(u){const i=await $(t?.input,r?.input);if(i?.length>0)return y(i,o),Promise.reject({errors:i.map(e=>p(new Error(e),`getters.${o}.input`,401))})}if(l){const i=await w(o,r,t?.input,s);if(!i||n.is_string(i))return Promise.reject({errors:[p(new Error(typeof i=="string"?i:`Not authorized to access ${o}.`),`getters.${o}.authorized`,403)]})}if(n.is_function(r?.get)){const i=await _(o,r,t?.input,t?.output,s,a);return Promise.resolve(i)}};var N=v;export{N as default};
2
+ `);for(let r=0;r<o?.length;r+=1){const s=o[r];console.log(`${r+1}. ${s}`)}},$=(o={},t={})=>m(o,t),v=async({get_name:o,get_options:t,getter_definition:r,request_context:s,api_schema_options:a})=>{const u=r?.input&&Object.keys(r?.input)?.length>0,l=n.is_function(r?.authorized);if(u){const i=await $(t?.input,r?.input);if(i?.length>0)return y(i,o),Promise.reject({errors:i.map(e=>p(new Error(e),`getters.${o}.input`,400))})}if(l){const i=await w(o,r,t?.input,s);if(!i||n.is_string(i))return Promise.reject({errors:[p(new Error(typeof i=="string"?i:`Not authorized to access ${o}.`),`getters.${o}.authorized`,403)]})}if(n.is_function(r?.get)){const i=await _(o,r,t?.input,t?.output,s,a);return Promise.resolve(i)}};var N=v;export{N as default};
@@ -1 +1 @@
1
- import i from"../../lib/types.js";import d from"./validate_type.js";import f from"./is_array_path.js";const c={types:["any","array","boolean","float","integer","number","object","string"],rules:{allowedValues:(r,e,t)=>r.includes(e)?{valid:!0,errors:[]}:{valid:!1,errors:[`Field ${t} only allows the following values: ${r.join(", ")}.`]},element:async(r,e,t,s)=>{if(e&&(i.is_object(r)||i.is_string(r))&&i.is_array(e)){const o=await Promise.all(e.flatMap(async(a,l)=>(await s(a,i.is_string(r)?{type:r}:r,`${t}.${l}`)).flatMap(n=>n)));return{valid:o.length===0,errors:o.flatMap(a=>a)}}return{valid:!0,errors:[]}},fields:async(r,e,t,s)=>{if(i.is_object(r)&&i.is_object(e)){const o=f(t)?e:{[t]:e},a=await s(o,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}.`]},maxLength:(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}.`]},min:(r,e,t)=>e>=r?{valid:!0,errors:[]}:{valid:!1,errors:[`Field ${t} must be greater than or equal to ${r}.`]},minLength:(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}.`]},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=d(r,e);return e&&!s?{valid:!1,errors:[`Field ${t} must be of type ${r}.`]}:{valid:!0,errors:[]}}}};var g=c;export{g as default};
1
+ import i from"../../lib/types.js";import m from"./validate_type.js";import v 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}.`]},g={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=v(t)?e:{[t]:e},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=g;export{h as default};
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.375",
4
+ "version": "0.0.0-canary.376",
5
5
  "description": "The Node.js framework for Joystick.",
6
6
  "main": "./dist/index.js",
7
7
  "scripts": {
@@ -80,7 +80,7 @@ const get = async ({
80
80
 
81
81
  return Promise.reject({
82
82
  errors: validation_errors.map((error) => {
83
- return format_api_error(new Error(error), `getters.${get_name}.input`, 401);
83
+ return format_api_error(new Error(error), `getters.${get_name}.input`, 400);
84
84
  }),
85
85
  });
86
86
  }
@@ -2,6 +2,62 @@ import types from '../../lib/types.js';
2
2
  import validate_type from "./validate_type.js";
3
3
  import is_array_path from "./is_array_path.js";
4
4
 
5
+ const allowed_values = (rule_value, input_value, parent_path) => {
6
+ const isValid = rule_value.includes(input_value);
7
+
8
+ if (!isValid) {
9
+ return {
10
+ valid: false,
11
+ errors: [
12
+ `Field ${parent_path} only allows the following values: ${rule_value.join(
13
+ ", "
14
+ )}.`,
15
+ ],
16
+ };
17
+ }
18
+
19
+ return {
20
+ valid: true,
21
+ errors: [],
22
+ };
23
+ };
24
+
25
+ const max_length = (rule_value, input_value, parent_path) => {
26
+ const valid = types.is_array(input_value) && input_value?.length <= rule_value;
27
+
28
+ if (!valid) {
29
+ return {
30
+ valid: false,
31
+ errors: [
32
+ `Field ${parent_path} length must be less than or equal to ${rule_value}.`,
33
+ ],
34
+ };
35
+ }
36
+
37
+ return {
38
+ valid: true,
39
+ errors: [],
40
+ };
41
+ };
42
+
43
+ const min_length = (rule_value, input_value, parent_path) => {
44
+ const valid = types.is_array(input_value) && input_value?.length >= rule_value;
45
+
46
+ if (!valid) {
47
+ return {
48
+ valid: false,
49
+ errors: [
50
+ `Field ${parent_path} length must be greater than or equal to ${rule_value}.`,
51
+ ],
52
+ };
53
+ }
54
+
55
+ return {
56
+ valid: true,
57
+ errors: [],
58
+ };
59
+ };
60
+
5
61
  const input_validators = {
6
62
  types: [
7
63
  "any",
@@ -14,25 +70,8 @@ const input_validators = {
14
70
  "string",
15
71
  ],
16
72
  rules: {
17
- allowedValues: (rule_value, input_value, parent_path) => {
18
- const isValid = rule_value.includes(input_value);
19
-
20
- if (!isValid) {
21
- return {
22
- valid: false,
23
- errors: [
24
- `Field ${parent_path} only allows the following values: ${rule_value.join(
25
- ", "
26
- )}.`,
27
- ],
28
- };
29
- }
30
-
31
- return {
32
- valid: true,
33
- errors: [],
34
- };
35
- },
73
+ allowed_values,
74
+ allowedValues: allowed_values,
36
75
  element: async (rule_value, input_value, parent_path, validate_input) => {
37
76
  // NOTE: Allow for element to be passed as a type string so you can do arrays of single
38
77
  // primitive elements (e.g., ['a', 'b', 'c'] or [1, 2, 3]).
@@ -110,23 +149,8 @@ const input_validators = {
110
149
  errors: [],
111
150
  };
112
151
  },
113
- maxLength: (rule_value, input_value, parent_path) => {
114
- const valid = types.is_array(input_value) && input_value?.length <= rule_value;
115
-
116
- if (!valid) {
117
- return {
118
- valid: false,
119
- errors: [
120
- `Field ${parent_path} length must be less than or equal to ${rule_value}.`,
121
- ],
122
- };
123
- }
124
-
125
- return {
126
- valid: true,
127
- errors: [],
128
- };
129
- },
152
+ max_length,
153
+ maxLength: max_length,
130
154
  min: (rule_value, input_value, parent_path) => {
131
155
  const valid = input_value >= rule_value;
132
156
 
@@ -144,23 +168,8 @@ const input_validators = {
144
168
  errors: [],
145
169
  };
146
170
  },
147
- minLength: (rule_value, input_value, parent_path) => {
148
- const valid = types.is_array(input_value) && input_value?.length >= rule_value;
149
-
150
- if (!valid) {
151
- return {
152
- valid: false,
153
- errors: [
154
- `Field ${parent_path} length must be greater than or equal to ${rule_value}.`,
155
- ],
156
- };
157
- }
158
-
159
- return {
160
- valid: true,
161
- errors: [],
162
- };
163
- },
171
+ min_length,
172
+ minLength: min_length,
164
173
  optional: (rule_value, input_value, parent_path) => {
165
174
  const valid = rule_value === false ? !!input_value : true;
166
175