jet-schema
Advanced tools
Comparing version 1.2.6 to 1.3.0
@@ -5,5 +5,5 @@ 'use strict'; | ||
function n(n){return void 0===n}function t(n){return "number"==typeof n}function e(n){return "string"==typeof n}function r(n){return !!n&&"object"==typeof n}function o(n){if(!function(n){return "object"==typeof n&&!Array.isArray(n)}(n))throw Error('"getEnumKeys" must receive a non-array object');let e=Object.keys(n).reduce(((t,e)=>(t.includes(e)||t.push(n[e]),t)),[]);return t(n[e[0]])&&(e=e.map((t=>n[t]))),[e[0],n=>e.some((t=>t===n))]}function i(n){if(!r(n)||Array.isArray(n))return !1;const o=n,i=Object.keys(o),l=Math.floor(i.length/2);if(!t(o[i[l]]))return function(n,t){if(r(n))for(const e of Object.entries(n))if(!t(e[0],e[1]))return !1;return !0}(n,((n,t)=>e(n)&&e(t)));if(i.length%2!=0)return !1;for(let n=0;n<l;n++){const t=i[n],e=o[t],r=i[n+l],u=o[r];if(e!==r||t!==String(u))return !1}return !0}const l=n=>n instanceof Date&&!isNaN(new Date(n).getTime());function u(n,t){return (e,r)=>(void 0!==e&&(e=n(e)),r?.(e),t(e))}const f='"init" must be true if schema is not optional.',s="This validation failed when setting up defaults.",a="Setup error: validator must be a function, enum, schema, or Date constructor.",c="schema.test failed: value was undefined but not optional.",d="schema.test failed: value was null but not nullable.",p="schema.test failed: value was neither undefined nor null but not an object.",m="schema.parse failed: value must be an object.",h='Failed in the "new" function',v='Failed in the "test" function',b='Failed in the "parse" function';function E(t){const e=function(n){const t=new Map;for(const e of n){const{vf:n,...r}=e;t.set(n,r);}return t}(t?.globals??[]),E=t?.cloneFn?t.cloneFn:g,F=t?.onError?function(n){return (t,e,r,o)=>{const i=D(t,e,r,o);return n(t,e,i)}}(t.onError):j;return (t,...g)=>{const[j]=g,D=function(t){let e={init:!0};n(t?.init)||(e.init=t.init);t?.id&&(e.id=t?.id);e=t?.nullish?{...e,optional:!0,nullable:!0}:{...e,optional:!!t?.optional,nullable:!!t?.nullable};return e}(j);let H=F;D.id&&(H=O(H,D.id)),D.optional||!1!==D.init&&!n(D.init)||H("","",f);const N=function(t,e,r,f,c=""){const d={},p={};for(const m in t){const h=t[m];if(d[m]={vf:n=>!1,default:()=>!1},h===Date)d[m]={vf:u((n=>new Date(n)),l),default:()=>new Date};else if("function"==typeof h||y(h)){let t,o,i=!1,l=!1;if(y(h)){const n=h;if(t=n.vf,"default"in n&&(o=n.default,i=!0),n.transform&&(t=u(n.transform,t),l=!0),n.onError){let t=n.onError;c&&(t=O(t,c)),d[m].onError=t;}}else t=h;const f=r.get(t);if(f&&(!i&&"default"in f&&(o=f.default),!l&&f.transform&&(t=u(f.transform,t)),f.onError)){let n=f.onError;c&&(n=O(n,c)),d[m].onError=n;}if(n(o))d[m].default=()=>{};else {const n=e(o);d[m].default=()=>n;}d[m].vf=t;}else if(w(h)){const n=h,t=n._schemaOptions.init;d[m].default=!0===t?()=>n.new():null===t?()=>null:()=>{},p[m]=()=>n.new(),d[m].vf=n.test;}else if(i(h)){const[n,t]=o(h);d[m].default=()=>e(n),d[m].vf=t;}else f(m,"",a);const v=d[m].vf,b=d[m].default();v(b)||(d[m].onError?d[m].onError(m,b,s):f(m,b,s));}return {childSchemaNewFns:p,allVldtrsHolder:d}}(t,E,e,H,D.id),R=function(n,t,e){return (r={})=>{const o={};for(const t in n){const e=n[t].default();void 0!==e&&(o[t]=e);}for(const i in r){if(!n[i])continue;const l=n[i].vf;let u=r[i];l(u,(n=>u=n))?o[i]=t(u):(n[i].onError?n[i].onError(i,u,h):e(i,u,h),o[i]="**ERROR**");}return o}}(N.allVldtrsHolder,E,H),S=function(t,e,o,i){return l=>{if(n(l))return !!e||(i("",l,c),!1);if(null===l)return !!o||(i("",l,d),!1);if(!r(l))return i("",l,p),!1;for(const n in t){const e=t[n].vf;let r=l[n];if(!e(r,(n=>r=n)))return t[n].onError?t[n].onError(n,r,v):i(n,r,v),!1;n in l&&(l[n]=r);}return !0}}(N.allVldtrsHolder,D.optional,D.nullable,H),V=function(n,t,e){return o=>{const i={};if(!r(o))return e("",o,m),i;for(const r in n){const l=n[r].vf;let u=o[r];l(u,(n=>u=n))||(n[r].onError?n[r].onError(r,u,b):e(r,u,b),i[r]="**ERROR**"),r in o&&(i[r]=t(u));}return i}}(N.allVldtrsHolder,E,H);return {new:R,test:S,pick:n=>{const e=t[n],r=n;if(e)return {default:N.allVldtrsHolder[r].default,test:N.allVldtrsHolder[r].vf,...w(e)?{pick:e.pick,new:N.childSchemaNewFns[r],schema:()=>({...e})}:{}}},parse:V,_schemaOptions:D}}}function w(n){return r(n)&&"_schemaOptions"in n}function y(n){return r(n)&&"vf"in n&&"function"==typeof n.vf}function g(n){return n instanceof Date?new Date(n):r(n)?structuredClone(n):n}function O(n,t){return (e,r,o)=>n(e,r,o,t)}function j(n,t,e,r){const o=D(n,t,e,r);throw new Error(o)}function D(n,t,e,r){n&&(n=`The property "${n}" failed to pass validation.`);let o="";if(!n||t||e||r)if(n||!t||e||r)if(n||t||!e||r){let i;i=n?{message:n,value:t,"more-details":e}:{message:e,value:t},r&&(i["schema-id"]=r),o=JSON.stringify(i);}else o=e;else o=JSON.stringify(t);else o=n;return o}const F=E(); | ||
function t(t){return void 0===t}function e(t){return "number"==typeof t}function n(t){return "string"==typeof t}function r(t){return !!t&&"object"==typeof t}function o(t){if(!function(t){return "object"==typeof t&&!Array.isArray(t)}(t))throw Error('"getEnumKeys" must receive a non-array object');let n=Object.keys(t).reduce(((e,n)=>(e.includes(n)||e.push(t[n]),e)),[]);return e(t[n[0]])&&(n=n.map((e=>t[e]))),[n[0],t=>n.some((e=>e===t))]}function i(t){if(!r(t)||Array.isArray(t))return !1;const o=t,i=Object.keys(o),l=Math.floor(i.length/2);if(!e(o[i[l]]))return function(t,e){if(r(t))for(const n of Object.entries(t))if(!e(n[0],n[1]))return !1;return !0}(t,((t,e)=>n(t)&&n(e)));if(i.length%2!=0)return !1;for(let t=0;t<l;t++){const e=i[t],n=o[e],r=i[t+l],f=o[r];if(n!==r||e!==String(f))return !1}return !0}const l=t=>t instanceof Date&&!isNaN(new Date(t).getTime());function f(t,e){return (n,r)=>(void 0!==n&&(n=t(n)),r?.(n),e(n))}const s='"init" must be true if schema is not optional.',u="This validation failed when setting up defaults.",a="Setup error: validator must be a function, enum, schema, or Date constructor.",c="schema.test failed: value was undefined but not optional.",d="schema.test failed: value was null but not nullable.",p="schema.parse failed: value must be an object.",h='Failed in the "new" function',m='Failed in the "test" function',v='Failed in the "parse" function',y="Argument had a property which was not in schema, which is not allowed in strict mode";function w(e){const n=function(t){const e=new Map;for(const n of t){const{vf:t,...r}=n;e.set(t,r);}return e}(e?.globals??[]),w=e?.cloneFn?e.cloneFn:g,F=e?.onError?function(t){return (e,n,r,o)=>{const i=D(e,n,r,o);return t(e,n,i)}}(e.onError):j;return (e,...g)=>{const[j]=g,D=function(e){let n={init:!0,safety:"filter"};t(e?.init)||(n.init=e.init);e?.id&&(n.id=e?.id);e?.safety&&(n.safety=e.safety);n=e?.nullish?{...n,optional:!0,nullable:!0}:{...n,optional:!!e?.optional,nullable:!!e?.nullable};return n}(j);let H=F;D.id&&(H=O(H,D.id)),D.optional||!1!==D.init&&!t(D.init)||H("","",s);const N=function(e,n,r,s,c=""){const d={},p={};for(const h in e){const m=e[h];if(d[h]={vf:t=>!1,default:()=>!1},m===Date)d[h]={vf:f((t=>new Date(t)),l),default:()=>new Date};else if("function"==typeof m||E(m)){let e,o,i=!1,l=!1;if(E(m)){const t=m;if(e=t.vf,"default"in t&&(o=t.default,i=!0),t.transform&&(e=f(t.transform,e),l=!0),t.onError){let e=t.onError;c&&(e=O(e,c)),d[h].onError=e;}}else e=m;const s=r.get(e);if(s&&(!i&&"default"in s&&(o=s.default),!l&&s.transform&&(e=f(s.transform,e)),s.onError)){let t=s.onError;c&&(t=O(t,c)),d[h].onError=t;}if(t(o))d[h].default=()=>{};else {const t=n(o);d[h].default=()=>t;}d[h].vf=e;}else if(b(m)){const t=m,e=t._schemaOptions.init;d[h].default=!0===e?()=>t.new():null===e?()=>null:()=>{},p[h]=()=>t.new(),d[h].vf=t.test;}else if(i(m)){const[t,e]=o(m);d[h].default=()=>n(t),d[h].vf=e;}else s(h,"",a);const v=d[h].vf,y=d[h].default();v(y)||(d[h].onError?d[h].onError(h,y,u):s(h,y,u));}return {childSchemaNewFns:p,allVldtrsHolder:d}}(e,w,n,H,D.id),S=function(t,e,n){return (r={})=>{const o={};for(const e in t){const n=t[e].default();void 0!==n&&(o[e]=n);}for(const i in r){const l=t[i];if(!l)continue;let f=r[i];l.vf(f,(t=>f=t))?o[i]=e(f):(l.onError?l.onError(i,f,h):n(i,f,h),o[i]="**ERROR**");}return o}}(N.allVldtrsHolder,w,H),V=function(e,n,o,i="filter",l){return f=>{if(t(f))return !!n||(l("",f,c),!1);if(null===f)return !!o||(l("",f,d),!1);if(!r(f))return !1;for(const t in f){const n=e[t];let r=f[t];if(n){if(!n.vf(r,(e=>{r=e,Object.defineProperty(f,t,{value:r});})))return n.onError?n.onError(t,r,m):l(t,r,m),!1}else {if("strict"===i)return l(t,r,y),!1;"pass"!==i&&Reflect.deleteProperty(f,t);}}return !0}}(N.allVldtrsHolder,D.optional,D.nullable,D.safety,H),k=function(t,e="filter",n){return o=>{if(!r(o))return n("",o,p),o;for(const r in o){const i=t[r];let l=o[r];i?i.vf(l,(t=>{l=t,Object.defineProperty(o,r,{value:l});}))||(i.onError?i.onError(r,l,v):n(r,l,v)):("pass"!==e&&Reflect.deleteProperty(o,r),"strict"===e&&n(r,l,y));}return o}}(N.allVldtrsHolder,D.safety,H);return {new:S,test:V,pick:t=>{const n=e[t],r=t;if(n)return {default:N.allVldtrsHolder[r].default,test:N.allVldtrsHolder[r].vf,...b(n)?{pick:n.pick,new:N.childSchemaNewFns[r],schema:()=>({...n})}:{}}},parse:k,_schemaOptions:D}}}function b(t){return r(t)&&"_schemaOptions"in t}function E(t){return r(t)&&"vf"in t&&"function"==typeof t.vf}function g(t){return t instanceof Date?new Date(t):r(t)?structuredClone(t):t}function O(t,e){return (n,r,o)=>t(n,r,o,e)}function j(t,e,n,r){const o=D(t,e,n,r);throw new Error(o)}function D(t,e,n,r){t&&(t=`The property "${t}" failed to pass validation.`);let o="";if(!t||e||n||r)if(t||!e||n||r)if(t||e||!n||r){let i;i=t?{message:t,value:e,"more-details":n}:{message:n,value:e},r&&(i["schema-id"]=r),o=JSON.stringify(i);}else o=n;else o=JSON.stringify(e);else o=t;return o}const F=w(); | ||
exports.default = E; | ||
exports.default = w; | ||
exports.schema = F; |
@@ -32,2 +32,3 @@ import { TFunc, IValidatorFnOrObj, TEnum, IValidatorObj, TValidatorFn } from './util'; | ||
id?: string; | ||
safety?: 'pass' | 'filter' | 'strict'; | ||
}; | ||
@@ -64,3 +65,7 @@ } | ||
}; | ||
export interface IOptNul { | ||
interface ISchemaOptionsBase { | ||
id?: string; | ||
safety?: 'pass' | 'filter' | 'strict'; | ||
} | ||
export interface IOptNul extends ISchemaOptionsBase { | ||
optional: true; | ||
@@ -70,23 +75,19 @@ nullable: true; | ||
nullish?: undefined; | ||
id?: string; | ||
} | ||
export interface IOptNotNul { | ||
export interface IOptNotNul extends ISchemaOptionsBase { | ||
optional: true; | ||
nullable?: false; | ||
init?: boolean; | ||
id?: string; | ||
} | ||
export interface INotOptButNul { | ||
export interface INotOptButNul extends ISchemaOptionsBase { | ||
optional?: false; | ||
nullable: true; | ||
init?: null | true; | ||
id?: string; | ||
} | ||
export interface INotOptOrNul { | ||
export interface INotOptOrNul extends ISchemaOptionsBase { | ||
optional?: false; | ||
nullable?: false; | ||
init?: true; | ||
id?: string; | ||
} | ||
export interface INullish { | ||
export interface INullish extends ISchemaOptionsBase { | ||
nullish: true; | ||
@@ -96,5 +97,4 @@ optional?: undefined; | ||
init?: null | boolean; | ||
id?: string; | ||
} | ||
declare function jetSchema<M extends TGlobalsArr<M>>(options?: IJetOptions<M>): <T, U extends TSchemaFnObjArg<T> = Required<{ [K in keyof T]: T[K] extends string | number ? TEnum | IValidatorFnOrObj<T[K]> : T[K] extends Date ? DateConstructor | IValidatorFnOrObj<T[K]> : IsStaticObj<T[K], NonNullable<T[K]>> extends true ? ISchema<T[K]> : IValidatorFnOrObj<T[K]>; }>, R extends TSchemaOptions<T> = TSchemaOptions<T>>(schemaFnObjArg: U, ...options: TSchemaOptionsHelper<T, R>) => unknown extends T ? ISchema<InferTypes<U, R>> : ISchema<T>; | ||
export default jetSchema; |
@@ -26,2 +26,3 @@ "use strict"; | ||
ParseFn: 'Failed in the "parse" function', | ||
StrictMode: 'Argument had a property which was not in schema, which is not allowed in strict mode', | ||
}; | ||
@@ -41,3 +42,3 @@ function jetSchema(options) { | ||
} | ||
const ret = _setupAllVldtrsHolder(schemaFnObjArg, cloneFn, globalsMap, onErrorF, optionsF.id), newFn = _setupNewFn(ret.allVldtrsHolder, cloneFn, onErrorF), testFn = _setupTestFn(ret.allVldtrsHolder, optionsF.optional, optionsF.nullable, onErrorF), parseFn = _setupParseFn(ret.allVldtrsHolder, cloneFn, onErrorF); | ||
const ret = _setupAllVldtrsHolder(schemaFnObjArg, cloneFn, globalsMap, onErrorF, optionsF.id), newFn = _setupNewFn(ret.allVldtrsHolder, cloneFn, onErrorF), testFn = _setupTestFn(ret.allVldtrsHolder, optionsF.optional, optionsF.nullable, optionsF.safety, onErrorF), parseFn = _setupParseFn(ret.allVldtrsHolder, optionsF.safety, onErrorF); | ||
return { | ||
@@ -178,13 +179,13 @@ new: newFn, | ||
for (const key in partial) { | ||
if (!allVldtrsHolder[key]) { | ||
const vldrObj = allVldtrsHolder[key]; | ||
if (!vldrObj) { | ||
continue; | ||
} | ||
const testFn = allVldtrsHolder[key].vf; | ||
let val = partial[key]; | ||
if (testFn(val, ((transVal) => val = transVal))) { | ||
if (vldrObj.vf(val, (transVal => val = transVal))) { | ||
retVal[key] = cloneFn(val); | ||
} | ||
else { | ||
if (!!allVldtrsHolder[key].onError) { | ||
allVldtrsHolder[key].onError(key, val, Errors.NewFn); | ||
if (!!vldrObj.onError) { | ||
vldrObj.onError(key, val, Errors.NewFn); | ||
} | ||
@@ -200,3 +201,3 @@ else { | ||
} | ||
function _setupTestFn(allVldtrsHolder, isOptional, isNullable, onError) { | ||
function _setupTestFn(allVldtrsHolder, isOptional, isNullable, safety = 'filter', onError) { | ||
return (arg) => { | ||
@@ -222,12 +223,24 @@ if ((0, util_1.isUndef)(arg)) { | ||
if (!(0, util_1.isObj)(arg)) { | ||
onError('', arg, Errors.NotAnObj); | ||
return false; | ||
} | ||
for (const key in allVldtrsHolder) { | ||
const testFn = allVldtrsHolder[key].vf; | ||
for (const key in arg) { | ||
const vldrObj = allVldtrsHolder[key]; | ||
let val = arg[key]; | ||
if (!testFn(val, ((transVal) => val = transVal))) { | ||
if (!!allVldtrsHolder[key].onError) { | ||
allVldtrsHolder[key].onError(key, val, Errors.TestFn); | ||
if (!vldrObj) { | ||
if (safety === 'strict') { | ||
onError(key, val, Errors.StrictMode); | ||
return false; | ||
} | ||
else if (safety !== 'pass') { | ||
Reflect.deleteProperty(arg, key); | ||
} | ||
continue; | ||
} | ||
if (!vldrObj.vf(val, (transVal => { | ||
val = transVal; | ||
Object.defineProperty(arg, key, { value: val }); | ||
}))) { | ||
if (!!vldrObj.onError) { | ||
vldrObj.onError(key, val, Errors.TestFn); | ||
} | ||
else { | ||
@@ -238,5 +251,2 @@ onError(key, val, Errors.TestFn); | ||
} | ||
if (key in arg) { | ||
arg[key] = val; | ||
} | ||
} | ||
@@ -246,26 +256,33 @@ return true; | ||
} | ||
function _setupParseFn(allVldtrsHolder, cloneFn, onError) { | ||
function _setupParseFn(allVldtrsHolder, safety = 'filter', onError) { | ||
return (arg) => { | ||
const retVal = {}; | ||
if (!(0, util_1.isObj)(arg)) { | ||
onError('', arg, Errors.ParseNotAnObj); | ||
return retVal; | ||
return arg; | ||
} | ||
for (const key in allVldtrsHolder) { | ||
const testFn = allVldtrsHolder[key].vf; | ||
for (const key in arg) { | ||
const vldrObj = allVldtrsHolder[key]; | ||
let val = arg[key]; | ||
if (!testFn(val, ((transVal) => val = transVal))) { | ||
if (!!allVldtrsHolder[key].onError) { | ||
allVldtrsHolder[key].onError(key, val, Errors.ParseFn); | ||
if (!vldrObj) { | ||
if (safety !== 'pass') { | ||
Reflect.deleteProperty(arg, key); | ||
} | ||
if (safety === 'strict') { | ||
onError(key, val, Errors.StrictMode); | ||
} | ||
continue; | ||
} | ||
if (!vldrObj.vf(val, (transVal => { | ||
val = transVal; | ||
Object.defineProperty(arg, key, { value: val }); | ||
}))) { | ||
if (!!vldrObj.onError) { | ||
vldrObj.onError(key, val, Errors.ParseFn); | ||
} | ||
else { | ||
onError(key, val, Errors.ParseFn); | ||
} | ||
retVal[key] = '**ERROR**'; | ||
} | ||
if (key in arg) { | ||
retVal[key] = cloneFn(val); | ||
} | ||
} | ||
return retVal; | ||
return arg; | ||
}; | ||
@@ -282,3 +299,3 @@ } | ||
function _processOptions(options) { | ||
let base = { init: true }; | ||
let base = { init: true, safety: 'filter' }; | ||
if (!(0, util_1.isUndef)(options === null || options === void 0 ? void 0 : options.init)) { | ||
@@ -290,2 +307,5 @@ base.init = options.init; | ||
} | ||
if (!!(options === null || options === void 0 ? void 0 : options.safety)) { | ||
base.safety = options.safety; | ||
} | ||
if (!(options === null || options === void 0 ? void 0 : options.nullish)) { | ||
@@ -292,0 +312,0 @@ base = Object.assign(Object.assign({}, base), { optional: !!(options === null || options === void 0 ? void 0 : options.optional), nullable: !!(options === null || options === void 0 ? void 0 : options.nullable) }); |
{ | ||
"name": "jet-schema", | ||
"version": "1.2.6", | ||
"version": "1.3.0", | ||
"description": "Simple, typescript-first schema validation tool", | ||
@@ -17,3 +17,2 @@ "main": "dist/index.js", | ||
"pre-publish": "mv README.md README-git && mv README-npm README.md", | ||
"publish": "npm run pre-publish && npm publish && npm run post-publish", | ||
"post-publish": "mv README.md README-npm && mv README-git README.md" | ||
@@ -20,0 +19,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63147
677