@sot1986/appsync-precognition 0.3.2 → 0.3.3
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.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/{types-1ORDfyL5.d.ts → types-8krJUacZ.d.ts} +11 -3
- package/dist/types-8krJUacZ.d.ts.map +1 -0
- package/dist/utils.d.ts +1 -1
- package/package.json +1 -1
- package/dist/types-1ORDfyL5.d.ts.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { n as
|
|
1
|
+
import { i as Rule, n as FullRule, r as NestedKeyOf, t as CustomFullRule } from "./types-8krJUacZ.js";
|
|
2
2
|
|
|
3
3
|
//#region src/index.d.ts
|
|
4
|
-
declare function validate<T extends { [key in keyof T & string]: T[key] }>(obj: Partial<T>, checks: Partial<Record<NestedKeyOf<T>, (FullRule | Rule<T>)[]>>, options?: {
|
|
4
|
+
declare function validate<T extends { [key in keyof T & string]: T[key] }>(obj: Partial<T>, checks: Partial<Record<NestedKeyOf<T>, (FullRule | CustomFullRule<T> | Rule<T>)[]>>, options?: {
|
|
5
5
|
trim?: boolean;
|
|
6
6
|
allowEmptyString?: boolean;
|
|
7
7
|
}): T;
|
|
@@ -10,7 +10,7 @@ declare function precognitiveValidation<T extends { [key in keyof T & string]: T
|
|
|
10
10
|
headers: any;
|
|
11
11
|
};
|
|
12
12
|
args: Partial<T>;
|
|
13
|
-
}, checks: Partial<Record<NestedKeyOf<T>, (FullRule | Rule<T>)[]>>, options?: {
|
|
13
|
+
}, checks: Partial<Record<NestedKeyOf<T>, (FullRule | CustomFullRule<T> | Rule<T>)[]>>, options?: {
|
|
14
14
|
trim?: boolean;
|
|
15
15
|
allowEmptyString?: boolean;
|
|
16
16
|
skipTo?: "END" | "NEXT";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";;;iBAagB,mCAAmC,aAAa,EAAE,aAC3D,QAAQ,YACL,QAAQ,OAAO,YAAY,KAAK,WAAW,eAAe,KAAK,KAAK,gBAF3B;;EAAnD,gBAAgB,CAAA,EAAA,OAAA;CAAmC,CAAA,EAOhD,CAPgD;AAAa,iBAuDhD,sBAvDgD,CAAA,UAAA,UAAA,MAwDpC,CAxDoC,GAAA,MAAA,GAwDvB,CAxDuB,CAwDrB,GAxDqB,CAAA,EAAA,CAAA,CAAA,GAAA,EAAA;EAAE,OAAA,EAAA;IACnD,OAAA,EAAA,GAAA;EAAR,CAAA;EAC8B,IAAA,EAwDK,OAxDL,CAwDa,CAxDb,CAAA;CAAZ,EAAA,MAAA,EAyDf,OAzDe,CAyDP,MAzDO,CAyDA,WAzDA,CAyDY,CAzDZ,CAAA,EAAA,CAyDiB,QAzDjB,GAyD4B,cAzD5B,CAyD2C,CAzD3C,CAAA,GAyDgD,IAzDhD,CAyDqD,CAzDrD,CAAA,CAAA,EAAA,CAAA,CAAA,EAAA,OAAqD,CAArD,EAAA;EAAiB,IAAA,CAAA,EAAA,OAAA;EAA0B,gBAAA,CAAA,EAAA,OAAA;EAAf,MAAA,CAAA,EAAA,KAAA,GAAA,MAAA;CAAyB,CAAA,EA+D3E,CA/D2E;AAAL,iBAuFzD,mBAAA,CAvFyD,IAAA,EAAA,MAAA,CAAA,EAAA,MAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{cleanString,getHeader,getNestedValue,isArray,setNestedValue}from"./utils.js";import{runtime,util}from"@aws-appsync/utils";function parse(e,
|
|
1
|
+
import{cleanString,getHeader,getNestedValue,isArray,setNestedValue}from"./utils.js";import{runtime,util}from"@aws-appsync/utils";function parse(e,m){const[h,..._]=typeof m===`string`?[m,void 0]:isArray(m)?[m[0],...m.slice(1)]:typeof m.rule===`string`?[m.rule,void 0]:[m.rule[0],...m.rule.slice(1)];switch(h){case`required`:return requiredRule(e);case`nullable`:return nullableRule(e);case`sometimes`:return sometimesRule(e);case`min`:return betweenRule(e,_[0]);case`max`:return betweenRule(e,void 0,_[0]);case`between`:return betweenRule(e,_[0],_[1]);case`regex`:return regexRule(e,..._);case`in`:return inRule(e,..._);case`notIn`:return notInRule(e,..._);case`before`:return beforeRule(e,_[0]);case`after`:return afterRule(e,_[0]);case`beforeOrEqual`:return beforeOrEqualRule(e,_[0]);case`afterOrEqual`:return afterOrEqualRule(e,_[0]);default:return typeRule(e,h)}}function betweenRule({value:e,message:m},h=-Infinity,_=Infinity){const[v,y]=[h===-Infinity,_===Infinity];const b={check:false,message:m??v?`:attribute should be at most ${_}`:y?`:attribute should be at least ${h}`:`:attribute must be between ${h} and ${_}`,value:e};if(typeof e===`number`)b.check=e>=h&&e<=_;if(typeof e===`string`){b.check=e.length>=h&&e.length<=_;b.message=m??v?`String must not exceed ${_} characters`:y?`String must contain at least ${h} characters`:`String must contain between ${h} and ${_} characters`}if(isArray(e)){b.check=e.length>=h&&e.length<=_;b.message=m??v?`Array must not contain more than ${_} elements`:y?`Array must contain at least ${h} elements`:`Array must contain between ${h} and ${_} elements`}return b}function regexRule({value:e,message:m},...h){const g={check:false,message:m??`:attribute must match the specified regular expression`,value:e};if(typeof e===`string`)g.check=h.some(m=>util.matches(m,e));return g}function inRule({value:e,message:m},...h){return{check:h.includes(e),message:m??`:attribute must be one of the specified values`,value:e}}function notInRule({value:e,message:m},...h){return{check:!h.includes(e),message:m??`:attribute must not be one of the specified values`,value:e}}function requiredRule({value:e,message:m}){const h={check:true,message:m??`:attribute is required`,value:e,skipNext:true};if(typeof e===`string`)h.check=e.length>0;if(isArray(e))h.check=e.length>0;if(typeof e===`number`)h.check=true;if(typeof e===`boolean`)h.check=true;if(typeof e===`object`&&!h.value){h.message=m??`:attribute is not nullable`;h.check=false}if(typeof e===`undefined`)h.check=false;h.skipNext=!h.check;return h}function nullableRule({value:e,message:m}){return{check:true,message:m??`:attribute must be nullable`,value:e,skipNext:typeof e===`undefined`||e===null}}function sometimesRule({value:e,message:m}){const h={check:true,message:m??`:attribute is not nullable`,value:e};if(typeof e===`undefined`){h.skipNext=true;return h}if(typeof e===`object`&&!h.value){h.check=false;h.skipNext=true;return h}return requiredRule({value:e,message:m})}function typeRule({value:e,message:m},h){const _={check:false,message:m??`:attribute must be a ${h}`,value:e};switch(h){case`array`:_.check=isArray(e);break;case`object`:_.check=typeof e===`object`&&!!e&&!isArray(e)&&Object.keys(e).length>0;break;case`boolean`:_.check=typeof e===`boolean`;break;case`number`:_.check=typeof e===`number`;break;default:_.check=typeof e===`string`}return _}function beforeRule({value:e,message:m},h){const g={check:false,message:m??`:attribute must be before ${h}`,value:e};const _=util.time.parseISO8601ToEpochMilliSeconds(h);if(typeof e===`string`)g.check=util.time.parseISO8601ToEpochMilliSeconds(e)<_;if(typeof e===`number`)g.check=e<_;return g}function afterRule({value:e,message:m},h){const g={check:false,message:m??`:attribute must be after ${h}`,value:e};const _=util.time.parseISO8601ToEpochMilliSeconds(h);if(typeof e===`string`)g.check=util.time.parseISO8601ToEpochMilliSeconds(e)>_;if(typeof e===`number`)g.check=e>_;return g}function beforeOrEqualRule({value:e,message:m},h){const g={check:false,message:m??`:attribute must be before or equal to ${h}`,value:e};const _=util.time.parseISO8601ToEpochMilliSeconds(h);if(typeof e===`string`)g.check=util.time.parseISO8601ToEpochMilliSeconds(e)<=_;if(typeof e===`number`)g.check=e<=_;return g}function afterOrEqualRule({value:e,message:m},h){const g={check:false,message:m??`:attribute must be after or equal to ${h}`,value:e};const _=util.time.parseISO8601ToEpochMilliSeconds(h);if(typeof e===`string`)g.check=util.time.parseISO8601ToEpochMilliSeconds(e)>=_;if(typeof e===`number`)g.check=e>=_;return g}function isRule(e){return typeof e===`object`&&!!e&&Object.hasOwn(e,`check`)}function isCustomFullRule(e){return typeof e===`object`&&!!e&&Object.hasOwn(e,`rule`)}function validate(m,g,v){let x={};Object.keys(g).forEach(S=>{let C=getNestedValue(m,S);if(typeof C===`string`){C=cleanString(C,v);setNestedValue(m,S,C)}let w=false;g[S]?.forEach(e=>{if(w)return;const m=isRule(e)?{...e,message:e.message??`:attribute is invalid`}:isCustomFullRule(e)?parse({value:C,message:e.message},e.rule):parse({value:C},e);w=!!m.skipNext||!m.check;if(m.check)return;if(x.msg)util.appendError(x.msg,x.errorType,x.data,x.errorInfo);if(util.matches(`:attribute`,m.message))m.message=m.message.replace(`:attribute`,formatAttributeName(S));x={msg:m.message,errorType:`ValidationError`,data:null,errorInfo:{path:S,value:C}}})});if(!x.msg)return m;util.error(x.msg,x.errorType,x.data,x.errorInfo)}function precognitiveValidation(e,h,g){if(getHeader(`precognition`,e)!==`true`)return validate(e.args,h,g);const _=getHeader(`Precognition-Validate-Only`,e)?.split(`,`).map(e=>e.trim());util.http.addResponseHeader(`Precognition`,`true`);if(!_){validate(e.args,h,g);util.http.addResponseHeader(`Precognition-Success`,`true`);runtime.earlyReturn(null)}util.http.addResponseHeader(`Precognition-Validate-Only`,_.join(`,`));const b={};_.forEach(e=>{b[e]=h[e]});validate(e.args,b,g);util.http.addResponseHeader(`Precognition-Success`,`true`);runtime.earlyReturn(null,{skipTo:g?.skipTo??`END`})}function formatAttributeName(e){return e.split(`.`).reduce((e,m)=>{if(util.matches(`^\\d+$`,m))return e;let h=``;m.split(``).forEach((e,m)=>{if(m!==0&&util.matches(`[A-Z]`,e))h+=` `;h+=e.toLowerCase()});return e?`${e} ${h}`:h},``)}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 betweenRule(value, (params[0]! as number))\n case 'max':\n return betweenRule(value, undefined, (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 '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 typeRule(value, name)\n }\n}\n\nfunction betweenRule<T>(value: T, minValue: number = -Infinity, maxValue: number = Infinity): Rule<T> {\n const [min, max] = [minValue === -Infinity, maxValue === Infinity]\n const result: Rule<T> = {\n check: false,\n message: min ? `:attribute should be at most ${maxValue}` : max ? `:attribute should be at least ${minValue}` : `:attribute must be between ${minValue} and ${maxValue}`,\n value,\n }\n if (typeof value === 'number')\n result.check = value >= minValue && value <= maxValue\n if (typeof value === 'string') {\n result.check = value.length >= minValue && value.length <= maxValue\n result.message = min\n ? `String must not exceed ${maxValue} characters`\n : max\n ? `String must contain at least ${minValue} characters`\n : `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 = min\n ? `Array must not contain more than ${maxValue} elements`\n : max\n ? `Array must contain at least ${minValue} elements`\n : `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: ':attribute must be nullable',\n value,\n skipNext: typeof value === 'undefined' || value === null,\n }\n return result\n}\n\nfunction sometimesRule<T>(value: T): Rule<T> {\n const result: Rule<T> = {\n check: true,\n message: ':attribute is not nullable',\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.check = false\n result.skipNext = true\n return result\n }\n return requiredRule(value)\n}\n\nfunction typeRule<T>(value: T, type: 'boolean' | 'object' | 'array' | 'number' | 'string'): Rule<T> {\n const result: Rule<T> = {\n check: false,\n message: `:attribute must be a ${type}`,\n value,\n }\n switch (type) {\n case 'array':\n result.check = isArray(value)\n break\n case 'object':\n result.check = typeof value === 'object' && !!value && !isArray(value) && Object.keys(value).length > 0\n break\n case 'boolean':\n result.check = typeof value === 'boolean'\n break\n case 'number':\n result.check = typeof value === 'number'\n break\n default:\n result.check = typeof value === 'string'\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 || !result.check\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 })\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 let words = ''\n part.split('').forEach((char, idx) => {\n if (idx !== 0 && util.matches('[A-Z]', char)) {\n words += ' '\n }\n words += char.toLowerCase()\n })\n return acc ? `${acc} ${words}` : words\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,YAAY,EAAQ,EAAO,GAAe,CACnD,IAAK,MACH,OAAO,YAAY,EAAO,IAAA,GAAY,EAAO,GAAc,CAC7D,IAAK,UACH,OAAO,YAAY,EAAQ,EAAO,GAAe,EAAO,GAAa,CACvE,IAAK,QACH,OAAO,UAAU,EAAO,GAAG,EAAmB,CAChD,IAAK,KACH,OAAO,OAAO,EAAO,GAAG,EAAO,CACjC,IAAK,QACH,OAAO,UAAU,EAAO,GAAG,EAAO,CACpC,IAAK,SACH,OAAO,WAAW,EAAO,EAAO,GAAa,CAC/C,IAAK,QACH,OAAO,UAAU,EAAO,EAAO,GAAa,CAC9C,IAAK,gBACH,OAAO,kBAAkB,EAAO,EAAO,GAAa,CACtD,IAAK,eACH,OAAO,iBAAiB,EAAO,EAAO,GAAa,CACrD,QACE,OAAO,SAAS,EAAO,EAAK,EAIlC,SAAS,YAAe,EAAU,EAAmB,CAAA,SAAW,EAAmB,SAAmB,CACpG,KAAM,CAAC,EAAK,GAAO,CAAC,IAAa,CAAA,SAAW,IAAa,SAAS,CAClE,MAAMA,EAAkB,CACtB,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U,EAAK,CAC9B,OAAO,EAET,IAAI,EAAQ,GACZ,EAAK,MAAM,GAAG,CAAC,SAAS,EAAM,IAAQ,CACpC,GAAI,IAAQ,GAAK,KAAK,QAAQ,QAAS,EAAK,CAC1C,GAAS,IAEX,GAAS,EAAK,aAAa,EAC3B,CACF,OAAO,EAAM,GAAG,EAAI,GAAG,IAAU,GAChC,GAAG"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["result: ParsedRule<T>","error: { msg?: string, errorType?: string, data?: any, errorInfo?: any }","result: ParsedRule<T>","rules.parse"],"sources":["../src/rules.ts","../src/index.ts"],"sourcesContent":["import type { CustomFullRule, FullRule, ParsedRule, ParseOptions } from './types'\nimport { util } from '@aws-appsync/utils'\nimport { isArray } from './utils'\n\nexport function parse<T>(\n o: ParseOptions<T>,\n r: FullRule | CustomFullRule,\n): ParsedRule<T> {\n const [n, ...p] = typeof r === 'string'\n ? [r, undefined]\n : isArray(r)\n ? [r[0], ...r.slice(1)]\n : typeof r.rule === 'string'\n ? [r.rule, undefined]\n : [r.rule[0], ...r.rule.slice(1)]\n\n switch (n) {\n case 'required':\n return requiredRule(o)\n case 'nullable':\n return nullableRule(o)\n case 'sometimes':\n return sometimesRule(o)\n case 'min':\n return betweenRule(o, (p[0]! as number))\n case 'max':\n return betweenRule(o, undefined, (p[0] as number))\n case 'between':\n return betweenRule(o, (p[0] as number), p[1] as number)\n case 'regex':\n return regexRule(o, ...p as string[])\n case 'in':\n return inRule(o, ...p)\n case 'notIn':\n return notInRule(o, ...p)\n case 'before':\n return beforeRule(o, p[0] as string)\n case 'after':\n return afterRule(o, p[0] as string)\n case 'beforeOrEqual':\n return beforeOrEqualRule(o, p[0] as string)\n case 'afterOrEqual':\n return afterOrEqualRule(o, p[0] as string)\n default:\n return typeRule(o, n)\n }\n}\n\nfunction betweenRule<T>({ value, message: m }: ParseOptions<T>, minV: number = -Infinity, maxV: number = Infinity): ParsedRule<T> {\n const [min, max] = [minV === -Infinity, maxV === Infinity]\n const result: ParsedRule<T> = {\n check: false,\n message: m ?? min ? `:attribute should be at most ${maxV}` : max ? `:attribute should be at least ${minV}` : `:attribute must be between ${minV} and ${maxV}`,\n value,\n }\n if (typeof value === 'number')\n result.check = value >= minV && value <= maxV\n if (typeof value === 'string') {\n result.check = value.length >= minV && value.length <= maxV\n result.message = m ?? min\n ? `String must not exceed ${maxV} characters`\n : max\n ? `String must contain at least ${minV} characters`\n : `String must contain between ${minV} and ${maxV} characters`\n }\n if (isArray(value)) {\n result.check = value.length >= minV && value.length <= maxV\n result.message = m ?? min\n ? `Array must not contain more than ${maxV} elements`\n : max\n ? `Array must contain at least ${minV} elements`\n : `Array must contain between ${minV} and ${maxV} elements`\n }\n return result\n}\n\nfunction regexRule<T>({ value, message: m }: ParseOptions<T>, ...p: string[]): ParsedRule<T> {\n const result: ParsedRule<T> = {\n check: false,\n message: m ?? ':attribute must match the specified regular expression',\n value,\n }\n if (typeof value === 'string') {\n result.check = p.some(pt => util.matches(pt, value))\n }\n return result\n}\n\nfunction inRule<T>({ value, message: m }: ParseOptions<T>, ...p: unknown[]): ParsedRule<T> {\n return {\n check: p.includes(value),\n message: m ?? ':attribute must be one of the specified values',\n value,\n }\n}\n\nfunction notInRule<T>({ value, message: m }: ParseOptions<T>, ...p: unknown[]): ParsedRule<T> {\n return {\n check: !p.includes(value),\n message: m ?? ':attribute must not be one of the specified values',\n value,\n }\n}\n\nexport function requiredRule<T>({ value, message: m }: ParseOptions<T>): ParsedRule<T> {\n const result: ParsedRule<T> = {\n check: true,\n message: m ?? ':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 = m ?? ':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, message: m }: ParseOptions<T>): ParsedRule<T> {\n const result: ParsedRule<T> = {\n check: true,\n message: m ?? ':attribute must be nullable',\n value,\n skipNext: typeof value === 'undefined' || value === null,\n }\n return result\n}\n\nfunction sometimesRule<T>({ value, message: m }: ParseOptions<T>): ParsedRule<T> {\n const result: ParsedRule<T> = {\n check: true,\n message: m ?? ':attribute is not nullable',\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.check = false\n result.skipNext = true\n return result\n }\n return requiredRule({ value, message: m })\n}\n\nfunction typeRule<T>({ value, message: m }: ParseOptions<T>, type: 'boolean' | 'object' | 'array' | 'number' | 'string'): ParsedRule<T> {\n const result: ParsedRule<T> = {\n check: false,\n message: m ?? `:attribute must be a ${type}`,\n value,\n }\n switch (type) {\n case 'array':\n result.check = isArray(value)\n break\n case 'object':\n result.check = typeof value === 'object' && !!value && !isArray(value) && Object.keys(value).length > 0\n break\n case 'boolean':\n result.check = typeof value === 'boolean'\n break\n case 'number':\n result.check = typeof value === 'number'\n break\n default:\n result.check = typeof value === 'string'\n }\n return result\n}\n\nfunction beforeRule<T>({ value, message: m }: ParseOptions<T>, start: string): ParsedRule<T> {\n const result: ParsedRule<T> = {\n check: false,\n message: m ?? `: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, message: m }: ParseOptions<T>, p: string): ParsedRule<T> {\n const result: ParsedRule<T> = {\n check: false,\n message: m ?? `:attribute must be after ${p}`,\n value,\n }\n const e = util.time.parseISO8601ToEpochMilliSeconds(p)\n\n if (typeof value === 'string')\n result.check = util.time.parseISO8601ToEpochMilliSeconds(value) > e\n\n if (typeof value === 'number')\n result.check = value > e\n\n return result\n}\n\nfunction beforeOrEqualRule<T>({ value, message: m }: ParseOptions<T>, p: string): ParsedRule<T> {\n const result: ParsedRule<T> = {\n check: false,\n message: m ?? `:attribute must be before or equal to ${p}`,\n value,\n }\n const s = util.time.parseISO8601ToEpochMilliSeconds(p)\n\n if (typeof value === 'string')\n result.check = util.time.parseISO8601ToEpochMilliSeconds(value) <= s\n\n if (typeof value === 'number')\n result.check = value <= s\n\n return result\n}\n\nfunction afterOrEqualRule<T>({ value, message: m }: ParseOptions<T>, p: string): ParsedRule<T> {\n const result: ParsedRule<T> = {\n check: false,\n message: m ?? `:attribute must be after or equal to ${p}`,\n value,\n }\n const e = util.time.parseISO8601ToEpochMilliSeconds(p)\n\n if (typeof value === 'string')\n result.check = util.time.parseISO8601ToEpochMilliSeconds(value) >= e\n\n if (typeof value === 'number')\n result.check = value >= e\n\n return result\n}\n","import type { CustomFullRule, FullRule, NestedKeyOf, ParsedRule, Rule } from './types'\nimport { runtime, util } from '@aws-appsync/utils'\nimport * as rules from './rules'\nimport { cleanString, getHeader, getNestedValue, setNestedValue } from './utils'\n\nfunction isRule<T>(rule: FullRule | CustomFullRule<T> | Rule<T>): rule is Rule<T> {\n return typeof rule === 'object' && !!rule && Object.hasOwn(rule, 'check')\n}\n\nfunction isCustomFullRule<T>(rule: FullRule | CustomFullRule<T> | Rule<T>): rule is CustomFullRule<T> {\n return typeof rule === 'object' && !!rule && Object.hasOwn(rule, 'rule')\n}\n\nexport function validate<T extends { [key in keyof T & string]: T[key] }>(\n obj: Partial<T>,\n checks: Partial<Record<NestedKeyOf<T>, (FullRule | CustomFullRule<T> | 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: ParsedRule<T> = isRule(rule)\n ? { ...rule, message: rule.message ?? ':attribute is invalid' }\n : isCustomFullRule(rule)\n ? rules.parse<T>({ value, message: rule.message }, rule.rule)\n : rules.parse<T>({ value }, rule)\n\n skip = !!result.skipNext || !result.check\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 if (util.matches(':attribute', result.message))\n result.message = result.message.replace(':attribute', formatAttributeName(path))\n\n error = {\n msg: result.message,\n errorType: 'ValidationError',\n data: null,\n errorInfo: { path, value },\n }\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 | CustomFullRule<T> | 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 let words = ''\n part.split('').forEach((char, idx) => {\n if (idx !== 0 && util.matches('[A-Z]', char)) {\n words += ' '\n }\n words += char.toLowerCase()\n })\n return acc ? `${acc} ${words}` : words\n }, '')\n}\n"],"mappings":"iIAIA,SAAgB,MACd,EACA,EACe,CACf,KAAM,CAAC,EAAG,GAAG,GAAK,OAAO,IAAM,SAC3B,CAAC,EAAG,IAAA,GAAU,CACd,QAAQ,EAAE,CACR,CAAC,EAAE,GAAI,GAAG,EAAE,MAAM,EAAE,CAAC,CACrB,OAAO,EAAE,OAAS,SAChB,CAAC,EAAE,KAAM,IAAA,GAAU,CACnB,CAAC,EAAE,KAAK,GAAI,GAAG,EAAE,KAAK,MAAM,EAAE,CAAC,CAEvC,OAAQ,EAAR,CACE,IAAK,WACH,OAAO,aAAa,EAAE,CACxB,IAAK,WACH,OAAO,aAAa,EAAE,CACxB,IAAK,YACH,OAAO,cAAc,EAAE,CACzB,IAAK,MACH,OAAO,YAAY,EAAI,EAAE,GAAe,CAC1C,IAAK,MACH,OAAO,YAAY,EAAG,IAAA,GAAY,EAAE,GAAc,CACpD,IAAK,UACH,OAAO,YAAY,EAAI,EAAE,GAAe,EAAE,GAAa,CACzD,IAAK,QACH,OAAO,UAAU,EAAG,GAAG,EAAc,CACvC,IAAK,KACH,OAAO,OAAO,EAAG,GAAG,EAAE,CACxB,IAAK,QACH,OAAO,UAAU,EAAG,GAAG,EAAE,CAC3B,IAAK,SACH,OAAO,WAAW,EAAG,EAAE,GAAa,CACtC,IAAK,QACH,OAAO,UAAU,EAAG,EAAE,GAAa,CACrC,IAAK,gBACH,OAAO,kBAAkB,EAAG,EAAE,GAAa,CAC7C,IAAK,eACH,OAAO,iBAAiB,EAAG,EAAE,GAAa,CAC5C,QACE,OAAO,SAAS,EAAG,EAAE,EAI3B,SAAS,YAAe,CAAE,QAAO,QAAS,GAAsB,EAAe,CAAA,SAAW,EAAe,SAAyB,CAChI,KAAM,CAAC,EAAK,GAAO,CAAC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CACH,GAAI,UAAU,eAAgB,EAAI,GAAK,OACrC,OAAO,SAAY,EAAI,KAAM,EAAQ,EAAQ,CAE/C,MAAM,EAAiB,UAAU,6BAA8B,EAAI,EAAE,MAAM,IAAI,CAAC,IAAI,GAAO,EAAI,MAAM,CAAC,CACtG,KAAK,KAAK,kBAAkB,eAAgB,OAAO,CAEnD,GAAI,CAAC,EAAgB,CACnB,SAAS,EAAI,KAAM,EAAQ,EAAQ,CACnC,KAAK,KAAK,kBAAkB,uBAAwB,OAAO,CAC3D,QAAQ,YAAY,KAAK,CAG3B,KAAK,KAAK,kBAAkB,6BAA8B,EAAe,KAAK,IAAI,CAAC,CACnF,MAAM,EAAqB,EAAE,CAC7B,EAAe,QAAS,GAAQ,CAC9B,EAAmB,GAAyB,EAAO,IACnD,CAEF,SAAS,EAAI,KAAM,EAAoB,EAAQ,CAC/C,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,IAAI,EAAQ,GACZ,EAAK,MAAM,GAAG,CAAC,SAAS,EAAM,IAAQ,CACpC,GAAI,IAAQ,GAAK,KAAK,QAAQ,QAAS,EAAK,CAC1C,GAAS,IAEX,GAAS,EAAK,aAAa,EAC3B,CACF,OAAO,EAAM,GAAG,EAAI,GAAG,IAAU,GAChC,GAAG"}
|
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
//#region src/types.d.ts
|
|
2
|
+
type FullRule = "required" | "nullable" | "sometimes" | "number" | "boolean" | "string" | "array" | "object" | ["min", number] | ["max", number] | ["between", number, number] | ["regex", ...string[]] | ["in", ...(string | number | boolean | null)[]] | ["notIn", ...(string | number | boolean | null)[]] | ["after", string] | ["before", string] | ["afterOrEqual", string] | ["beforeOrEqual", string];
|
|
3
|
+
interface CustomFullRule<T = unknown> {
|
|
4
|
+
rule: FullRule;
|
|
5
|
+
value: T;
|
|
6
|
+
message?: string;
|
|
7
|
+
skipNext?: boolean;
|
|
8
|
+
attribute?: string;
|
|
9
|
+
}
|
|
2
10
|
interface Rule<T = unknown> {
|
|
3
11
|
check: boolean;
|
|
4
|
-
message: string;
|
|
5
12
|
value: T;
|
|
13
|
+
message?: string;
|
|
6
14
|
skipNext?: boolean;
|
|
15
|
+
attribute?: string;
|
|
7
16
|
}
|
|
8
|
-
type FullRule = "required" | "nullable" | "sometimes" | "number" | "boolean" | "string" | "array" | "object" | ["min", number] | ["max", number] | ["between", number, number] | ["regex", ...string[]] | ["in", ...(string | number | boolean | null)[]] | ["notIn", ...(string | number | boolean | null)[]] | ["after", string] | ["before", string] | ["afterOrEqual", string] | ["beforeOrEqual", string];
|
|
9
17
|
type ArrayKeys<T extends unknown[]> = T extends [unknown, ...unknown[]] ? T extends Record<infer Index, unknown> ? Index extends `${number}` ? Index : never : never : `${number}`;
|
|
10
18
|
type ObjectKeys<T extends object> = T extends unknown[] ? ArrayKeys<T> : keyof T & string;
|
|
11
19
|
interface HasConstructor {
|
|
@@ -13,4 +21,4 @@ interface HasConstructor {
|
|
|
13
21
|
}
|
|
14
22
|
type NestedKeyOf<T> = T extends Record<infer Key, unknown> ? T extends HasConstructor ? never : T extends CallableFunction ? never : Key extends string | number ? (ObjectKeys<T> | (T[Key] extends object ? `${ObjectKeys<Pick<T, Key>>}.${NestedKeyOf<T[Key]>}` : T extends unknown[] ? T extends [unknown, ...unknown[]] ? never : T[number] extends object ? `${number}.${NestedKeyOf<T[number]>}` : never : never)) : never : never;
|
|
15
23
|
//#endregion
|
|
16
|
-
export {
|
|
24
|
+
export { Rule as i, FullRule as n, NestedKeyOf as r, CustomFullRule as t };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-8krJUacZ.d.ts","names":[],"sources":["../src/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