@sot1986/appsync-precognition 0.2.2 → 0.2.4
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.
- package/dist/index.js +1 -298
- package/dist/index.js.map +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +1 -41
- package/dist/utils.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,298 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { runtime, util } from "@aws-appsync/utils";
|
|
3
|
-
|
|
4
|
-
//#region src/rules.ts
|
|
5
|
-
function parse(value, rule) {
|
|
6
|
-
const [name, ...params] = typeof rule === "string" ? [rule, void 0] : [rule[0], ...rule.slice(1)];
|
|
7
|
-
switch (name) {
|
|
8
|
-
case "required": return requiredRule(value);
|
|
9
|
-
case "nullable": return nullableRule(value);
|
|
10
|
-
case "sometimes": return sometimesRule(value);
|
|
11
|
-
case "min": return minRule(value, params[0]);
|
|
12
|
-
case "max": return maxRule(value, params[0]);
|
|
13
|
-
case "between": return betweenRule(value, params[0], params[1]);
|
|
14
|
-
case "regex": return regexRule(value, ...params);
|
|
15
|
-
case "in": return inRule(value, ...params);
|
|
16
|
-
case "notIn": return notInRule(value, ...params);
|
|
17
|
-
case "array": return arrayRule(value);
|
|
18
|
-
case "object": return objectRule(value);
|
|
19
|
-
case "boolean": return booleanRule(value);
|
|
20
|
-
case "number": return numberRule(value);
|
|
21
|
-
case "string": return stringRule(value);
|
|
22
|
-
case "before": return beforeRule(value, params[0]);
|
|
23
|
-
case "after": return afterRule(value, params[0]);
|
|
24
|
-
case "beforeOrEqual": return beforeOrEqualRule(value, params[0]);
|
|
25
|
-
case "afterOrEqual": return afterOrEqualRule(value, params[0]);
|
|
26
|
-
default: return {
|
|
27
|
-
check: false,
|
|
28
|
-
message: `Unknown rule ${name}`,
|
|
29
|
-
value
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
function minRule(value, minValue) {
|
|
34
|
-
const result = {
|
|
35
|
-
check: false,
|
|
36
|
-
message: `:attribute must be greater than or equal to ${minValue}`,
|
|
37
|
-
value
|
|
38
|
-
};
|
|
39
|
-
if (typeof value === "number") result.check = value >= minValue;
|
|
40
|
-
if (typeof value === "string") result.check = value.length >= minValue;
|
|
41
|
-
if (isArray(value)) {
|
|
42
|
-
result.check = value.length >= minValue;
|
|
43
|
-
result.message = `Array must contain at least ${minValue} elements`;
|
|
44
|
-
}
|
|
45
|
-
return result;
|
|
46
|
-
}
|
|
47
|
-
function maxRule(value, maxValue) {
|
|
48
|
-
const result = {
|
|
49
|
-
check: false,
|
|
50
|
-
message: `:attribute must be less than or equal to ${maxValue}`,
|
|
51
|
-
value
|
|
52
|
-
};
|
|
53
|
-
if (typeof value === "number") result.check = value <= maxValue;
|
|
54
|
-
if (typeof value === "string") {
|
|
55
|
-
result.check = value.length <= maxValue;
|
|
56
|
-
result.message = `String must contain at most ${maxValue} characters`;
|
|
57
|
-
}
|
|
58
|
-
if (isArray(value)) {
|
|
59
|
-
result.check = value.length <= maxValue;
|
|
60
|
-
result.message = `Array must contain at most ${maxValue} elements`;
|
|
61
|
-
}
|
|
62
|
-
return result;
|
|
63
|
-
}
|
|
64
|
-
function betweenRule(value, minValue, maxValue) {
|
|
65
|
-
const result = {
|
|
66
|
-
check: false,
|
|
67
|
-
message: `:attribute must be between ${minValue} and ${maxValue}`,
|
|
68
|
-
value
|
|
69
|
-
};
|
|
70
|
-
if (typeof value === "number") result.check = value >= minValue && value <= maxValue;
|
|
71
|
-
if (typeof value === "string") {
|
|
72
|
-
result.check = value.length >= minValue && value.length <= maxValue;
|
|
73
|
-
result.message = `String must contain between ${minValue} and ${maxValue} characters`;
|
|
74
|
-
}
|
|
75
|
-
if (isArray(value)) {
|
|
76
|
-
result.check = value.length >= minValue && value.length <= maxValue;
|
|
77
|
-
result.message = `Array must contain between ${minValue} and ${maxValue} elements`;
|
|
78
|
-
}
|
|
79
|
-
return result;
|
|
80
|
-
}
|
|
81
|
-
function regexRule(value, ...patterns) {
|
|
82
|
-
const result = {
|
|
83
|
-
check: false,
|
|
84
|
-
message: ":attribute must match the specified regular expression",
|
|
85
|
-
value
|
|
86
|
-
};
|
|
87
|
-
if (typeof value === "string") result.check = patterns.some((pattern) => util.matches(pattern, value));
|
|
88
|
-
return result;
|
|
89
|
-
}
|
|
90
|
-
function inRule(value, ...params) {
|
|
91
|
-
return {
|
|
92
|
-
check: params.includes(value),
|
|
93
|
-
message: ":attribute must be one of the specified values",
|
|
94
|
-
value
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
function notInRule(value, ...params) {
|
|
98
|
-
return {
|
|
99
|
-
check: !params.includes(value),
|
|
100
|
-
message: ":attribute must not be one of the specified values",
|
|
101
|
-
value
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
function requiredRule(value) {
|
|
105
|
-
const result = {
|
|
106
|
-
check: true,
|
|
107
|
-
message: ":attribute is required",
|
|
108
|
-
value,
|
|
109
|
-
skipNext: true
|
|
110
|
-
};
|
|
111
|
-
if (typeof value === "string") result.check = value.length > 0;
|
|
112
|
-
if (isArray(value)) result.check = value.length > 0;
|
|
113
|
-
if (typeof value === "number") result.check = true;
|
|
114
|
-
if (typeof value === "boolean") result.check = true;
|
|
115
|
-
if (typeof value === "object" && !result.value) {
|
|
116
|
-
result.message = ":attribute is not nullable";
|
|
117
|
-
result.check = false;
|
|
118
|
-
}
|
|
119
|
-
if (typeof value === "undefined") result.check = false;
|
|
120
|
-
result.skipNext = !result.check;
|
|
121
|
-
return result;
|
|
122
|
-
}
|
|
123
|
-
function nullableRule(value) {
|
|
124
|
-
return {
|
|
125
|
-
check: true,
|
|
126
|
-
message: "",
|
|
127
|
-
value,
|
|
128
|
-
skipNext: typeof value === "object" && !value
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
function sometimesRule(value) {
|
|
132
|
-
const result = {
|
|
133
|
-
check: true,
|
|
134
|
-
message: "",
|
|
135
|
-
value
|
|
136
|
-
};
|
|
137
|
-
if (typeof value === "undefined") {
|
|
138
|
-
result.skipNext = true;
|
|
139
|
-
return result;
|
|
140
|
-
}
|
|
141
|
-
if (typeof value === "object" && !result.value) {
|
|
142
|
-
result.message = ":attribute is not nullable";
|
|
143
|
-
result.check = false;
|
|
144
|
-
result.skipNext = true;
|
|
145
|
-
return result;
|
|
146
|
-
}
|
|
147
|
-
return requiredRule(value);
|
|
148
|
-
}
|
|
149
|
-
function arrayRule(value) {
|
|
150
|
-
const result = {
|
|
151
|
-
check: false,
|
|
152
|
-
message: ":attribute must be an array",
|
|
153
|
-
value
|
|
154
|
-
};
|
|
155
|
-
if (isArray(value)) result.check = true;
|
|
156
|
-
return result;
|
|
157
|
-
}
|
|
158
|
-
function objectRule(value) {
|
|
159
|
-
const result = {
|
|
160
|
-
check: false,
|
|
161
|
-
message: ":attribute must be an object",
|
|
162
|
-
value
|
|
163
|
-
};
|
|
164
|
-
if (typeof value === "object" && !isArray(result.value)) result.check = true;
|
|
165
|
-
return result;
|
|
166
|
-
}
|
|
167
|
-
function booleanRule(value) {
|
|
168
|
-
const result = {
|
|
169
|
-
check: false,
|
|
170
|
-
message: ":attribute must be a boolean",
|
|
171
|
-
value
|
|
172
|
-
};
|
|
173
|
-
if (typeof value === "boolean") result.check = true;
|
|
174
|
-
return result;
|
|
175
|
-
}
|
|
176
|
-
function numberRule(value) {
|
|
177
|
-
const result = {
|
|
178
|
-
check: false,
|
|
179
|
-
message: ":attribute must be a number",
|
|
180
|
-
value
|
|
181
|
-
};
|
|
182
|
-
if (typeof value === "number") result.check = true;
|
|
183
|
-
return result;
|
|
184
|
-
}
|
|
185
|
-
function stringRule(value) {
|
|
186
|
-
const result = {
|
|
187
|
-
check: false,
|
|
188
|
-
message: ":attribute must be a string",
|
|
189
|
-
value
|
|
190
|
-
};
|
|
191
|
-
if (typeof value === "string") result.check = true;
|
|
192
|
-
return result;
|
|
193
|
-
}
|
|
194
|
-
function beforeRule(value, start) {
|
|
195
|
-
const result = {
|
|
196
|
-
check: false,
|
|
197
|
-
message: `:attribute must be before ${start}`,
|
|
198
|
-
value
|
|
199
|
-
};
|
|
200
|
-
const startValue = util.time.parseISO8601ToEpochMilliSeconds(start);
|
|
201
|
-
if (typeof value === "string") result.check = util.time.parseISO8601ToEpochMilliSeconds(value) < startValue;
|
|
202
|
-
if (typeof value === "number") result.check = value < startValue;
|
|
203
|
-
return result;
|
|
204
|
-
}
|
|
205
|
-
function afterRule(value, start) {
|
|
206
|
-
const result = {
|
|
207
|
-
check: false,
|
|
208
|
-
message: `:attribute must be after ${start}`,
|
|
209
|
-
value
|
|
210
|
-
};
|
|
211
|
-
const startValue = util.time.parseISO8601ToEpochMilliSeconds(start);
|
|
212
|
-
if (typeof value === "string") result.check = util.time.parseISO8601ToEpochMilliSeconds(value) > startValue;
|
|
213
|
-
if (typeof value === "number") result.check = value > startValue;
|
|
214
|
-
return result;
|
|
215
|
-
}
|
|
216
|
-
function beforeOrEqualRule(value, start) {
|
|
217
|
-
const result = {
|
|
218
|
-
check: false,
|
|
219
|
-
message: `:attribute must be before or equal to ${start}`,
|
|
220
|
-
value
|
|
221
|
-
};
|
|
222
|
-
const startValue = util.time.parseISO8601ToEpochMilliSeconds(start);
|
|
223
|
-
if (typeof value === "string") result.check = util.time.parseISO8601ToEpochMilliSeconds(value) <= startValue;
|
|
224
|
-
if (typeof value === "number") result.check = value <= startValue;
|
|
225
|
-
return result;
|
|
226
|
-
}
|
|
227
|
-
function afterOrEqualRule(value, start) {
|
|
228
|
-
const result = {
|
|
229
|
-
check: false,
|
|
230
|
-
message: `:attribute must be after or equal to ${start}`,
|
|
231
|
-
value
|
|
232
|
-
};
|
|
233
|
-
const startValue = util.time.parseISO8601ToEpochMilliSeconds(start);
|
|
234
|
-
if (typeof value === "string") result.check = util.time.parseISO8601ToEpochMilliSeconds(value) >= startValue;
|
|
235
|
-
if (typeof value === "number") result.check = value >= startValue;
|
|
236
|
-
return result;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
//#endregion
|
|
240
|
-
//#region src/index.ts
|
|
241
|
-
function validate(obj, checks, options) {
|
|
242
|
-
let error = {};
|
|
243
|
-
Object.keys(checks).forEach((path) => {
|
|
244
|
-
let value = getNestedValue(obj, path);
|
|
245
|
-
if (typeof value === "string") {
|
|
246
|
-
value = cleanString(value, options);
|
|
247
|
-
setNestedValue(obj, path, value);
|
|
248
|
-
}
|
|
249
|
-
let skip = false;
|
|
250
|
-
checks[path]?.forEach((rule) => {
|
|
251
|
-
if (skip) return;
|
|
252
|
-
const result = typeof rule === "string" || isArray(rule) ? parse(value, rule) : { ...rule };
|
|
253
|
-
skip = result.skipNext ?? false;
|
|
254
|
-
if (result.check) return;
|
|
255
|
-
if (error.msg) util.appendError(error.msg, error.errorType, error.data, error.errorInfo);
|
|
256
|
-
result.message = result.message.replace(":attribute", formatAttributeName(path));
|
|
257
|
-
error = {
|
|
258
|
-
msg: result.message,
|
|
259
|
-
errorType: "ValidationError",
|
|
260
|
-
data: null,
|
|
261
|
-
errorInfo: {
|
|
262
|
-
path,
|
|
263
|
-
value
|
|
264
|
-
}
|
|
265
|
-
};
|
|
266
|
-
skip = true;
|
|
267
|
-
});
|
|
268
|
-
});
|
|
269
|
-
if (!error.msg) return obj;
|
|
270
|
-
util.error(error.msg, error.errorType, error.data, error.errorInfo);
|
|
271
|
-
}
|
|
272
|
-
function precognitiveValidation(ctx, checks, options) {
|
|
273
|
-
if (getHeader("precognition", ctx) !== "true") return validate(ctx.args, checks, options);
|
|
274
|
-
const validationKeys = getHeader("Precognition-Validate-Only", ctx)?.split(",").map((key) => key.trim());
|
|
275
|
-
util.http.addResponseHeader("Precognition", "true");
|
|
276
|
-
if (!validationKeys) {
|
|
277
|
-
validate(ctx.args, checks, options);
|
|
278
|
-
util.http.addResponseHeader("Precognition-Success", "true");
|
|
279
|
-
runtime.earlyReturn(null);
|
|
280
|
-
}
|
|
281
|
-
util.http.addResponseHeader("Precognition-Validate-Only", validationKeys.join(","));
|
|
282
|
-
const precognitionChecks = {};
|
|
283
|
-
validationKeys.forEach((key) => {
|
|
284
|
-
precognitionChecks[key] = checks[key];
|
|
285
|
-
});
|
|
286
|
-
validate(ctx.args, precognitionChecks, options);
|
|
287
|
-
util.http.addResponseHeader("Precognition-Success", "true");
|
|
288
|
-
runtime.earlyReturn(null, { skipTo: options?.skipTo ?? "END" });
|
|
289
|
-
}
|
|
290
|
-
function formatAttributeName(path) {
|
|
291
|
-
return path.split(".").reduce((acc, part) => {
|
|
292
|
-
if (util.matches("^\\d+$", part)) return acc;
|
|
293
|
-
return acc ? `${acc} ${part.toLowerCase()}` : part.toLowerCase();
|
|
294
|
-
}, "");
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
//#endregion
|
|
298
|
-
export { formatAttributeName, precognitiveValidation, validate };
|
|
1
|
+
import{cleanString,getHeader,getNestedValue,isArray,setNestedValue}from"./utils.js";import{runtime,util}from"@aws-appsync/utils";function parse(e,v){const[y,...b]=typeof v===`string`?[v,void 0]:[v[0],...v.slice(1)];switch(y){case`required`:return requiredRule(e);case`nullable`:return nullableRule(e);case`sometimes`:return sometimesRule(e);case`min`:return minRule(e,b[0]);case`max`:return maxRule(e,b[0]);case`between`:return betweenRule(e,b[0],b[1]);case`regex`:return regexRule(e,...b);case`in`:return inRule(e,...b);case`notIn`:return notInRule(e,...b);case`array`:return arrayRule(e);case`object`:return objectRule(e);case`boolean`:return booleanRule(e);case`number`:return numberRule(e);case`string`:return stringRule(e);case`before`:return beforeRule(e,b[0]);case`after`:return afterRule(e,b[0]);case`beforeOrEqual`:return beforeOrEqualRule(e,b[0]);case`afterOrEqual`:return afterOrEqualRule(e,b[0]);default:return{check:false,message:`Unknown rule ${y}`,value:e}}}function minRule(e,v){const y={check:false,message:`:attribute must be greater than or equal to ${v}`,value:e};if(typeof e===`number`)y.check=e>=v;if(typeof e===`string`)y.check=e.length>=v;if(isArray(e)){y.check=e.length>=v;y.message=`Array must contain at least ${v} elements`}return y}function maxRule(e,v){const y={check:false,message:`:attribute must be less than or equal to ${v}`,value:e};if(typeof e===`number`)y.check=e<=v;if(typeof e===`string`){y.check=e.length<=v;y.message=`String must contain at most ${v} characters`}if(isArray(e)){y.check=e.length<=v;y.message=`Array must contain at most ${v} elements`}return y}function betweenRule(e,v,y){const x={check:false,message:`:attribute must be between ${v} and ${y}`,value:e};if(typeof e===`number`)x.check=e>=v&&e<=y;if(typeof e===`string`){x.check=e.length>=v&&e.length<=y;x.message=`String must contain between ${v} and ${y} characters`}if(isArray(e)){x.check=e.length>=v&&e.length<=y;x.message=`Array must contain between ${v} and ${y} elements`}return x}function regexRule(e,...v){const y={check:false,message:`:attribute must match the specified regular expression`,value:e};if(typeof e===`string`)y.check=v.some(v=>util.matches(v,e));return y}function inRule(e,...v){return{check:v.includes(e),message:`:attribute must be one of the specified values`,value:e}}function notInRule(e,...v){return{check:!v.includes(e),message:`:attribute must not be one of the specified values`,value:e}}function requiredRule(e){const v={check:true,message:`:attribute is required`,value:e,skipNext:true};if(typeof e===`string`)v.check=e.length>0;if(isArray(e))v.check=e.length>0;if(typeof e===`number`)v.check=true;if(typeof e===`boolean`)v.check=true;if(typeof e===`object`&&!v.value){v.message=`:attribute is not nullable`;v.check=false}if(typeof e===`undefined`)v.check=false;v.skipNext=!v.check;return v}function nullableRule(e){return{check:true,message:``,value:e,skipNext:typeof e===`object`&&!e}}function sometimesRule(e){const v={check:true,message:``,value:e};if(typeof e===`undefined`){v.skipNext=true;return v}if(typeof e===`object`&&!v.value){v.message=`:attribute is not nullable`;v.check=false;v.skipNext=true;return v}return requiredRule(e)}function arrayRule(e){const v={check:false,message:`:attribute must be an array`,value:e};if(isArray(e))v.check=true;return v}function objectRule(e){const v={check:false,message:`:attribute must be an object`,value:e};if(typeof e===`object`&&!isArray(v.value))v.check=true;return v}function booleanRule(e){const v={check:false,message:`:attribute must be a boolean`,value:e};if(typeof e===`boolean`)v.check=true;return v}function numberRule(e){const v={check:false,message:`:attribute must be a number`,value:e};if(typeof e===`number`)v.check=true;return v}function stringRule(e){const v={check:false,message:`:attribute must be a string`,value:e};if(typeof e===`string`)v.check=true;return v}function beforeRule(e,v){const y={check:false,message:`:attribute must be before ${v}`,value:e};const b=util.time.parseISO8601ToEpochMilliSeconds(v);if(typeof e===`string`)y.check=util.time.parseISO8601ToEpochMilliSeconds(e)<b;if(typeof e===`number`)y.check=e<b;return y}function afterRule(e,v){const y={check:false,message:`:attribute must be after ${v}`,value:e};const b=util.time.parseISO8601ToEpochMilliSeconds(v);if(typeof e===`string`)y.check=util.time.parseISO8601ToEpochMilliSeconds(e)>b;if(typeof e===`number`)y.check=e>b;return y}function beforeOrEqualRule(e,v){const y={check:false,message:`:attribute must be before or equal to ${v}`,value:e};const b=util.time.parseISO8601ToEpochMilliSeconds(v);if(typeof e===`string`)y.check=util.time.parseISO8601ToEpochMilliSeconds(e)<=b;if(typeof e===`number`)y.check=e<=b;return y}function afterOrEqualRule(e,v){const y={check:false,message:`:attribute must be after or equal to ${v}`,value:e};const b=util.time.parseISO8601ToEpochMilliSeconds(v);if(typeof e===`string`)y.check=util.time.parseISO8601ToEpochMilliSeconds(e)>=b;if(typeof e===`number`)y.check=e>=b;return y}function validate(v,S,w){let T={};Object.keys(S).forEach(E=>{let D=getNestedValue(v,E);if(typeof D===`string`){D=cleanString(D,w);setNestedValue(v,E,D)}let O=false;S[E]?.forEach(e=>{if(O)return;const v=typeof e===`string`||isArray(e)?parse(D,e):{...e};O=v.skipNext??false;if(v.check)return;if(T.msg)util.appendError(T.msg,T.errorType,T.data,T.errorInfo);v.message=v.message.replace(`:attribute`,formatAttributeName(E));T={msg:v.message,errorType:`ValidationError`,data:null,errorInfo:{path:E,value:D}};O=true})});if(!T.msg)return v;util.error(T.msg,T.errorType,T.data,T.errorInfo)}function precognitiveValidation(e,y,b){if(getHeader(`precognition`,e)!==`true`)return validate(e.args,y,b);const x=getHeader(`Precognition-Validate-Only`,e)?.split(`,`).map(e=>e.trim());util.http.addResponseHeader(`Precognition`,`true`);if(!x){validate(e.args,y,b);util.http.addResponseHeader(`Precognition-Success`,`true`);runtime.earlyReturn(null)}util.http.addResponseHeader(`Precognition-Validate-Only`,x.join(`,`));const C={};x.forEach(e=>{C[e]=y[e]});validate(e.args,C,b);util.http.addResponseHeader(`Precognition-Success`,`true`);runtime.earlyReturn(null,{skipTo:b?.skipTo??`END`})}function formatAttributeName(e){return e.split(`.`).reduce((e,v)=>{if(util.matches(`^\\d+$`,v))return e;return e?`${e} ${v.toLowerCase()}`:v.toLowerCase()},``)}export{formatAttributeName,precognitiveValidation,validate};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["result: Rule<T>","error: { msg?: string, errorType?: string, data?: any, errorInfo?: any }","rules.parse"],"sources":["../src/rules.ts","../src/index.ts"],"sourcesContent":["import type { FullRule, Rule } from './types'\nimport { util } from '@aws-appsync/utils'\nimport { isArray } from './utils'\n\nexport function parse<T>(value: T, rule: FullRule): Rule<T> {\n const [name, ...params] = typeof rule === 'string'\n ? [rule, undefined]\n : [rule[0], ...rule.slice(1)]\n\n switch (name) {\n case 'required':\n return requiredRule(value)\n case 'nullable':\n return nullableRule(value)\n case 'sometimes':\n return sometimesRule(value)\n case 'min':\n return minRule(value, (params[0]! as number))\n case 'max':\n return maxRule(value, (params[0] as number))\n case 'between':\n return betweenRule(value, (params[0] as number), params[1] as number)\n case 'regex':\n return regexRule(value, ...params as string[])\n case 'in':\n return inRule(value, ...params)\n case 'notIn':\n return notInRule(value, ...params)\n case 'array':\n return arrayRule(value)\n case 'object':\n return objectRule(value)\n case 'boolean':\n return booleanRule(value)\n case 'number':\n return numberRule(value)\n case 'string':\n return stringRule(value)\n case 'before':\n return beforeRule(value, params[0] as string)\n case 'after':\n return afterRule(value, params[0] as string)\n case 'beforeOrEqual':\n return beforeOrEqualRule(value, params[0] as string)\n case 'afterOrEqual':\n return afterOrEqualRule(value, params[0] as string)\n default:\n return { check: false, message: `Unknown rule ${name}`, value }\n }\n}\n\nfunction minRule<T>(value: T, minValue: number): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be greater than or equal to ${minValue}`,\n value,\n }\n if (typeof value === 'number') {\n result.check = value >= minValue\n }\n if (typeof value === 'string') {\n result.check = value.length >= minValue\n }\n if (isArray(value)) {\n result.check = value.length >= minValue\n result.message = `Array must contain at least ${minValue} elements`\n }\n return result\n}\n\nfunction maxRule<T>(value: T, maxValue: number): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be less than or equal to ${maxValue}`,\n value,\n }\n\n if (typeof value === 'number') {\n result.check = value <= maxValue\n }\n if (typeof value === 'string') {\n result.check = value.length <= maxValue\n result.message = `String must contain at most ${maxValue} characters`\n }\n if (isArray(value)) {\n result.check = value.length <= maxValue\n result.message = `Array must contain at most ${maxValue} elements`\n }\n return result\n}\n\nfunction betweenRule<T>(value: T, minValue: number, maxValue: number): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be between ${minValue} and ${maxValue}`,\n value,\n }\n if (typeof value === 'number') {\n result.check = value >= minValue && value <= maxValue\n }\n if (typeof value === 'string') {\n result.check = value.length >= minValue && value.length <= maxValue\n result.message = `String must contain between ${minValue} and ${maxValue} characters`\n }\n if (isArray(value)) {\n result.check = value.length >= minValue && value.length <= maxValue\n result.message = `Array must contain between ${minValue} and ${maxValue} elements`\n }\n return result\n}\n\nfunction regexRule<T>(value: T, ...patterns: string[]): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: ':attribute must match the specified regular expression',\n value,\n }\n if (typeof value === 'string') {\n result.check = patterns.some(pattern => util.matches(pattern, value))\n }\n return result\n}\n\nfunction inRule<T>(value: T, ...params: unknown[]): Rule<T> {\n return {\n check: params.includes(value),\n message: ':attribute must be one of the specified values',\n value,\n }\n}\n\nfunction notInRule<T>(value: T, ...params: unknown[]): Rule<T> {\n return {\n check: !params.includes(value),\n message: ':attribute must not be one of the specified values',\n value,\n }\n}\n\nexport function requiredRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: true,\n message: ':attribute is required',\n value,\n skipNext: true,\n }\n if (typeof value === 'string')\n result.check = value.length > 0\n if (isArray(value))\n result.check = value.length > 0\n if (typeof value === 'number')\n result.check = true\n if (typeof value === 'boolean')\n result.check = true\n if (typeof value === 'object' && !result.value) {\n result.message = ':attribute is not nullable'\n result.check = false\n }\n if (typeof value === 'undefined')\n result.check = false\n result.skipNext = !result.check\n return result\n}\n\nfunction nullableRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: true,\n message: '',\n value,\n skipNext: typeof value === 'object' && !value,\n }\n return result\n}\n\nfunction sometimesRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: true,\n message: '',\n value,\n }\n if (typeof value === 'undefined') {\n result.skipNext = true\n return result\n }\n if (typeof value === 'object' && !result.value) {\n result.message = ':attribute is not nullable'\n result.check = false\n result.skipNext = true\n return result\n }\n return requiredRule(value)\n}\n\nfunction arrayRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: ':attribute must be an array',\n value,\n }\n if (isArray(value)) {\n result.check = true\n }\n return result\n}\n\nfunction objectRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: ':attribute must be an object',\n value,\n }\n if (typeof value === 'object' && !isArray(result.value)) {\n result.check = true\n }\n return result\n}\n\nfunction booleanRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: ':attribute must be a boolean',\n value,\n }\n if (typeof value === 'boolean') {\n result.check = true\n }\n return result\n}\n\nfunction numberRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: ':attribute must be a number',\n value,\n }\n if (typeof value === 'number') {\n result.check = true\n }\n return result\n}\n\nfunction stringRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: ':attribute must be a string',\n value,\n }\n if (typeof value === 'string') {\n result.check = true\n }\n return result\n}\n\nfunction beforeRule<T>(value: T, start: string): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be before ${start}`,\n value,\n }\n const startValue = util.time.parseISO8601ToEpochMilliSeconds(start)\n\n if (typeof value === 'string') {\n const date = util.time.parseISO8601ToEpochMilliSeconds(value)\n result.check = date < startValue\n }\n\n if (typeof value === 'number') {\n result.check = value < startValue\n }\n return result\n}\n\nfunction afterRule<T>(value: T, start: string): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be after ${start}`,\n value,\n }\n const startValue = util.time.parseISO8601ToEpochMilliSeconds(start)\n\n if (typeof value === 'string') {\n const date = util.time.parseISO8601ToEpochMilliSeconds(value)\n result.check = date > startValue\n }\n\n if (typeof value === 'number') {\n result.check = value > startValue\n }\n return result\n}\n\nfunction beforeOrEqualRule<T>(value: T, start: string): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be before or equal to ${start}`,\n value,\n }\n const startValue = util.time.parseISO8601ToEpochMilliSeconds(start)\n\n if (typeof value === 'string') {\n const date = util.time.parseISO8601ToEpochMilliSeconds(value)\n result.check = date <= startValue\n }\n\n if (typeof value === 'number') {\n result.check = value <= startValue\n }\n return result\n}\n\nfunction afterOrEqualRule<T>(value: T, start: string): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be after or equal to ${start}`,\n value,\n }\n const startValue = util.time.parseISO8601ToEpochMilliSeconds(start)\n\n if (typeof value === 'string') {\n const date = util.time.parseISO8601ToEpochMilliSeconds(value)\n result.check = date >= startValue\n }\n\n if (typeof value === 'number') {\n result.check = value >= startValue\n }\n return result\n}\n","import type { FullRule, NestedKeyOf, Rule } from './types'\nimport { runtime, util } from '@aws-appsync/utils'\nimport * as rules from './rules'\nimport { cleanString, getHeader, getNestedValue, isArray, setNestedValue } from './utils'\n\nexport function validate<T extends { [key in keyof T & string]: T[key] }>(\n obj: Partial<T>,\n checks: Partial<Record<NestedKeyOf<T>, (FullRule | Rule<T>)[]>>,\n options?: {\n trim?: boolean\n allowEmptyString?: boolean\n },\n): T {\n let error: { msg?: string, errorType?: string, data?: any, errorInfo?: any } = {}\n\n Object.keys(checks).forEach((path) => {\n let value = getNestedValue(obj, path as NestedKeyOf<T>)\n if (typeof value === 'string') {\n value = cleanString(value, options)\n setNestedValue(obj, path as NestedKeyOf<T>, value)\n }\n\n let skip = false\n checks[path as NestedKeyOf<T>]?.forEach((rule) => {\n if (skip)\n return\n\n const result = (typeof rule === 'string' || isArray(rule))\n ? rules.parse(value, rule)\n : { ...rule }\n\n skip = result.skipNext ?? false\n\n if (result.check)\n return\n\n if (error.msg)\n util.appendError(error.msg, error.errorType, error.data, error.errorInfo)\n\n result.message = result.message.replace(':attribute', formatAttributeName(path))\n error = {\n msg: result.message,\n errorType: 'ValidationError',\n data: null,\n errorInfo: { path, value },\n }\n\n skip = true\n })\n })\n\n if (!error.msg) {\n return obj as T\n }\n\n util.error(error.msg, error.errorType, error.data, error.errorInfo)\n}\n\nexport function precognitiveValidation<\n T extends { [key in keyof T & string]: T[key] },\n>(\n ctx: { request: { headers: any }, args: Partial<T> },\n checks: Partial<Record<NestedKeyOf<T>, (FullRule | Rule<T>)[]>>,\n options?: {\n trim?: boolean\n allowEmptyString?: boolean\n skipTo?: 'END' | 'NEXT'\n },\n): T {\n if (getHeader('precognition', ctx) !== 'true') {\n return validate<T>(ctx.args, checks, options)\n }\n const validationKeys = getHeader('Precognition-Validate-Only', ctx)?.split(',').map(key => key.trim())\n util.http.addResponseHeader('Precognition', 'true')\n\n if (!validationKeys) {\n validate(ctx.args, checks, options)\n util.http.addResponseHeader('Precognition-Success', 'true')\n runtime.earlyReturn(null)\n }\n\n util.http.addResponseHeader('Precognition-Validate-Only', validationKeys.join(','))\n const precognitionChecks = {} as Partial<typeof checks>\n validationKeys.forEach((key) => {\n precognitionChecks[key as NestedKeyOf<T>] = checks[key as NestedKeyOf<T>]\n })\n\n validate(ctx.args, precognitionChecks, options)\n util.http.addResponseHeader('Precognition-Success', 'true')\n runtime.earlyReturn(null, { skipTo: options?.skipTo ?? 'END' })\n}\n\nexport function formatAttributeName(path: string): string {\n return path.split('.').reduce((acc, part) => {\n if (util.matches('^\\\\d+$', part)) {\n return acc\n }\n return acc ? `${acc} ${part.toLowerCase()}` : part.toLowerCase()\n }, '')\n}\n"],"mappings":";;;;AAIA,SAAgB,MAAS,OAAU,MAAyB;CAC1D,MAAM,CAAC,MAAM,GAAG,UAAU,OAAO,SAAS,WACtC,CAAC,MAAM,OAAU,GACjB,CAAC,KAAK,IAAI,GAAG,KAAK,MAAM,EAAE,CAAC;AAE/B,SAAQ,MAAR;EACE,KAAK,WACH,QAAO,aAAa,MAAM;EAC5B,KAAK,WACH,QAAO,aAAa,MAAM;EAC5B,KAAK,YACH,QAAO,cAAc,MAAM;EAC7B,KAAK,MACH,QAAO,QAAQ,OAAQ,OAAO,GAAe;EAC/C,KAAK,MACH,QAAO,QAAQ,OAAQ,OAAO,GAAc;EAC9C,KAAK,UACH,QAAO,YAAY,OAAQ,OAAO,IAAe,OAAO,GAAa;EACvE,KAAK,QACH,QAAO,UAAU,OAAO,GAAG,OAAmB;EAChD,KAAK,KACH,QAAO,OAAO,OAAO,GAAG,OAAO;EACjC,KAAK,QACH,QAAO,UAAU,OAAO,GAAG,OAAO;EACpC,KAAK,QACH,QAAO,UAAU,MAAM;EACzB,KAAK,SACH,QAAO,WAAW,MAAM;EAC1B,KAAK,UACH,QAAO,YAAY,MAAM;EAC3B,KAAK,SACH,QAAO,WAAW,MAAM;EAC1B,KAAK,SACH,QAAO,WAAW,MAAM;EAC1B,KAAK,SACH,QAAO,WAAW,OAAO,OAAO,GAAa;EAC/C,KAAK,QACH,QAAO,UAAU,OAAO,OAAO,GAAa;EAC9C,KAAK,gBACH,QAAO,kBAAkB,OAAO,OAAO,GAAa;EACtD,KAAK,eACH,QAAO,iBAAiB,OAAO,OAAO,GAAa;EACrD,QACE,QAAO;GAAE,OAAO;GAAO,SAAS,gBAAgB;GAAQ;GAAO;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F,KAAI,CAAC,MAAM,IACT,QAAO;AAGT,MAAK,MAAM,MAAM,KAAK,MAAM,WAAW,MAAM,MAAM,MAAM,UAAU;;AAGrE,SAAgB,uBAGd,KACA,QACA,SAKG;AACH,KAAI,UAAU,gBAAgB,IAAI,KAAK,OACrC,QAAO,SAAY,IAAI,MAAM,QAAQ,QAAQ;CAE/C,MAAM,iBAAiB,UAAU,8BAA8B,IAAI,EAAE,MAAM,IAAI,CAAC,KAAI,QAAO,IAAI,MAAM,CAAC;AACtG,MAAK,KAAK,kBAAkB,gBAAgB,OAAO;AAEnD,KAAI,CAAC,gBAAgB;AACnB,WAAS,IAAI,MAAM,QAAQ,QAAQ;AACnC,OAAK,KAAK,kBAAkB,wBAAwB,OAAO;AAC3D,UAAQ,YAAY,KAAK;;AAG3B,MAAK,KAAK,kBAAkB,8BAA8B,eAAe,KAAK,IAAI,CAAC;CACnF,MAAM,qBAAqB,EAAE;AAC7B,gBAAe,SAAS,QAAQ;AAC9B,qBAAmB,OAAyB,OAAO;GACnD;AAEF,UAAS,IAAI,MAAM,oBAAoB,QAAQ;AAC/C,MAAK,KAAK,kBAAkB,wBAAwB,OAAO;AAC3D,SAAQ,YAAY,MAAM,EAAE,QAAQ,SAAS,UAAU,OAAO,CAAC;;AAGjE,SAAgB,oBAAoB,MAAsB;AACxD,QAAO,KAAK,MAAM,IAAI,CAAC,QAAQ,KAAK,SAAS;AAC3C,MAAI,KAAK,QAAQ,UAAU,KAAK,CAC9B,QAAO;AAET,SAAO,MAAM,GAAG,IAAI,GAAG,KAAK,aAAa,KAAK,KAAK,aAAa;IAC/D,GAAG"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["result: Rule<T>","error: { msg?: string, errorType?: string, data?: any, errorInfo?: any }","rules.parse"],"sources":["../src/rules.ts","../src/index.ts"],"sourcesContent":["import type { FullRule, Rule } from './types'\nimport { util } from '@aws-appsync/utils'\nimport { isArray } from './utils'\n\nexport function parse<T>(value: T, rule: FullRule): Rule<T> {\n const [name, ...params] = typeof rule === 'string'\n ? [rule, undefined]\n : [rule[0], ...rule.slice(1)]\n\n switch (name) {\n case 'required':\n return requiredRule(value)\n case 'nullable':\n return nullableRule(value)\n case 'sometimes':\n return sometimesRule(value)\n case 'min':\n return minRule(value, (params[0]! as number))\n case 'max':\n return maxRule(value, (params[0] as number))\n case 'between':\n return betweenRule(value, (params[0] as number), params[1] as number)\n case 'regex':\n return regexRule(value, ...params as string[])\n case 'in':\n return inRule(value, ...params)\n case 'notIn':\n return notInRule(value, ...params)\n case 'array':\n return arrayRule(value)\n case 'object':\n return objectRule(value)\n case 'boolean':\n return booleanRule(value)\n case 'number':\n return numberRule(value)\n case 'string':\n return stringRule(value)\n case 'before':\n return beforeRule(value, params[0] as string)\n case 'after':\n return afterRule(value, params[0] as string)\n case 'beforeOrEqual':\n return beforeOrEqualRule(value, params[0] as string)\n case 'afterOrEqual':\n return afterOrEqualRule(value, params[0] as string)\n default:\n return { check: false, message: `Unknown rule ${name}`, value }\n }\n}\n\nfunction minRule<T>(value: T, minValue: number): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be greater than or equal to ${minValue}`,\n value,\n }\n if (typeof value === 'number') {\n result.check = value >= minValue\n }\n if (typeof value === 'string') {\n result.check = value.length >= minValue\n }\n if (isArray(value)) {\n result.check = value.length >= minValue\n result.message = `Array must contain at least ${minValue} elements`\n }\n return result\n}\n\nfunction maxRule<T>(value: T, maxValue: number): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be less than or equal to ${maxValue}`,\n value,\n }\n\n if (typeof value === 'number') {\n result.check = value <= maxValue\n }\n if (typeof value === 'string') {\n result.check = value.length <= maxValue\n result.message = `String must contain at most ${maxValue} characters`\n }\n if (isArray(value)) {\n result.check = value.length <= maxValue\n result.message = `Array must contain at most ${maxValue} elements`\n }\n return result\n}\n\nfunction betweenRule<T>(value: T, minValue: number, maxValue: number): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be between ${minValue} and ${maxValue}`,\n value,\n }\n if (typeof value === 'number') {\n result.check = value >= minValue && value <= maxValue\n }\n if (typeof value === 'string') {\n result.check = value.length >= minValue && value.length <= maxValue\n result.message = `String must contain between ${minValue} and ${maxValue} characters`\n }\n if (isArray(value)) {\n result.check = value.length >= minValue && value.length <= maxValue\n result.message = `Array must contain between ${minValue} and ${maxValue} elements`\n }\n return result\n}\n\nfunction regexRule<T>(value: T, ...patterns: string[]): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: ':attribute must match the specified regular expression',\n value,\n }\n if (typeof value === 'string') {\n result.check = patterns.some(pattern => util.matches(pattern, value))\n }\n return result\n}\n\nfunction inRule<T>(value: T, ...params: unknown[]): Rule<T> {\n return {\n check: params.includes(value),\n message: ':attribute must be one of the specified values',\n value,\n }\n}\n\nfunction notInRule<T>(value: T, ...params: unknown[]): Rule<T> {\n return {\n check: !params.includes(value),\n message: ':attribute must not be one of the specified values',\n value,\n }\n}\n\nexport function requiredRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: true,\n message: ':attribute is required',\n value,\n skipNext: true,\n }\n if (typeof value === 'string')\n result.check = value.length > 0\n if (isArray(value))\n result.check = value.length > 0\n if (typeof value === 'number')\n result.check = true\n if (typeof value === 'boolean')\n result.check = true\n if (typeof value === 'object' && !result.value) {\n result.message = ':attribute is not nullable'\n result.check = false\n }\n if (typeof value === 'undefined')\n result.check = false\n result.skipNext = !result.check\n return result\n}\n\nfunction nullableRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: true,\n message: '',\n value,\n skipNext: typeof value === 'object' && !value,\n }\n return result\n}\n\nfunction sometimesRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: true,\n message: '',\n value,\n }\n if (typeof value === 'undefined') {\n result.skipNext = true\n return result\n }\n if (typeof value === 'object' && !result.value) {\n result.message = ':attribute is not nullable'\n result.check = false\n result.skipNext = true\n return result\n }\n return requiredRule(value)\n}\n\nfunction arrayRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: ':attribute must be an array',\n value,\n }\n if (isArray(value)) {\n result.check = true\n }\n return result\n}\n\nfunction objectRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: ':attribute must be an object',\n value,\n }\n if (typeof value === 'object' && !isArray(result.value)) {\n result.check = true\n }\n return result\n}\n\nfunction booleanRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: ':attribute must be a boolean',\n value,\n }\n if (typeof value === 'boolean') {\n result.check = true\n }\n return result\n}\n\nfunction numberRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: ':attribute must be a number',\n value,\n }\n if (typeof value === 'number') {\n result.check = true\n }\n return result\n}\n\nfunction stringRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: ':attribute must be a string',\n value,\n }\n if (typeof value === 'string') {\n result.check = true\n }\n return result\n}\n\nfunction beforeRule<T>(value: T, start: string): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be before ${start}`,\n value,\n }\n const startValue = util.time.parseISO8601ToEpochMilliSeconds(start)\n\n if (typeof value === 'string') {\n const date = util.time.parseISO8601ToEpochMilliSeconds(value)\n result.check = date < startValue\n }\n\n if (typeof value === 'number') {\n result.check = value < startValue\n }\n return result\n}\n\nfunction afterRule<T>(value: T, start: string): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be after ${start}`,\n value,\n }\n const startValue = util.time.parseISO8601ToEpochMilliSeconds(start)\n\n if (typeof value === 'string') {\n const date = util.time.parseISO8601ToEpochMilliSeconds(value)\n result.check = date > startValue\n }\n\n if (typeof value === 'number') {\n result.check = value > startValue\n }\n return result\n}\n\nfunction beforeOrEqualRule<T>(value: T, start: string): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be before or equal to ${start}`,\n value,\n }\n const startValue = util.time.parseISO8601ToEpochMilliSeconds(start)\n\n if (typeof value === 'string') {\n const date = util.time.parseISO8601ToEpochMilliSeconds(value)\n result.check = date <= startValue\n }\n\n if (typeof value === 'number') {\n result.check = value <= startValue\n }\n return result\n}\n\nfunction afterOrEqualRule<T>(value: T, start: string): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be after or equal to ${start}`,\n value,\n }\n const startValue = util.time.parseISO8601ToEpochMilliSeconds(start)\n\n if (typeof value === 'string') {\n const date = util.time.parseISO8601ToEpochMilliSeconds(value)\n result.check = date >= startValue\n }\n\n if (typeof value === 'number') {\n result.check = value >= startValue\n }\n return result\n}\n","import type { FullRule, NestedKeyOf, Rule } from './types'\nimport { runtime, util } from '@aws-appsync/utils'\nimport * as rules from './rules'\nimport { cleanString, getHeader, getNestedValue, isArray, setNestedValue } from './utils'\n\nexport function validate<T extends { [key in keyof T & string]: T[key] }>(\n obj: Partial<T>,\n checks: Partial<Record<NestedKeyOf<T>, (FullRule | Rule<T>)[]>>,\n options?: {\n trim?: boolean\n allowEmptyString?: boolean\n },\n): T {\n let error: { msg?: string, errorType?: string, data?: any, errorInfo?: any } = {}\n\n Object.keys(checks).forEach((path) => {\n let value = getNestedValue(obj, path as NestedKeyOf<T>)\n if (typeof value === 'string') {\n value = cleanString(value, options)\n setNestedValue(obj, path as NestedKeyOf<T>, value)\n }\n\n let skip = false\n checks[path as NestedKeyOf<T>]?.forEach((rule) => {\n if (skip)\n return\n\n const result = (typeof rule === 'string' || isArray(rule))\n ? rules.parse(value, rule)\n : { ...rule }\n\n skip = result.skipNext ?? false\n\n if (result.check)\n return\n\n if (error.msg)\n util.appendError(error.msg, error.errorType, error.data, error.errorInfo)\n\n result.message = result.message.replace(':attribute', formatAttributeName(path))\n error = {\n msg: result.message,\n errorType: 'ValidationError',\n data: null,\n errorInfo: { path, value },\n }\n\n skip = true\n })\n })\n\n if (!error.msg) {\n return obj as T\n }\n\n util.error(error.msg, error.errorType, error.data, error.errorInfo)\n}\n\nexport function precognitiveValidation<\n T extends { [key in keyof T & string]: T[key] },\n>(\n ctx: { request: { headers: any }, args: Partial<T> },\n checks: Partial<Record<NestedKeyOf<T>, (FullRule | Rule<T>)[]>>,\n options?: {\n trim?: boolean\n allowEmptyString?: boolean\n skipTo?: 'END' | 'NEXT'\n },\n): T {\n if (getHeader('precognition', ctx) !== 'true') {\n return validate<T>(ctx.args, checks, options)\n }\n const validationKeys = getHeader('Precognition-Validate-Only', ctx)?.split(',').map(key => key.trim())\n util.http.addResponseHeader('Precognition', 'true')\n\n if (!validationKeys) {\n validate(ctx.args, checks, options)\n util.http.addResponseHeader('Precognition-Success', 'true')\n runtime.earlyReturn(null)\n }\n\n util.http.addResponseHeader('Precognition-Validate-Only', validationKeys.join(','))\n const precognitionChecks = {} as Partial<typeof checks>\n validationKeys.forEach((key) => {\n precognitionChecks[key as NestedKeyOf<T>] = checks[key as NestedKeyOf<T>]\n })\n\n validate(ctx.args, precognitionChecks, options)\n util.http.addResponseHeader('Precognition-Success', 'true')\n runtime.earlyReturn(null, { skipTo: options?.skipTo ?? 'END' })\n}\n\nexport function formatAttributeName(path: string): string {\n return path.split('.').reduce((acc, part) => {\n if (util.matches('^\\\\d+$', part)) {\n return acc\n }\n return acc ? `${acc} ${part.toLowerCase()}` : part.toLowerCase()\n }, '')\n}\n"],"mappings":"iIAIA,SAAgB,MAAS,EAAU,EAAyB,CAC1D,KAAM,CAAC,EAAM,GAAG,GAAU,OAAO,IAAS,SACtC,CAAC,EAAM,IAAA,GAAU,CACjB,CAAC,EAAK,GAAI,GAAG,EAAK,MAAM,EAAE,CAAC,CAE/B,OAAQ,EAAR,CACE,IAAK,WACH,OAAO,aAAa,EAAM,CAC5B,IAAK,WACH,OAAO,aAAa,EAAM,CAC5B,IAAK,YACH,OAAO,cAAc,EAAM,CAC7B,IAAK,MACH,OAAO,QAAQ,EAAQ,EAAO,GAAe,CAC/C,IAAK,MACH,OAAO,QAAQ,EAAQ,EAAO,GAAc,CAC9C,IAAK,UACH,OAAO,YAAY,EAAQ,EAAO,GAAe,EAAO,G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,KAAK,KAAK,kBAAkB,uBAAwB,OAAO,CAC3D,QAAQ,YAAY,KAAM,CAAE,OAAQ,GAAS,QAAU,MAAO,CAAC,CAGjE,SAAgB,oBAAoB,EAAsB,CACxD,OAAO,EAAK,MAAM,IAAI,CAAC,QAAQ,EAAK,IAAS,CAC3C,GAAI,KAAK,QAAQ,SAAU,EAAK,CAC9B,OAAO,EAET,OAAO,EAAM,GAAG,EAAI,GAAG,EAAK,aAAa,GAAK,EAAK,aAAa,EAC/D,GAAG"}
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","names":[],"sources":["../src/utils.ts"],"sourcesContent":[],"mappings":";;;iBAGgB,OAAA;iBAIA,yCAAyC,aAAa,EAAE,aAAa,QAAQ,UAAU,YAAY;AAJnG,iBAUA,cAVA,CAAA,UAAA,UAAA,MAUyC,CAVzC,GAAA,MAAA,GAUsD,CAVtD,CAUwD,GAVxD,CAAA,EAAA,CAAA,CAAA,GAAA,EAUqE,OAVrE,CAU6E,CAV7E,CAAA,EAAA,IAAA,EAUuF,WAVvF,CAUmG,CAVnG,CAAA,EAAA,KAAA,EAAA,OAAA,CAAA,EAAA,IAAA;AAIA,iBAmBA,SAAA,CAnBA,IAAA,EAAA,MAAA,EAAA,GAAA,EAAA;EAAyC,OAAA,EAAA;IAAa,OAAA,EAAA,GAAA;EAAE,CAAA;CAAqB,CAAA,EAAA,MAAA,GAAA,IAAA;AAAR,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","names":[],"sources":["../src/utils.ts"],"sourcesContent":[],"mappings":";;;iBAGgB,OAAA;iBAIA,yCAAyC,aAAa,EAAE,aAAa,QAAQ,UAAU,YAAY;AAJnG,iBAUA,cAVA,CAAA,UAAA,UAAA,MAUyC,CAVzC,GAAA,MAAA,GAUsD,CAVtD,CAUwD,GAVxD,CAAA,EAAA,CAAA,CAAA,GAAA,EAUqE,OAVrE,CAU6E,CAV7E,CAAA,EAAA,IAAA,EAUuF,WAVvF,CAUmG,CAVnG,CAAA,EAAA,KAAA,EAAA,OAAA,CAAA,EAAA,IAAA;AAIA,iBAmBA,SAAA,CAnBA,IAAA,EAAA,MAAA,EAAA,GAAA,EAAA;EAAyC,OAAA,EAAA;IAAa,OAAA,EAAA,GAAA;EAAE,CAAA;CAAqB,CAAA,EAAA,MAAA,GAAA,IAAA;AAAR,iBAyBrE,WAAA,CAzBqE,KAAA,EAAA,MAAA,EAAA,OAAkB,CAAlB,EAAA;EAA8B,IAAA,CAAA,EAAA,OAAA;EAAZ,gBAAA,CAAA,EAAA,OAAA;CAAA,CAAA,EAAA,MAAA,GAAA,IAAA;AAMvF,cA+BH,IAAA,GA/BG,2EAAA;AAAyC,cAgC5C,IAAA,GAhC4C,0CAAA;AAAa,cAiCzD,GAAA,GAjCyD,qMAAA;AAAE,cAkC3D,KAAA,GAlC2D,mDAAA;AAAqB,cAmChF,KAAA,GAnCgF,qBAAA;AAAR,cAoCxE,IAAA,GApCwE,iDAAA;AAA8B,cAqCtG,IAAA,GArCsG,sDAAA;AAAZ,cAsC1F,QAAA,GAtC0F,mGAAA;AAAA,cAuC1F,OAAA,GAvC0F,oBAAA;AAavF,cA2BH,OAAA,GA3BG,UAAA"}
|
package/dist/utils.js
CHANGED
|
@@ -1,41 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
//#region src/utils.ts
|
|
4
|
-
function isArray(value) {
|
|
5
|
-
return typeof value === "object" && !!value && Object.hasOwn(value, "length") && typeof value.length === "number";
|
|
6
|
-
}
|
|
7
|
-
function getNestedValue(obj, path) {
|
|
8
|
-
return path.split(".").reduce((current, key) => util.matches("^d+$", key) ? current[Number(key)] : current[key], obj);
|
|
9
|
-
}
|
|
10
|
-
function setNestedValue(obj, path, value) {
|
|
11
|
-
const keys = path.split(".");
|
|
12
|
-
if (keys.length === 1) {
|
|
13
|
-
obj[keys[0]] = value;
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
const lastKey = keys.pop();
|
|
17
|
-
const parentObject = getNestedValue(obj, keys.join("."));
|
|
18
|
-
if (typeof parentObject === "object" && !!parentObject) parentObject[lastKey] = value;
|
|
19
|
-
}
|
|
20
|
-
function getHeader(name, ctx) {
|
|
21
|
-
return Object.entries(ctx.request.headers).reduce((prev, [key, value]) => typeof prev === "string" ? prev : key.toLowerCase() === name.toLowerCase() && typeof value === "string" ? value : null, null);
|
|
22
|
-
}
|
|
23
|
-
function cleanString(value, options) {
|
|
24
|
-
if (options?.trim === false) return value;
|
|
25
|
-
const parsed = value.trim();
|
|
26
|
-
if (options?.allowEmptyString) return parsed;
|
|
27
|
-
return parsed === "" ? null : parsed;
|
|
28
|
-
}
|
|
29
|
-
const uuid = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$";
|
|
30
|
-
const ulid = "^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}$";
|
|
31
|
-
const url = "^https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)$|^https?:\\/\\/(localhost|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})(:\\d+)?(\\/.*)?$";
|
|
32
|
-
const email = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
|
|
33
|
-
const phone = "^\\+[1-9]\\d{1,20}$";
|
|
34
|
-
const date = "^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])$";
|
|
35
|
-
const time = "^([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(\\.\\d{1,6})?Z?$";
|
|
36
|
-
const datetime = "^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])T([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(\\.\\d{1,6})?Z$";
|
|
37
|
-
const numeric = "^-?\\d+(\\.\\d+)?$";
|
|
38
|
-
const integer = "^-?\\d+$";
|
|
39
|
-
|
|
40
|
-
//#endregion
|
|
41
|
-
export { cleanString, date, datetime, email, getHeader, getNestedValue, integer, isArray, numeric, phone, setNestedValue, time, ulid, url, uuid };
|
|
1
|
+
import{util}from"@aws-appsync/utils";function isArray(e){return typeof e===`object`&&!!e&&Object.hasOwn(e,`length`)&&typeof e.length===`number`}function getNestedValue(u,d){return d.split(`.`).reduce((u,d)=>util.matches(`^\\d+$`,d)?u[Number(d)]:u[d],u)}function setNestedValue(e,u,f){const p=u.split(`.`);if(p.length===1){e[p[0]]=f;return}const m=p.pop();const h=getNestedValue(e,p.join(`.`));if(typeof h===`object`&&!!h)h[m]=f}function getHeader(e,u){const d=e.toLowerCase();const f=Object.keys(u.request.headers).find(e=>e.toLowerCase()===d);return f?u.request.headers[f]:null}function cleanString(e,u){if(u?.trim===false)return e;const d=e.trim();if(u?.allowEmptyString)return d;return d===``?null:d}const uuid=`^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`;const ulid=`^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}$`;const url=`^https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)$|^https?:\\/\\/(localhost|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})(:\\d+)?(\\/.*)?$`;const email=`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$`;const phone=`^\\+[1-9]\\d{1,20}$`;const date=`^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])$`;const time=`^([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(\\.\\d{1,6})?Z?$`;const datetime=`^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])T([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(\\.\\d{1,6})?Z$`;const numeric=`^-?\\d+(\\.\\d+)?$`;const integer=`^-?\\d+$`;export{cleanString,date,datetime,email,getHeader,getNestedValue,integer,isArray,numeric,phone,setNestedValue,time,ulid,url,uuid};
|
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","names":["parsed: string | null"],"sources":["../src/utils.ts"],"sourcesContent":["import type { NestedKeyOf } from './types'\nimport { util } from '@aws-appsync/utils'\n\nexport function isArray(value: unknown): value is unknown[] {\n return (typeof value === 'object' && !!value && Object.hasOwn(value, 'length')) && typeof (value as unknown[]).length === 'number'\n}\n\nexport function getNestedValue<T extends { [key in keyof T & string]: T[key] }>(obj: Partial<T>, path: NestedKeyOf<T>): any {\n return path.split('.').reduce<unknown>((current, key) => util.matches('
|
|
1
|
+
{"version":3,"file":"utils.js","names":["parsed: string | null"],"sources":["../src/utils.ts"],"sourcesContent":["import type { NestedKeyOf } from './types'\nimport { util } from '@aws-appsync/utils'\n\nexport function isArray(value: unknown): value is unknown[] {\n return (typeof value === 'object' && !!value && Object.hasOwn(value, 'length')) && typeof (value as unknown[]).length === 'number'\n}\n\nexport function getNestedValue<T extends { [key in keyof T & string]: T[key] }>(obj: Partial<T>, path: NestedKeyOf<T>): any {\n return path.split('.').reduce<unknown>((current, key) => util.matches('^\\\\d+$', key)\n ? (current as unknown[])[Number(key)]\n : (current as Record<string, unknown>)[key], obj)\n}\n\nexport function setNestedValue<T extends { [key in keyof T & string]: T[key] }>(obj: Partial<T>, path: NestedKeyOf<T>, value: unknown): void {\n const keys = path.split('.')\n if (keys.length === 1) {\n obj[keys[0] as keyof typeof obj] = value as any\n return\n }\n const lastKey = keys.pop() as string\n const parentObject = getNestedValue(obj, keys.join('.') as NestedKeyOf<T>)\n if (typeof parentObject === 'object' && !!parentObject) {\n parentObject[lastKey] = value\n }\n}\n\nexport function getHeader(name: string, ctx: { request: { headers: any } }): string | null {\n const lowerCaseName = name.toLowerCase()\n const key = Object.keys(ctx.request.headers).find(h => h.toLowerCase() === lowerCaseName)\n return key ? ctx.request.headers[key] : null\n}\n\nexport function cleanString(value: string, options?: {\n trim?: boolean\n allowEmptyString?: boolean\n}): string | null {\n if (options?.trim === false)\n return value\n const parsed: string | null = value.trim()\n if (options?.allowEmptyString)\n return parsed\n return parsed === '' ? null : parsed\n}\n\nexport const uuid = '^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'\nexport const ulid = '^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}$'\nexport const url = '^https?:\\\\/\\\\/(www\\\\.)?[-a-zA-Z0-9@:%._\\\\+~#=]{1,256}\\\\.[a-zA-Z0-9()]{1,6}\\\\b([-a-zA-Z0-9()@:%_\\\\+.~#?&//=]*)$|^https?:\\\\/\\\\/(localhost|\\\\d{1,3}\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}\\\\.\\\\d{1,3})(:\\\\d+)?(\\\\/.*)?$'\nexport const email = '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,}$'\nexport const phone = '^\\\\+[1-9]\\\\d{1,20}$'\nexport const date = '^\\\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\\\d|3[01])$'\nexport const time = '^([01]\\\\d|2[0-3]):[0-5]\\\\d:[0-5]\\\\d(\\\\.\\\\d{1,6})?Z?$'\nexport const datetime = '^\\\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\\\d|3[01])T([01]\\\\d|2[0-3]):[0-5]\\\\d:[0-5]\\\\d(\\\\.\\\\d{1,6})?Z$'\nexport const numeric = '^-?\\\\d+(\\\\.\\\\d+)?$'\nexport const integer = '^-?\\\\d+$'\n"],"mappings":"qCAGA,SAAgB,QAAQ,EAAoC,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