@oxog/vld 2.4.0 → 3.0.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/CHANGELOG.md +165 -1
- package/README.md +113 -1
- package/dist/chunks/errors-core-BU14-Dk8.js +1 -0
- package/dist/cjs/chunks/errors-core-C0mA6tfa.cjs +1 -0
- package/dist/cjs/cli/bin.cjs +1 -1
- package/dist/cjs/codecs/index.cjs +1 -1
- package/dist/cjs/coercion/index.cjs +1 -1
- package/dist/cjs/compile.cjs +1 -1
- package/dist/cjs/errors.cjs +1 -1
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/locales/af.cjs +1 -1
- package/dist/cjs/locales/ar.cjs +1 -1
- package/dist/cjs/locales/bn.cjs +1 -1
- package/dist/cjs/locales/da.cjs +1 -1
- package/dist/cjs/locales/de.cjs +1 -1
- package/dist/cjs/locales/en.cjs +1 -1
- package/dist/cjs/locales/es-MX.cjs +1 -1
- package/dist/cjs/locales/es.cjs +1 -1
- package/dist/cjs/locales/fi.cjs +1 -1
- package/dist/cjs/locales/fr.cjs +1 -1
- package/dist/cjs/locales/gu.cjs +1 -1
- package/dist/cjs/locales/hi.cjs +1 -1
- package/dist/cjs/locales/id.cjs +1 -1
- package/dist/cjs/locales/it.cjs +1 -1
- package/dist/cjs/locales/ja.cjs +1 -1
- package/dist/cjs/locales/kn.cjs +1 -1
- package/dist/cjs/locales/ko.cjs +1 -1
- package/dist/cjs/locales/ne.cjs +1 -1
- package/dist/cjs/locales/nl.cjs +1 -1
- package/dist/cjs/locales/no.cjs +1 -1
- package/dist/cjs/locales/pl.cjs +1 -1
- package/dist/cjs/locales/pt-BR-v4.cjs +1 -1
- package/dist/cjs/locales/pt-BR.cjs +1 -1
- package/dist/cjs/locales/pt.cjs +1 -1
- package/dist/cjs/locales/ru.cjs +1 -1
- package/dist/cjs/locales/sk.cjs +1 -1
- package/dist/cjs/locales/sv.cjs +1 -1
- package/dist/cjs/locales/sw.cjs +1 -1
- package/dist/cjs/locales/th.cjs +1 -1
- package/dist/cjs/locales/tr.cjs +1 -1
- package/dist/cjs/locales/vi.cjs +1 -1
- package/dist/cjs/locales/zh.cjs +1 -1
- package/dist/cjs/mini.cjs +1 -1
- package/dist/cjs/v3/index.cjs +1 -1
- package/dist/cjs/v4/core/index.cjs +1 -1
- package/dist/cjs/v4/index.cjs +1 -1
- package/dist/cjs/v4/mini/index.cjs +1 -1
- package/dist/cjs/v4-mini/index.cjs +1 -1
- package/dist/cjs/validators/any.cjs +1 -1
- package/dist/cjs/validators/array-v2.cjs +1 -0
- package/dist/cjs/validators/array.cjs +1 -1
- package/dist/cjs/validators/base.cjs +1 -1
- package/dist/cjs/validators/base64.cjs +1 -1
- package/dist/cjs/validators/bigint-v2.cjs +1 -0
- package/dist/cjs/validators/bigint.cjs +1 -1
- package/dist/cjs/validators/boolean.cjs +1 -1
- package/dist/cjs/validators/codec.cjs +1 -1
- package/dist/cjs/validators/composite-v2.cjs +1 -0
- package/dist/cjs/validators/custom.cjs +1 -1
- package/dist/cjs/validators/date-v2.cjs +1 -0
- package/dist/cjs/validators/date.cjs +1 -1
- package/dist/cjs/validators/discriminated-union.cjs +1 -1
- package/dist/cjs/validators/enum.cjs +1 -1
- package/dist/cjs/validators/file.cjs +1 -1
- package/dist/cjs/validators/function.cjs +1 -1
- package/dist/cjs/validators/hex.cjs +1 -1
- package/dist/cjs/validators/index.cjs +1 -1
- package/dist/cjs/validators/intersection.cjs +1 -1
- package/dist/cjs/validators/json.cjs +1 -1
- package/dist/cjs/validators/lazy.cjs +1 -1
- package/dist/cjs/validators/leaf-v2.cjs +1 -0
- package/dist/cjs/validators/literal.cjs +1 -1
- package/dist/cjs/validators/map.cjs +1 -1
- package/dist/cjs/validators/nan.cjs +1 -1
- package/dist/cjs/validators/never.cjs +1 -1
- package/dist/cjs/validators/null.cjs +1 -1
- package/dist/cjs/validators/number-v2.cjs +1 -0
- package/dist/cjs/validators/number.cjs +1 -1
- package/dist/cjs/validators/object-v2.cjs +1 -0
- package/dist/cjs/validators/object.cjs +1 -1
- package/dist/cjs/validators/promise.cjs +1 -1
- package/dist/cjs/validators/record.cjs +1 -1
- package/dist/cjs/validators/set.cjs +1 -1
- package/dist/cjs/validators/string-bool.cjs +1 -1
- package/dist/cjs/validators/string-formats.cjs +1 -1
- package/dist/cjs/validators/string-v2.cjs +1 -0
- package/dist/cjs/validators/string.cjs +1 -1
- package/dist/cjs/validators/symbol.cjs +1 -1
- package/dist/cjs/validators/template-literal.cjs +1 -1
- package/dist/cjs/validators/tuple.cjs +1 -1
- package/dist/cjs/validators/uint8array.cjs +1 -1
- package/dist/cjs/validators/undefined.cjs +1 -1
- package/dist/cjs/validators/union-v2.cjs +1 -0
- package/dist/cjs/validators/union.cjs +1 -1
- package/dist/cjs/validators/unknown.cjs +1 -1
- package/dist/cjs/validators/void.cjs +1 -1
- package/dist/cjs/validators/wrapper-v2.cjs +1 -0
- package/dist/cjs/validators/xor.cjs +1 -1
- package/dist/errors-core.d.ts +3 -0
- package/dist/errors.js +1 -1
- package/dist/index.d.ts +168 -12
- package/dist/index.js +1 -1
- package/dist/locales/af.js +1 -1
- package/dist/locales/ar.js +1 -1
- package/dist/locales/bn.js +1 -1
- package/dist/locales/da.js +1 -1
- package/dist/locales/de.js +1 -1
- package/dist/locales/en.js +1 -1
- package/dist/locales/es-MX.js +1 -1
- package/dist/locales/es.js +1 -1
- package/dist/locales/fi.js +1 -1
- package/dist/locales/fr.js +1 -1
- package/dist/locales/gu.js +1 -1
- package/dist/locales/hi.js +1 -1
- package/dist/locales/id.js +1 -1
- package/dist/locales/it.js +1 -1
- package/dist/locales/ja.js +1 -1
- package/dist/locales/kn.js +1 -1
- package/dist/locales/ko.js +1 -1
- package/dist/locales/ne.js +1 -1
- package/dist/locales/nl.js +1 -1
- package/dist/locales/no.js +1 -1
- package/dist/locales/pl.js +1 -1
- package/dist/locales/pt-BR-v4.js +1 -1
- package/dist/locales/pt-BR.js +1 -1
- package/dist/locales/pt.js +1 -1
- package/dist/locales/ru.js +1 -1
- package/dist/locales/sk.js +1 -1
- package/dist/locales/sv.js +1 -1
- package/dist/locales/sw.js +1 -1
- package/dist/locales/th.js +1 -1
- package/dist/locales/tr.js +1 -1
- package/dist/locales/types.d.ts +1 -0
- package/dist/locales/vi.js +1 -1
- package/dist/locales/zh.js +1 -1
- package/dist/v3/index.js +1 -1
- package/dist/v4/core/index.js +1 -1
- package/dist/v4/index.js +1 -1
- package/dist/v4/mini/index.js +1 -1
- package/dist/v4-mini/index.js +1 -1
- package/dist/validators/array-v2.d.ts +113 -0
- package/dist/validators/array-v2.js +1 -0
- package/dist/validators/array.js +1 -1
- package/dist/validators/base.js +1 -1
- package/dist/validators/base64.js +1 -1
- package/dist/validators/bigint-v2.d.ts +105 -0
- package/dist/validators/bigint-v2.js +1 -0
- package/dist/validators/bigint.js +1 -1
- package/dist/validators/boolean.js +1 -1
- package/dist/validators/codec.js +1 -1
- package/dist/validators/composite-v2.d.ts +59 -0
- package/dist/validators/composite-v2.js +1 -0
- package/dist/validators/date-v2.d.ts +140 -0
- package/dist/validators/date-v2.js +1 -0
- package/dist/validators/date.js +1 -1
- package/dist/validators/enum.js +1 -1
- package/dist/validators/file.js +1 -1
- package/dist/validators/function.js +1 -1
- package/dist/validators/hex.js +1 -1
- package/dist/validators/intersection.js +1 -1
- package/dist/validators/leaf-v2.d.ts +138 -0
- package/dist/validators/leaf-v2.js +1 -0
- package/dist/validators/literal.js +1 -1
- package/dist/validators/map.js +1 -1
- package/dist/validators/nan.js +1 -1
- package/dist/validators/never.js +1 -1
- package/dist/validators/null.js +1 -1
- package/dist/validators/number-v2.d.ts +252 -0
- package/dist/validators/number-v2.js +1 -0
- package/dist/validators/number.js +1 -1
- package/dist/validators/object-v2.d.ts +40 -0
- package/dist/validators/object-v2.js +1 -0
- package/dist/validators/object.js +1 -1
- package/dist/validators/record.js +1 -1
- package/dist/validators/set.js +1 -1
- package/dist/validators/string-formats.js +1 -1
- package/dist/validators/string-v2.d.ts +274 -0
- package/dist/validators/string-v2.js +1 -0
- package/dist/validators/string.js +1 -1
- package/dist/validators/symbol.js +1 -1
- package/dist/validators/tuple.js +1 -1
- package/dist/validators/uint8array.js +1 -1
- package/dist/validators/undefined.js +1 -1
- package/dist/validators/union-v2.d.ts +28 -0
- package/dist/validators/union-v2.js +1 -0
- package/dist/validators/void.js +1 -1
- package/dist/validators/wrapper-v2.d.ts +74 -0
- package/dist/validators/wrapper-v2.js +1 -0
- package/dist/zod-error.d.ts +84 -0
- package/package.json +1 -1
- package/dist/chunks/errors-core-CerMg2rq.js +0 -1
- package/dist/cjs/chunks/errors-core-DrydF46s.cjs +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{VldBase as t,VLD_VALIDATOR_TYPES as e}from"./base.js";import{V as s,c as i,g as h}from"../chunks/errors-core-BU14-Dk8.js";class VldBigIntCheck{}function n(t){return"bigint"==typeof t?t:BigInt(t)}class VldBigIntCheckMin extends VldBigIntCheck{constructor(t,e){super(),this._msg=e,this.kind="min",this._threshold=n(t)}check(t){return t<this._threshold?{code:"too_small",path:[],origin:"bigint",message:this._msg||`BigInt must be >= ${this._threshold}`}:null}meta(){return{kind:"min",value:this._threshold,message:this._msg}}}class VldBigIntCheckMax extends VldBigIntCheck{constructor(t,e){super(),this._msg=e,this.kind="max",this._threshold=n(t)}check(t){return t>this._threshold?{code:"too_big",path:[],origin:"bigint",message:this._msg||`BigInt must be <= ${this._threshold}`}:null}meta(){return{kind:"max",value:this._threshold,message:this._msg}}}class VldBigIntCheckGt extends VldBigIntCheck{constructor(t,e){super(),this._msg=e,this.kind="gt",this._threshold=n(t)}check(t){return t<=this._threshold?{code:"too_small",path:[],origin:"bigint",message:this._msg||`BigInt must be > ${this._threshold}`}:null}meta(){return{kind:"gt",value:this._threshold,message:this._msg}}}class VldBigIntCheckLt extends VldBigIntCheck{constructor(t,e){super(),this._msg=e,this.kind="lt",this._threshold=n(t)}check(t){return t>=this._threshold?{code:"too_big",path:[],origin:"bigint",message:this._msg||`BigInt must be < ${this._threshold}`}:null}meta(){return{kind:"lt",value:this._threshold,message:this._msg}}}class VldBigIntCheckMultipleOf extends VldBigIntCheck{constructor(t,e){super(),this._msg=e,this.kind="multipleOf",this._divisor=n(t)}check(t){return 0n===this._divisor?null:t%this._divisor!==0n?{code:"custom",path:[],message:this._msg||`BigInt must be a multiple of ${this._divisor}`}:null}meta(){return{kind:"multipleOf",value:this._divisor,message:this._msg}}}const c=Object.freeze({type:"bigint",checks:Object.freeze([]),isSimple:!0});class VldBigIntV2 extends t{constructor(t=c){super(e.BIGINT),this.__def=t}static create(){return new VldBigIntV2}get isSimple(){return this.__def.isSimple}parse(t){if("bigint"!=typeof t)throw new s([i("bigint",h(t),this.__def.errorMessage)]);return this.__def.isSimple?t:this.parseKnownBigInt(t)}parseKnownBigInt(t){const e=this.__def.checks;for(let i=0;i<e.length;i++){const h=e[i].check(t);if(null!==h)throw new s([h])}return t}safeParse(t){try{return{success:!0,data:this.parse(t)}}catch(t){return{success:!1,error:t instanceof s?t:new s([{code:"custom",path:[],message:String(t)}])}}}withDef(t){const e=(s=t.checks??this.__def.checks,i=t.jsonSchema??this.__def.jsonSchema,h=t.errorMessage??this.__def.errorMessage,Object.freeze({type:"bigint",checks:s,jsonSchema:i,errorMessage:h,isSimple:0===s.length}));var s,i,h;return new this.constructor(e)}min(t,e){const s=n(t);return this.withDef({type:"bigint",checks:[...this.__def.checks,new VldBigIntCheckMin(t,e)],jsonSchema:{...this.__def.jsonSchema,minimum:s.toString()}})}max(t,e){const s=n(t);return this.withDef({type:"bigint",checks:[...this.__def.checks,new VldBigIntCheckMax(t,e)],jsonSchema:{...this.__def.jsonSchema,maximum:s.toString()}})}positive(t){return this.withDef({type:"bigint",checks:[...this.__def.checks,new VldBigIntCheckGt(0n,t)]})}negative(t){return this.withDef({type:"bigint",checks:[...this.__def.checks,new VldBigIntCheckLt(0n,t)]})}nonnegative(t){return this.withDef({type:"bigint",checks:[...this.__def.checks,new VldBigIntCheckMin(0n,t)]})}nonpositive(t){return this.withDef({type:"bigint",checks:[...this.__def.checks,new VldBigIntCheckMax(0n,t)]})}gt(t,e){return this.withDef({type:"bigint",checks:[...this.__def.checks,new VldBigIntCheckGt(t,e)]})}lt(t,e){return this.withDef({type:"bigint",checks:[...this.__def.checks,new VldBigIntCheckLt(t,e)]})}gte(t,e){return this.min(t,e)}lte(t,e){return this.max(t,e)}multipleOf(t,e){const s=n(t);return this.withDef({type:"bigint",checks:[...this.__def.checks,new VldBigIntCheckMultipleOf(t,e)],jsonSchema:{...this.__def.jsonSchema,multipleOf:s.toString()}})}}export{VldBigIntCheck,VldBigIntCheckGt,VldBigIntCheckLt,VldBigIntCheckMax,VldBigIntCheckMin,VldBigIntCheckMultipleOf,VldBigIntV2};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{VldBase as e,VLD_VALIDATOR_TYPES as i}from"./base.js";import{getMessages as n}from"../locales/runtime.js";import{V as t}from"../chunks/errors-core-
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as i}from"./base.js";import{getMessages as n}from"../locales/runtime.js";import{V as t}from"../chunks/errors-core-BU14-Dk8.js";function s(e){return new t([{code:"invalid_type",path:[],message:e}])}class VldBigInt extends e{constructor(e){super(e?.validatorType||i.BIGINT),this.config={checks:e?.checks||[],errorMessage:e?.errorMessage,jsonSchema:e?.jsonSchema},this._checks=this.config.checks,this._fastCheckMode=this.detectFastCheckMode()}get jsonSchema(){return this.config.jsonSchema}get isSimple(){return 0===this._checks.length}get minValue(){return this.config.jsonSchema?.minimum?BigInt(this.config.jsonSchema.minimum):null}get maxValue(){return this.config.jsonSchema?.maximum?BigInt(this.config.jsonSchema.maximum):null}get format(){return this.config.jsonSchema?.format??null}static create(){return new VldBigInt}detectFastCheckMode(){if(0===this._checks.length)return"none";if(1!==this._checks.length)return;const e=this.config.jsonSchema;return 0n===e?.exclusiveMinimum&&void 0===e.minimum&&void 0===e.maximum?"positive":0n===e?.exclusiveMaximum&&void 0===e.minimum&&void 0===e.maximum?"negative":0n===e?.minimum&&void 0===e.maximum&&void 0===e.exclusiveMaximum?"nonnegative":0n===e?.maximum&&void 0===e.minimum&&void 0===e.exclusiveMinimum?"nonpositive":void 0}getValidationError(){return new Error(this.config.errorMessage||n().invalidBigint)}parse(e){if("bigint"!=typeof e)throw this.getValidationError();return this.parseKnownBigInt(e)}parseKnownBigInt(e){switch(this._fastCheckMode){case"none":return e;case"positive":if(e>0n)return e;throw this.getValidationError();case"negative":if(e<0n)return e;throw this.getValidationError();case"nonnegative":if(e>=0n)return e;throw this.getValidationError();case"nonpositive":if(e<=0n)return e;throw this.getValidationError()}for(const i of this._checks)if(!i(e))throw this.getValidationError();return e}safeParse(e){if("bigint"!=typeof e)return{success:!1,error:s(this.getValidationError().message)};try{return{success:!0,data:this.parseKnownBigInt(e)}}catch(e){return{success:!1,error:s(e.message)}}}min(e,i){return new VldBigInt({...this.config,checks:[...this.config.checks,i=>i>=e],errorMessage:i||`BigInt must be at least ${e}`,jsonSchema:{...this.config.jsonSchema,minimum:e}})}max(e,i){return new VldBigInt({...this.config,checks:[...this.config.checks,i=>i<=e],errorMessage:i||`BigInt must be at most ${e}`,jsonSchema:{...this.config.jsonSchema,maximum:e}})}positive(e){return new VldBigInt({...this.config,checks:[...this.config.checks,e=>e>0n],errorMessage:e||"BigInt must be positive",jsonSchema:{...this.config.jsonSchema,exclusiveMinimum:0n}})}negative(e){return new VldBigInt({...this.config,checks:[...this.config.checks,e=>e<0n],errorMessage:e||"BigInt must be negative",jsonSchema:{...this.config.jsonSchema,exclusiveMaximum:0n}})}nonnegative(e){return new VldBigInt({...this.config,checks:[...this.config.checks,e=>e>=0n],errorMessage:e||"BigInt must be non-negative",jsonSchema:{...this.config.jsonSchema,minimum:0n}})}nonpositive(e){return new VldBigInt({...this.config,checks:[...this.config.checks,e=>e<=0n],errorMessage:e||"BigInt must be non-positive",jsonSchema:{...this.config.jsonSchema,maximum:0n}})}gt(e,i){const n="bigint"==typeof e?e:BigInt(e);return new VldBigInt({...this.config,checks:[...this.config.checks,e=>e>n],errorMessage:i||`BigInt must be greater than ${n}`,jsonSchema:{...this.config.jsonSchema,exclusiveMinimum:n}})}lt(e,i){const n="bigint"==typeof e?e:BigInt(e);return new VldBigInt({...this.config,checks:[...this.config.checks,e=>e<n],errorMessage:i||`BigInt must be less than ${n}`,jsonSchema:{...this.config.jsonSchema,exclusiveMaximum:n}})}gte(e,i){const n="bigint"==typeof e?e:BigInt(e);return this.min(n,i)}lte(e,i){const n="bigint"==typeof e?e:BigInt(e);return this.max(n,i)}multipleOf(e,i){return new VldBigInt({...this.config,checks:[...this.config.checks,i=>i%e===0n],errorMessage:i||`BigInt must be a multiple of ${e}`,jsonSchema:{...this.config.jsonSchema,multipleOf:e}})}}export{VldBigInt};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{VldBase as e,VLD_VALIDATOR_TYPES as s}from"./base.js";import{V as r,c as t,g as a}from"../chunks/errors-core-
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as s}from"./base.js";import{V as r,c as t,g as a}from"../chunks/errors-core-BU14-Dk8.js";class VldBoolean extends e{constructor(e,r){super(r||s.BOOLEAN),this.errorMessage=e}get isSimple(){return!0}static create(){return new VldBoolean}parse(e){if("boolean"!=typeof e)throw new r([t("boolean",a(e),this.errorMessage)]);return e}parseKnownBoolean(e){return e}safeParse(e){return"boolean"==typeof e?{success:!0,data:e}:{success:!1,error:new r([t("boolean",a(e),this.errorMessage)])}}true(e){return new VldTrue(e||"Value must be true")}false(e){return new VldFalse(e||"Value must be false")}}class VldTrue extends VldBoolean{constructor(e){super(e),this.message=e}get isSimple(){return!1}parse(e){if(!0!==e)throw new r([{code:"custom",path:[],message:this.message}]);return!0}parseKnownBoolean(e){if(!0!==e)throw new r([{code:"custom",path:[],message:this.message}]);return!0}safeParse(e){return!0!==e?{success:!1,error:new r([{code:"custom",path:[],message:this.message}])}:{success:!0,data:!0}}}class VldFalse extends VldBoolean{constructor(e){super(e),this.message=e}get isSimple(){return!1}parse(e){if(!1!==e)throw new r([{code:"custom",path:[],message:this.message}]);return!1}parseKnownBoolean(e){if(!1!==e)throw new r([{code:"custom",path:[],message:this.message}]);return!1}safeParse(e){return!1!==e?{success:!1,error:new r([{code:"custom",path:[],message:this.message}])}:{success:!0,data:!1}}}export{VldBoolean};
|
package/dist/validators/codec.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{VldBase as r,VLD_VALIDATOR_TYPES as s}from"./base.js";import{createIssue as e}from"../errors.js";import{getMessages as t}from"../locales/runtime.js";import{V as c}from"../chunks/errors-core-
|
|
1
|
+
import{VldBase as r,VLD_VALIDATOR_TYPES as s}from"./base.js";import{createIssue as e}from"../errors.js";import{getMessages as t}from"../locales/runtime.js";import{V as c}from"../chunks/errors-core-BU14-Dk8.js";class VldCodec extends r{constructor(r,e,t){super(s.CODEC),this.inputValidator=r,this.outputValidator=e,this.codecTransform=t}parse(r){const s=this.safeParse(r);if(!s.success)throw s.error;return s.data}safeParse(r){const s=this.inputValidator.safeParse(r);if(!s.success)return{success:!1,error:s.error};try{const r=this.codecTransform.decode(s.data);if(r instanceof Promise)throw new c([e("custom",[],t().codecAsyncNotSupported)]);return this.outputValidator.safeParse(r)}catch(r){return r instanceof c?{success:!1,error:r}:{success:!1,error:new c([e("custom",[],t().codecDecodeFailed)])}}}invert(){return new VldCodec(this.outputValidator,this.inputValidator,{decode:this.codecTransform.encode,encode:this.codecTransform.decode})}encode(r){const s=this.safeEncode(r);if(!s.success)throw s.error;return s.data}safeEncode(r){const s=this.outputValidator.safeParse(r);if(!s.success)return{success:!1,error:s.error};try{const r=this.codecTransform.encode(s.data);if(r instanceof Promise)throw new c([e("custom",[],t().codecAsyncNotSupported)]);return this.inputValidator.safeParse(r)}catch(r){return r instanceof c?{success:!1,error:r}:{success:!1,error:new c([e("custom",[],t().codecEncodeFailed)])}}}async parseAsync(r){const s=await this.safeParseAsync(r);if(!s.success)throw s.error;return s.data}async safeParseAsync(r){const s=this.inputValidator.safeParse(r);if(!s.success)return{success:!1,error:s.error};try{const r=await this.codecTransform.decode(s.data);return this.outputValidator.safeParse(r)}catch(r){return r instanceof c?{success:!1,error:r}:{success:!1,error:new c([e("custom",[],t().codecDecodeFailed)])}}}async encodeAsync(r){const s=await this.safeEncodeAsync(r);if(!s.success)throw s.error;return s.data}async safeEncodeAsync(r){const s=this.outputValidator.safeParse(r);if(!s.success)return{success:!1,error:s.error};try{const r=await this.codecTransform.encode(s.data);return this.inputValidator.safeParse(r)}catch(r){return r instanceof c?{success:!1,error:r}:{success:!1,error:new c([e("custom",[],t().codecEncodeFailed)])}}}static create(r,s,e){return new VldCodec(r,s,e)}static invert(r){return r.invert()}}export{VldCodec};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* V2 composite validators: VldTupleV2, VldSetV2, VldMapV2, VldIntersectionV2.
|
|
3
|
+
* Single-def pattern applied to remaining composite types.
|
|
4
|
+
*/
|
|
5
|
+
import { VldBase, type ParseResult } from './base.js';
|
|
6
|
+
export interface VldTupleDef {
|
|
7
|
+
readonly type: 'tuple';
|
|
8
|
+
readonly validators: ReadonlyArray<VldBase<any, any>>;
|
|
9
|
+
readonly validatorTypes: ReadonlyArray<string>;
|
|
10
|
+
}
|
|
11
|
+
export declare class VldTupleV2<T extends readonly VldBase<any, any>[]> extends VldBase<unknown[], unknown[]> {
|
|
12
|
+
readonly __def: VldTupleDef;
|
|
13
|
+
constructor(validators: T);
|
|
14
|
+
static create<T extends readonly VldBase<any, any>[]>(...validators: T): VldTupleV2<T>;
|
|
15
|
+
parse(value: unknown): unknown[];
|
|
16
|
+
parseKnownTuple(value: unknown[]): unknown[];
|
|
17
|
+
safeParse(value: unknown): ParseResult<unknown[]>;
|
|
18
|
+
get items(): ReadonlyArray<VldBase<any, any>>;
|
|
19
|
+
}
|
|
20
|
+
export interface VldSetDef {
|
|
21
|
+
readonly type: 'set';
|
|
22
|
+
readonly valueValidator: VldBase<unknown, any>;
|
|
23
|
+
readonly errorMessage?: string | undefined;
|
|
24
|
+
readonly isSimple: boolean;
|
|
25
|
+
}
|
|
26
|
+
export declare class VldSetV2<T> extends VldBase<Set<T>, Set<T>> {
|
|
27
|
+
readonly __def: VldSetDef;
|
|
28
|
+
constructor(valueValidator: VldBase<unknown, T>, errorMessage?: string);
|
|
29
|
+
static create<T>(valueValidator: VldBase<unknown, T>): VldSetV2<T>;
|
|
30
|
+
parse(value: unknown): Set<T>;
|
|
31
|
+
safeParse(value: unknown): ParseResult<Set<T>>;
|
|
32
|
+
get isSimple(): boolean;
|
|
33
|
+
}
|
|
34
|
+
export interface VldMapDef {
|
|
35
|
+
readonly type: 'map';
|
|
36
|
+
readonly keyValidator: VldBase<unknown, any>;
|
|
37
|
+
readonly valueValidator: VldBase<unknown, any>;
|
|
38
|
+
readonly isSimple: boolean;
|
|
39
|
+
}
|
|
40
|
+
export declare class VldMapV2<K, V> extends VldBase<Map<K, V>, Map<K, V>> {
|
|
41
|
+
readonly __def: VldMapDef;
|
|
42
|
+
constructor(keyValidator: VldBase<unknown, K>, valueValidator: VldBase<unknown, V>);
|
|
43
|
+
static create<K, V>(keyValidator: VldBase<unknown, K>, valueValidator: VldBase<unknown, V>): VldMapV2<K, V>;
|
|
44
|
+
parse(value: unknown): Map<K, V>;
|
|
45
|
+
safeParse(value: unknown): ParseResult<Map<K, V>>;
|
|
46
|
+
get isSimple(): boolean;
|
|
47
|
+
}
|
|
48
|
+
export interface VldIntersectionDef {
|
|
49
|
+
readonly type: 'intersection';
|
|
50
|
+
readonly left: VldBase<any, any>;
|
|
51
|
+
readonly right: VldBase<any, any>;
|
|
52
|
+
}
|
|
53
|
+
export declare class VldIntersectionV2<A, B> extends VldBase<A & B, A & B> {
|
|
54
|
+
readonly __def: VldIntersectionDef;
|
|
55
|
+
constructor(left: VldBase<any, A>, right: VldBase<any, B>);
|
|
56
|
+
static create<A, B>(left: VldBase<any, A>, right: VldBase<any, B>): VldIntersectionV2<A, B>;
|
|
57
|
+
parse(value: unknown): A & B;
|
|
58
|
+
safeParse(value: unknown): ParseResult<A & B>;
|
|
59
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as t}from"./base.js";import{V as s,c as r,g as a}from"../chunks/errors-core-BU14-Dk8.js";class VldTupleV2 extends e{constructor(e){super(t.TUPLE),this.__def=Object.freeze({type:"tuple",validators:Object.freeze([...e]),validatorTypes:Object.freeze(e.map(e=>e.validatorType))})}static create(...e){return new VldTupleV2(e)}parse(e){if(!Array.isArray(e))throw new s([r("tuple",a(e),void 0)]);return this.parseKnownTuple(e)}parseKnownTuple(e){const t=this.__def.validators,s=t.length;if(e.length<s)throw new Error(`Tuple must have at least ${s} items, got ${e.length}`);const r=new Array(s);for(let a=0;a<s;a++)r[a]=t[a].parse(e[a]);return r}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:e instanceof s?e:new s([{code:"custom",path:[],message:String(e)}])}}}get items(){return this.__def.validators}}class VldSetV2 extends e{constructor(e,s){super(t.SET),this.__def=Object.freeze({type:"set",valueValidator:e,errorMessage:s,isSimple:!0===e.isSimple})}static create(e){return new VldSetV2(e)}parse(e){if(!(e instanceof Set))throw new s([r("set",a(e),this.__def.errorMessage)]);if(this.__def.isSimple)return e;const t=new Set;for(const s of e)t.add(this.__def.valueValidator.parse(s));return t}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:e instanceof s?e:new s([{code:"custom",path:[],message:String(e)}])}}}get isSimple(){return this.__def.isSimple}}class VldMapV2 extends e{constructor(e,s){super(t.MAP);const r=!0===e.isSimple,a=!0===s.isSimple;this.__def=Object.freeze({type:"map",keyValidator:e,valueValidator:s,isSimple:r&&a})}static create(e,t){return new VldMapV2(e,t)}parse(e){if(!(e instanceof Map))throw new s([r("map",a(e),void 0)]);if(this.__def.isSimple)return e;const t=new Map;for(const[s,r]of e)t.set(this.__def.keyValidator.parse(s),this.__def.valueValidator.parse(r));return t}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:e instanceof s?e:new s([{code:"custom",path:[],message:String(e)}])}}}get isSimple(){return this.__def.isSimple}}class VldIntersectionV2 extends e{constructor(e,s){super(t.INTERSECTION),this.__def=Object.freeze({type:"intersection",left:e,right:s})}static create(e,t){return new VldIntersectionV2(e,t)}parse(e){return{...this.__def.left.parse(e),...this.__def.right.parse(e)}}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:e instanceof s?e:new s([{code:"custom",path:[],message:String(e)}])}}}}export{VldIntersectionV2,VldMapV2,VldSetV2,VldTupleV2};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VldDateV2 — OPTIMIZED: check returns Issue | null.
|
|
3
|
+
*/
|
|
4
|
+
import { VldBase, type ErrorParam, type ParseResult } from './base.js';
|
|
5
|
+
import { type VldIssue } from '../errors-core.js';
|
|
6
|
+
export declare abstract class VldDateCheck {
|
|
7
|
+
abstract readonly kind: 'min' | 'max' | 'gt' | 'lt' | 'past' | 'future' | 'today' | 'weekday' | 'weekend';
|
|
8
|
+
abstract check(value: Date): VldIssue | null;
|
|
9
|
+
abstract meta(): Record<string, any>;
|
|
10
|
+
}
|
|
11
|
+
export declare class VldDateCheckMin extends VldDateCheck {
|
|
12
|
+
private _msg?;
|
|
13
|
+
readonly kind: "min";
|
|
14
|
+
private _threshold;
|
|
15
|
+
constructor(threshold: Date | string | number, _msg?: string | undefined);
|
|
16
|
+
check(value: Date): VldIssue | null;
|
|
17
|
+
meta(): {
|
|
18
|
+
kind: string;
|
|
19
|
+
value: Date;
|
|
20
|
+
message: string | undefined;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export declare class VldDateCheckMax extends VldDateCheck {
|
|
24
|
+
private _msg?;
|
|
25
|
+
readonly kind: "max";
|
|
26
|
+
private _threshold;
|
|
27
|
+
constructor(threshold: Date | string | number, _msg?: string | undefined);
|
|
28
|
+
check(value: Date): VldIssue | null;
|
|
29
|
+
meta(): {
|
|
30
|
+
kind: string;
|
|
31
|
+
value: Date;
|
|
32
|
+
message: string | undefined;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export declare class VldDateCheckGt extends VldDateCheck {
|
|
36
|
+
private _msg?;
|
|
37
|
+
readonly kind: "gt";
|
|
38
|
+
private _threshold;
|
|
39
|
+
constructor(threshold: Date | number, _msg?: string | undefined);
|
|
40
|
+
check(value: Date): VldIssue | null;
|
|
41
|
+
meta(): {
|
|
42
|
+
kind: string;
|
|
43
|
+
value: Date;
|
|
44
|
+
message: string | undefined;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export declare class VldDateCheckLt extends VldDateCheck {
|
|
48
|
+
private _msg?;
|
|
49
|
+
readonly kind: "lt";
|
|
50
|
+
private _threshold;
|
|
51
|
+
constructor(threshold: Date | number, _msg?: string | undefined);
|
|
52
|
+
check(value: Date): VldIssue | null;
|
|
53
|
+
meta(): {
|
|
54
|
+
kind: string;
|
|
55
|
+
value: Date;
|
|
56
|
+
message: string | undefined;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export declare class VldDateCheckPast extends VldDateCheck {
|
|
60
|
+
private _msg?;
|
|
61
|
+
readonly kind: "past";
|
|
62
|
+
constructor(_msg?: string | undefined);
|
|
63
|
+
check(value: Date): VldIssue | null;
|
|
64
|
+
meta(): {
|
|
65
|
+
kind: string;
|
|
66
|
+
message: string | undefined;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export declare class VldDateCheckFuture extends VldDateCheck {
|
|
70
|
+
private _msg?;
|
|
71
|
+
readonly kind: "future";
|
|
72
|
+
constructor(_msg?: string | undefined);
|
|
73
|
+
check(value: Date): VldIssue | null;
|
|
74
|
+
meta(): {
|
|
75
|
+
kind: string;
|
|
76
|
+
message: string | undefined;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
export declare class VldDateCheckToday extends VldDateCheck {
|
|
80
|
+
private _msg?;
|
|
81
|
+
readonly kind: "today";
|
|
82
|
+
constructor(_msg?: string | undefined);
|
|
83
|
+
check(value: Date): VldIssue | null;
|
|
84
|
+
meta(): {
|
|
85
|
+
kind: string;
|
|
86
|
+
message: string | undefined;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
export declare class VldDateCheckWeekday extends VldDateCheck {
|
|
90
|
+
private _msg?;
|
|
91
|
+
readonly kind: "weekday";
|
|
92
|
+
constructor(_msg?: string | undefined);
|
|
93
|
+
check(value: Date): VldIssue | null;
|
|
94
|
+
meta(): {
|
|
95
|
+
kind: string;
|
|
96
|
+
message: string | undefined;
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
export declare class VldDateCheckWeekend extends VldDateCheck {
|
|
100
|
+
private _msg?;
|
|
101
|
+
readonly kind: "weekend";
|
|
102
|
+
constructor(_msg?: string | undefined);
|
|
103
|
+
check(value: Date): VldIssue | null;
|
|
104
|
+
meta(): {
|
|
105
|
+
kind: string;
|
|
106
|
+
message: string | undefined;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
export interface VldDateDef {
|
|
110
|
+
readonly type: 'date';
|
|
111
|
+
readonly checks: ReadonlyArray<VldDateCheck>;
|
|
112
|
+
readonly jsonSchema?: {
|
|
113
|
+
minimum?: string;
|
|
114
|
+
maximum?: string;
|
|
115
|
+
format?: string;
|
|
116
|
+
} | undefined;
|
|
117
|
+
readonly errorMessage?: string | undefined;
|
|
118
|
+
readonly isSimple: boolean;
|
|
119
|
+
}
|
|
120
|
+
export declare class VldDateV2 extends VldBase<Date, Date> {
|
|
121
|
+
readonly __def: VldDateDef;
|
|
122
|
+
constructor(def?: VldDateDef);
|
|
123
|
+
static create(): VldDateV2;
|
|
124
|
+
get isSimple(): boolean;
|
|
125
|
+
parse(value: unknown): Date;
|
|
126
|
+
parseKnownDate(value: Date): Date;
|
|
127
|
+
safeParse(value: unknown): ParseResult<Date>;
|
|
128
|
+
protected withDef(def: Partial<VldDateDef> & {
|
|
129
|
+
type: 'date';
|
|
130
|
+
}): VldDateV2;
|
|
131
|
+
min(date: Date | string | number, message?: ErrorParam): VldDateV2;
|
|
132
|
+
max(date: Date | string | number, message?: ErrorParam): VldDateV2;
|
|
133
|
+
past(message?: ErrorParam): VldDateV2;
|
|
134
|
+
future(message?: ErrorParam): VldDateV2;
|
|
135
|
+
today(message?: ErrorParam): VldDateV2;
|
|
136
|
+
weekday(message?: ErrorParam): VldDateV2;
|
|
137
|
+
weekend(message?: ErrorParam): VldDateV2;
|
|
138
|
+
gt(value: Date | number, message?: ErrorParam): VldDateV2;
|
|
139
|
+
lt(value: Date | number, message?: ErrorParam): VldDateV2;
|
|
140
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as t}from"./base.js";import{V as s,c as h,g as a}from"../chunks/errors-core-BU14-Dk8.js";class VldDateCheck{}function r(e){return e instanceof Date?e:new Date(e)}class VldDateCheckMin extends VldDateCheck{constructor(e,t){super(),this._msg=t,this.kind="min",this._threshold=r(e)}check(e){return e.getTime()<this._threshold.getTime()?{code:"too_small",path:[],origin:"date",message:this._msg||`Date must be >= ${this._threshold.toISOString()}`}:null}meta(){return{kind:"min",value:this._threshold,message:this._msg}}}class VldDateCheckMax extends VldDateCheck{constructor(e,t){super(),this._msg=t,this.kind="max",this._threshold=r(e)}check(e){return e.getTime()>this._threshold.getTime()?{code:"too_big",path:[],origin:"date",message:this._msg||`Date must be <= ${this._threshold.toISOString()}`}:null}meta(){return{kind:"max",value:this._threshold,message:this._msg}}}class VldDateCheckGt extends VldDateCheck{constructor(e,t){super(),this._msg=t,this.kind="gt",this._threshold=r(e)}check(e){return e.getTime()<=this._threshold.getTime()?{code:"too_small",path:[],origin:"date",message:this._msg||`Date must be > ${this._threshold.toISOString()}`}:null}meta(){return{kind:"gt",value:this._threshold,message:this._msg}}}class VldDateCheckLt extends VldDateCheck{constructor(e,t){super(),this._msg=t,this.kind="lt",this._threshold=r(e)}check(e){return e.getTime()>=this._threshold.getTime()?{code:"too_big",path:[],origin:"date",message:this._msg||`Date must be < ${this._threshold.toISOString()}`}:null}meta(){return{kind:"lt",value:this._threshold,message:this._msg}}}class VldDateCheckPast extends VldDateCheck{constructor(e){super(),this._msg=e,this.kind="past"}check(e){return e.getTime()>=Date.now()?{code:"custom",path:[],message:this._msg||"Date must be in the past"}:null}meta(){return{kind:"past",message:this._msg}}}class VldDateCheckFuture extends VldDateCheck{constructor(e){super(),this._msg=e,this.kind="future"}check(e){return e.getTime()<=Date.now()?{code:"custom",path:[],message:this._msg||"Date must be in the future"}:null}meta(){return{kind:"future",message:this._msg}}}class VldDateCheckToday extends VldDateCheck{constructor(e){super(),this._msg=e,this.kind="today"}check(e){const t=new Date;return e.toDateString()!==t.toDateString()?{code:"custom",path:[],message:this._msg||"Date must be today"}:null}meta(){return{kind:"today",message:this._msg}}}class VldDateCheckWeekday extends VldDateCheck{constructor(e){super(),this._msg=e,this.kind="weekday"}check(e){const t=e.getDay();return 0===t||6===t?{code:"custom",path:[],message:this._msg||"Date must be a weekday"}:null}meta(){return{kind:"weekday",message:this._msg}}}class VldDateCheckWeekend extends VldDateCheck{constructor(e){super(),this._msg=e,this.kind="weekend"}check(e){const t=e.getDay();return 0!==t&&6!==t?{code:"custom",path:[],message:this._msg||"Date must be a weekend"}:null}meta(){return{kind:"weekend",message:this._msg}}}const c=Object.freeze({type:"date",checks:Object.freeze([]),isSimple:!0});class VldDateV2 extends e{constructor(e=c){super(t.DATE),this.__def=e}static create(){return new VldDateV2}get isSimple(){return this.__def.isSimple}parse(e){let t;if(e instanceof Date){if(Number.isNaN(e.getTime()))throw new s([h("date","Invalid Date",this.__def.errorMessage)]);t=e}else{if("string"!=typeof e&&"number"!=typeof e)throw new s([h("date",a(e),this.__def.errorMessage)]);if(t=new Date(e),Number.isNaN(t.getTime()))throw new s([h("date",a(e),this.__def.errorMessage)])}return this.__def.isSimple?t:this.parseKnownDate(t)}parseKnownDate(e){const t=this.__def.checks;for(let h=0;h<t.length;h++){const a=t[h].check(e);if(null!==a)throw new s([a])}return e}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:e instanceof s?e:new s([{code:"custom",path:[],message:String(e)}])}}}withDef(e){const t=(s=e.checks??this.__def.checks,h=e.jsonSchema??this.__def.jsonSchema,a=e.errorMessage??this.__def.errorMessage,Object.freeze({type:"date",checks:s,jsonSchema:h,errorMessage:a,isSimple:0===s.length}));var s,h,a;return new this.constructor(t)}min(e,t){return this.withDef({type:"date",checks:[...this.__def.checks,new VldDateCheckMin(e,t)],jsonSchema:{...this.__def.jsonSchema,minimum:r(e).toISOString()}})}max(e,t){return this.withDef({type:"date",checks:[...this.__def.checks,new VldDateCheckMax(e,t)],jsonSchema:{...this.__def.jsonSchema,maximum:r(e).toISOString()}})}past(e){return this.withDef({type:"date",checks:[...this.__def.checks,new VldDateCheckPast(e)]})}future(e){return this.withDef({type:"date",checks:[...this.__def.checks,new VldDateCheckFuture(e)]})}today(e){return this.withDef({type:"date",checks:[...this.__def.checks,new VldDateCheckToday(e)]})}weekday(e){return this.withDef({type:"date",checks:[...this.__def.checks,new VldDateCheckWeekday(e)]})}weekend(e){return this.withDef({type:"date",checks:[...this.__def.checks,new VldDateCheckWeekend(e)]})}gt(e,t){return this.withDef({type:"date",checks:[...this.__def.checks,new VldDateCheckGt(e,t)]})}lt(e,t){return this.withDef({type:"date",checks:[...this.__def.checks,new VldDateCheckLt(e,t)]})}}export{VldDateCheck,VldDateCheckFuture,VldDateCheckGt,VldDateCheckLt,VldDateCheckMax,VldDateCheckMin,VldDateCheckPast,VldDateCheckToday,VldDateCheckWeekday,VldDateCheckWeekend,VldDateV2};
|
package/dist/validators/date.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{VldBase as e,VLD_VALIDATOR_TYPES as t}from"./base.js";import{getMessages as n}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as t}from"./base.js";import{getMessages as n}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-BU14-Dk8.js";function i(e){return new s([{code:"invalid_date",path:[],message:e}])}class VldDate extends e{constructor(e){super(e?.validatorType||t.DATE),this.config={checks:e?.checks||[],errorMessage:e?.errorMessage,jsonSchema:e?.jsonSchema},this._checks=this.config.checks,this._isSimple=0===this._checks.length}get jsonSchema(){return this.config.jsonSchema}get minDate(){const e=this.config.jsonSchema?.formatMinimum??this.config.jsonSchema?.minimum;return e?new Date(e):null}get maxDate(){const e=this.config.jsonSchema?.formatMaximum??this.config.jsonSchema?.maximum;return e?new Date(e):null}static create(){return new VldDate}parse(e){let t;if(e instanceof Date)t=e;else{if("string"!=typeof e&&"number"!=typeof e)throw new Error(this.config.errorMessage||n().invalidDate);t=new Date(e)}if(isNaN(t.getTime()))throw new Error(this.config.errorMessage||n().invalidDate);return this.parseValidDate(t)}parseKnownDate(e){if(isNaN(e.getTime()))throw new Error(this.config.errorMessage||n().invalidDate);return this.parseValidDate(e)}parseValidDate(e){if(this._isSimple)return e;for(const t of this._checks)if(!t.fn(e))throw new Error(t.message);return e}safeParse(e){if(e instanceof Date)try{return{success:!0,data:this.parseKnownDate(e)}}catch(e){return{success:!1,error:i(e.message)}}try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:i(e.message)}}}min(e,t){const s=e instanceof Date?e:new Date(e);if(isNaN(s.getTime()))throw new Error(`Invalid date provided to min(): ${e}`);return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e>=s,message:t||n().dateMin(s)}],jsonSchema:{...this.config.jsonSchema,formatMinimum:s.toISOString()}})}max(e,t){const s=e instanceof Date?e:new Date(e);if(isNaN(s.getTime()))throw new Error(`Invalid date provided to max(): ${e}`);return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e<=s,message:t||n().dateMax(s)}],jsonSchema:{...this.config.jsonSchema,formatMaximum:s.toISOString()}})}between(e,t,n){const s=e instanceof Date?e:new Date(e),i=t instanceof Date?t:new Date(t);if(isNaN(s.getTime()))throw new Error(`Invalid min date provided to between(): ${e}`);if(isNaN(i.getTime()))throw new Error(`Invalid max date provided to between(): ${t}`);return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e>=s&&e<=i,message:n||`Date must be between ${s.toISOString()} and ${i.toISOString()}`}],jsonSchema:{...this.config.jsonSchema,formatMinimum:s.toISOString(),formatMaximum:i.toISOString()}})}past(e){const t=new Date;return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e<t,message:e||"Date must be in the past"}],jsonSchema:{...this.config.jsonSchema,formatExclusiveMaximum:t.toISOString()}})}future(e){const t=new Date;return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e>t,message:e||"Date must be in the future"}],jsonSchema:{...this.config.jsonSchema,formatExclusiveMinimum:t.toISOString()}})}today(e){const t=new Date,n=t.getFullYear(),s=t.getMonth(),i=t.getDate();return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e.getFullYear()===n&&e.getMonth()===s&&e.getDate()===i,message:e||"Date must be today"}]})}weekday(e){return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>{const t=e.getDay();return t>=1&&t<=5},message:e||"Date must be a weekday"}]})}weekend(e){return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>{const t=e.getDay();return 0===t||6===t},message:e||"Date must be a weekend"}]})}gt(e,t){const n=e instanceof Date?e:new Date(e);return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e.getTime()>n.getTime(),message:t||`Date must be greater than ${n.toISOString()}`}],jsonSchema:{...this.config.jsonSchema,formatExclusiveMinimum:n.toISOString()}})}lt(e,t){const n=e instanceof Date?e:new Date(e);return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e.getTime()<n.getTime(),message:t||`Date must be less than ${n.toISOString()}`}],jsonSchema:{...this.config.jsonSchema,formatExclusiveMaximum:n.toISOString()}})}}export{VldDate};
|
package/dist/validators/enum.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{getMessages as s}from"../locales/runtime.js";import{V as t}from"../chunks/errors-core-
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{getMessages as s}from"../locales/runtime.js";import{V as t}from"../chunks/errors-core-BU14-Dk8.js";class VldEnum extends e{constructor(e,s){super(r.ENUM),this._values=e,this.errorMessage=s,this._valueSet=e.length>6?new Set(e):void 0}createError(e){const r={code:"invalid_value",path:[],values:[...this._values],message:this.errorMessage||s().enumExpected([...this._values],JSON.stringify(e))};return new t([r])}get values(){return this._values}get options(){return this._values}get enum(){const e={};for(const r of this._values)e[r]=r;return e}static create(e){return new VldEnum(e)}parse(e){if("string"!=typeof e&&"number"!=typeof e)throw this.createError(e);const r=this._values;switch(r.length){case 1:if(e===r[0])return e;break;case 2:if(e===r[0]||e===r[1])return e;break;case 3:if(e===r[0]||e===r[1]||e===r[2])return e;break;case 4:if(e===r[0]||e===r[1]||e===r[2]||e===r[3])return e;break;case 5:if(e===r[0]||e===r[1]||e===r[2]||e===r[3]||e===r[4])return e;break;case 6:if(e===r[0]||e===r[1]||e===r[2]||e===r[3]||e===r[4]||e===r[5])return e;break;default:if(this._valueSet.has(e))return e}throw this.createError(e)}safeParse(e){if("string"!=typeof e&&"number"!=typeof e)return{success:!1,error:this.createError(e)};const r=this._values;switch(r.length){case 1:if(e===r[0])return{success:!0,data:e};break;case 2:if(e===r[0]||e===r[1])return{success:!0,data:e};break;case 3:if(e===r[0]||e===r[1]||e===r[2])return{success:!0,data:e};break;case 4:if(e===r[0]||e===r[1]||e===r[2]||e===r[3])return{success:!0,data:e};break;case 5:if(e===r[0]||e===r[1]||e===r[2]||e===r[3]||e===r[4])return{success:!0,data:e};break;case 6:if(e===r[0]||e===r[1]||e===r[2]||e===r[3]||e===r[4]||e===r[5])return{success:!0,data:e};break;default:if(this._valueSet.has(e))return{success:!0,data:e}}return{success:!1,error:this.createError(e)}}exclude(...e){const r=1===e.length&&Array.isArray(e[0])?e[0]:e,s=this._values.filter(e=>!r.includes(e));if(0===s.length)throw new Error("Cannot exclude all enum values");return new VldEnum(s,this.errorMessage)}extract(...e){const r=1===e.length&&Array.isArray(e[0])?e[0]:e,s=this._values.filter(e=>r.includes(e));if(0===s.length)throw new Error("Cannot extract non-existent enum values");return new VldEnum(s,this.errorMessage)}}export{VldEnum};
|
package/dist/validators/file.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{VldBase as e,VLD_VALIDATOR_TYPES as s}from"./base.js";import{getMessages as i}from"../locales/runtime.js";import{V as r}from"../chunks/errors-core-
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as s}from"./base.js";import{getMessages as i}from"../locales/runtime.js";import{V as r}from"../chunks/errors-core-BU14-Dk8.js";class VldFile extends e{constructor(e=[]){super(s.FILE),this.checks=e}static create(){return new VldFile}isFileLike(e){return"object"==typeof e&&null!==e&&("undefined"!=typeof File&&e instanceof File||"size"in e&&"type"in e)}parse(e){if(this.isFileLike(e))return this.parseKnownFile(e);if("undefined"==typeof File)throw new Error(i().fileNotSupported||"File API not supported in this environment");throw new Error(i().invalidFile||"Expected a File object")}parseKnownFile(e){return this.runChecks(e)}runChecks(e){const s=i();for(const i of this.checks)switch(i.type){case"min":if(e.size<i.value)throw new Error(i.message||s.fileMinSize?.(i.value)||`File size must be at least ${i.value} bytes`);break;case"max":if(e.size>i.value)throw new Error(i.message||s.fileMaxSize?.(i.value)||`File size must not exceed ${i.value} bytes`);break;case"mime":if(!i.value.includes(e.type))throw new Error(i.message||s.fileMimeType?.(i.value)||`Invalid file type. Expected: ${i.value.join(", ")}`)}return e}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:(s=e.message,new r([{code:"invalid_type",path:[],message:s}]))}}var s}min(e,s){return new VldFile([...this.checks,{type:"min",value:e,message:s}])}max(e,s){return new VldFile([...this.checks,{type:"max",value:e,message:s}])}mime(e,s){const i=Array.isArray(e)?e:[e];return new VldFile([...this.checks,{type:"mime",value:i,message:s}])}size(e,s,i){return this.min(e,i).max(s,i)}}function t(){return VldFile.create()}export{VldFile,t as file};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{VldBase as r,VLD_VALIDATOR_TYPES as e}from"./base.js";import{getMessages as t}from"../locales/runtime.js";import{V as n}from"../chunks/errors-core-
|
|
1
|
+
import{VldBase as r,VLD_VALIDATOR_TYPES as e}from"./base.js";import{getMessages as t}from"../locales/runtime.js";import{V as n}from"../chunks/errors-core-BU14-Dk8.js";class VldFunction extends r{constructor(){super(e.FUNCTION)}static create(){return new VldFunction}parse(r){if("function"!=typeof r)throw new Error(t().invalidFunction||"Expected a function");return r}parseKnownFunction(r){return r}safeParse(r){try{return{success:!0,data:this.parse(r)}}catch(r){return{success:!1,error:(e=r.message,new n([{code:"invalid_type",path:[],message:e}]))}}var e}}function s(){return VldFunction.create()}export{VldFunction,s as functionValidator};
|
package/dist/validators/hex.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{createIssue as s}from"../errors.js";import{getMessages as t}from"../locales/runtime.js";import{V as o}from"../chunks/errors-core-
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{createIssue as s}from"../errors.js";import{getMessages as t}from"../locales/runtime.js";import{V as o}from"../chunks/errors-core-BU14-Dk8.js";class VldHex extends e{constructor(e=!1){super(r.HEX),this.lowercase=e}parse(e){const r=this.safeParse(e);if(!r.success)throw r.error;return r.data}safeParse(e){if("string"!=typeof e)return{success:!1,error:new o([s("invalid_type",[],t().expectedString)])};if(!VldHex.HEX_REGEX.test(e))return{success:!1,error:new o([s("custom",[],t().invalidHex)])};if(e.length%2!=0)return{success:!1,error:new o([s("custom",[],t().invalidHex)])};return{success:!0,data:this.lowercase?e.toLowerCase():e}}lowercaseMode(){return new VldHex(!0)}static create(e=!1){return new VldHex(e)}}VldHex.HEX_REGEX=/^[0-9a-fA-F]*$/;export{VldHex};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{VldBase as t,VLD_VALIDATOR_TYPES as e}from"./base.js";import{i as r}from"../chunks/security-Dy24T8N7.js";import{getMessages as o}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-
|
|
1
|
+
import{VldBase as t,VLD_VALIDATOR_TYPES as e}from"./base.js";import{i as r}from"../chunks/security-Dy24T8N7.js";import{getMessages as o}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-BU14-Dk8.js";function n(t){return null!==t&&"object"==typeof t&&t.constructor===Object&&"[object Object]"===Object.prototype.toString.call(t)}function c(t,e){const o={...t},s=Object.keys(e);for(let i=0;i<s.length;i++){const a=s[i];if(r(a))continue;if(!Object.prototype.hasOwnProperty.call(e,a))continue;const u=e[a],p=t[a];n(u)&&n(p)?o[a]=c(p,u):o[a]=u}return o}class VldIntersection extends t{constructor(t,r){super(e.INTERSECTION),this.validatorA=t,this.validatorB=r}static create(t,e){return new VldIntersection(t,e)}parse(t){try{const e=this.validatorA.parse(t),r=this.validatorB.parse(t),o=n(e),s=n(r);if(o&&s)return c(e,r);if(!o&&!s){if(e===r)return e;throw new Error("Values must be identical for intersection of primitive types")}throw new Error("Cannot create intersection of object and primitive types. Both validators must produce the same type category.")}catch(t){throw new Error(o().intersectionError(t.message))}}safeParse(t){try{return{success:!0,data:this.parse(t)}}catch(t){return{success:!1,error:(e=t.message,new s([{code:"invalid_type",path:[],message:e}]))}}var e}}export{VldIntersection};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* V2 leaf validators: VldLiteralV2, VldBooleanV2, VldEnumV2, VldRecordV2,
|
|
3
|
+
* VldAnyV2, VldUnknownV2, VldVoidV2, VldNeverV2, VldNullV2, VldUndefinedV2,
|
|
4
|
+
* VldSymbolV2, VldNullishV2, VldFunctionV2.
|
|
5
|
+
*/
|
|
6
|
+
import { VldBase, type ParseResult } from './base.js';
|
|
7
|
+
export interface VldLiteralDef<T> {
|
|
8
|
+
readonly type: 'literal';
|
|
9
|
+
readonly value: T | undefined;
|
|
10
|
+
}
|
|
11
|
+
export declare class VldLiteralV2<T extends string | number | boolean | null | undefined> extends VldBase<T, T> {
|
|
12
|
+
readonly __def: VldLiteralDef<T>;
|
|
13
|
+
constructor(value: T);
|
|
14
|
+
static create<T extends string | number | boolean | null | undefined>(value: T): VldLiteralV2<T>;
|
|
15
|
+
parse(value: unknown): T;
|
|
16
|
+
safeParse(value: unknown): ParseResult<T>;
|
|
17
|
+
get literal(): T;
|
|
18
|
+
get isSimple(): boolean;
|
|
19
|
+
}
|
|
20
|
+
export declare class VldBooleanV2 extends VldBase<boolean, boolean> {
|
|
21
|
+
readonly __def: {
|
|
22
|
+
type: 'boolean';
|
|
23
|
+
};
|
|
24
|
+
constructor();
|
|
25
|
+
static create(): VldBooleanV2;
|
|
26
|
+
parse(value: unknown): boolean;
|
|
27
|
+
safeParse(value: unknown): ParseResult<boolean>;
|
|
28
|
+
get isSimple(): boolean;
|
|
29
|
+
}
|
|
30
|
+
export interface VldEnumDef<T extends readonly (string | number)[]> {
|
|
31
|
+
readonly type: 'enum';
|
|
32
|
+
readonly values: ReadonlyArray<T[number]>;
|
|
33
|
+
readonly valuesSet: Set<T[number]>;
|
|
34
|
+
}
|
|
35
|
+
export declare class VldEnumV2<T extends readonly (string | number)[]> extends VldBase<T[number], T[number]> {
|
|
36
|
+
readonly __def: VldEnumDef<T>;
|
|
37
|
+
constructor(values: T);
|
|
38
|
+
static create<T extends readonly (string | number)[]>(values: T): VldEnumV2<T>;
|
|
39
|
+
parse(value: unknown): T[number];
|
|
40
|
+
safeParse(value: unknown): ParseResult<T[number]>;
|
|
41
|
+
get options(): T[number][];
|
|
42
|
+
get enumValues(): T[number][];
|
|
43
|
+
get isSimple(): boolean;
|
|
44
|
+
}
|
|
45
|
+
export interface VldRecordDef<T> {
|
|
46
|
+
readonly type: 'record';
|
|
47
|
+
readonly valueValidator: VldBase<unknown, T>;
|
|
48
|
+
readonly simpleValueMode: 'string' | 'number' | 'boolean' | 'bigint' | 'symbol' | undefined;
|
|
49
|
+
}
|
|
50
|
+
export declare class VldRecordV2<T> extends VldBase<Record<string, T>, Record<string, T>> {
|
|
51
|
+
readonly __def: VldRecordDef<T>;
|
|
52
|
+
constructor(valueValidator: VldBase<unknown, T>);
|
|
53
|
+
static create<T>(valueValidator: VldBase<unknown, T>): VldRecordV2<T>;
|
|
54
|
+
parse(value: unknown): Record<string, T>;
|
|
55
|
+
parseKnownRecord(value: Record<string, unknown>): Record<string, T>;
|
|
56
|
+
safeParse(value: unknown): ParseResult<Record<string, T>>;
|
|
57
|
+
get isSimple(): boolean;
|
|
58
|
+
}
|
|
59
|
+
export declare class VldAnyV2 extends VldBase<any, any> {
|
|
60
|
+
readonly __def: {
|
|
61
|
+
type: 'any';
|
|
62
|
+
};
|
|
63
|
+
constructor();
|
|
64
|
+
static create(): VldAnyV2;
|
|
65
|
+
parse(value: unknown): any;
|
|
66
|
+
safeParse(value: unknown): ParseResult<any>;
|
|
67
|
+
get isSimple(): boolean;
|
|
68
|
+
}
|
|
69
|
+
export declare class VldUnknownV2 extends VldBase<unknown, unknown> {
|
|
70
|
+
readonly __def: {
|
|
71
|
+
type: 'unknown';
|
|
72
|
+
};
|
|
73
|
+
constructor();
|
|
74
|
+
static create(): VldUnknownV2;
|
|
75
|
+
parse(value: unknown): unknown;
|
|
76
|
+
safeParse(value: unknown): ParseResult<unknown>;
|
|
77
|
+
get isSimple(): boolean;
|
|
78
|
+
}
|
|
79
|
+
export declare class VldVoidV2 extends VldBase<undefined, undefined> {
|
|
80
|
+
readonly __def: {
|
|
81
|
+
type: 'void';
|
|
82
|
+
};
|
|
83
|
+
constructor();
|
|
84
|
+
static create(): VldVoidV2;
|
|
85
|
+
parse(value: unknown): undefined;
|
|
86
|
+
safeParse(value: unknown): ParseResult<undefined>;
|
|
87
|
+
get isSimple(): boolean;
|
|
88
|
+
}
|
|
89
|
+
export declare class VldNeverV2 extends VldBase<never, never> {
|
|
90
|
+
readonly __def: {
|
|
91
|
+
type: 'never';
|
|
92
|
+
};
|
|
93
|
+
constructor();
|
|
94
|
+
static create(): VldNeverV2;
|
|
95
|
+
parse(_value: unknown): never;
|
|
96
|
+
safeParse(_value: unknown): ParseResult<never>;
|
|
97
|
+
get isSimple(): boolean;
|
|
98
|
+
}
|
|
99
|
+
export declare class VldNullV2 extends VldBase<null, null> {
|
|
100
|
+
readonly __def: {
|
|
101
|
+
type: 'null';
|
|
102
|
+
};
|
|
103
|
+
constructor();
|
|
104
|
+
static create(): VldNullV2;
|
|
105
|
+
parse(value: unknown): null;
|
|
106
|
+
safeParse(value: unknown): ParseResult<null>;
|
|
107
|
+
get isSimple(): boolean;
|
|
108
|
+
}
|
|
109
|
+
export declare class VldUndefinedV2 extends VldBase<undefined, undefined> {
|
|
110
|
+
readonly __def: {
|
|
111
|
+
type: 'undefined';
|
|
112
|
+
};
|
|
113
|
+
constructor();
|
|
114
|
+
static create(): VldUndefinedV2;
|
|
115
|
+
parse(value: unknown): undefined;
|
|
116
|
+
safeParse(value: unknown): ParseResult<undefined>;
|
|
117
|
+
get isSimple(): boolean;
|
|
118
|
+
}
|
|
119
|
+
export declare class VldSymbolV2 extends VldBase<symbol, symbol> {
|
|
120
|
+
readonly __def: {
|
|
121
|
+
type: 'symbol';
|
|
122
|
+
};
|
|
123
|
+
constructor();
|
|
124
|
+
static create(): VldSymbolV2;
|
|
125
|
+
parse(value: unknown): symbol;
|
|
126
|
+
safeParse(value: unknown): ParseResult<symbol>;
|
|
127
|
+
get isSimple(): boolean;
|
|
128
|
+
}
|
|
129
|
+
export declare class VldFunctionV2 extends VldBase<Function, (...args: any[]) => any> {
|
|
130
|
+
readonly __def: {
|
|
131
|
+
type: 'function';
|
|
132
|
+
};
|
|
133
|
+
constructor();
|
|
134
|
+
static create(): VldFunctionV2;
|
|
135
|
+
parse(value: unknown): (...args: any[]) => any;
|
|
136
|
+
safeParse(value: unknown): ParseResult<(...args: any[]) => any>;
|
|
137
|
+
get isSimple(): boolean;
|
|
138
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{V as t,c as s,g as n}from"../chunks/errors-core-BU14-Dk8.js";class VldLiteralV2 extends e{constructor(e){super(r.LITERAL),this.__def=Object.freeze({type:"literal",value:e})}static create(e){return new VldLiteralV2(e)}parse(e){const r=this.__def.value;if(e!==r)throw new t([{code:"invalid_literal",path:[],expected:String(r),received:void 0===e?"undefined":typeof e,message:`Invalid input: expected ${JSON.stringify(r)}, received ${JSON.stringify(e)}`}]);return r}safeParse(e){const r=this.__def.value;return e===r?{success:!0,data:r}:{success:!1,error:new t([{code:"invalid_literal",path:[],expected:String(r),received:void 0===e?"undefined":typeof e,message:`Invalid input: expected ${JSON.stringify(r)}, received ${JSON.stringify(e)}`}])}}get literal(){return this.__def.value}get isSimple(){return!0}}class VldBooleanV2 extends e{constructor(){super(r.BOOLEAN),this.__def=Object.freeze({type:"boolean"})}static create(){return new VldBooleanV2}parse(e){if("boolean"!=typeof e)throw new t([s("boolean",n(e),void 0)]);return e}safeParse(e){return"boolean"==typeof e?{success:!0,data:e}:{success:!1,error:new t([s("boolean",n(e),void 0)])}}get isSimple(){return!0}}class VldEnumV2 extends e{constructor(e){super(r.ENUM),this.__def=Object.freeze({type:"enum",values:Object.freeze([...e]),valuesSet:new Set(e)})}static create(e){return new VldEnumV2(e)}parse(e){if(!this.__def.valuesSet.has(e))throw new t([{code:"invalid_value",path:[],values:[...this.__def.values],received:typeof e,message:`Invalid enum value. Expected ${[...this.__def.values].join(" | ")}, received ${JSON.stringify(e)}`}]);return e}safeParse(e){return this.__def.valuesSet.has(e)?{success:!0,data:e}:{success:!1,error:new t([{code:"invalid_value",path:[],values:[...this.__def.values],received:typeof e,message:`Invalid enum value. Expected ${[...this.__def.values].join(" | ")}, received ${JSON.stringify(e)}`}])}}get options(){return[...this.__def.values]}get enumValues(){return this.options}get isSimple(){return!0}}class VldRecordV2 extends e{constructor(e){super(r.RECORD);const t=!0===e.isSimple?e.validatorType===r.STRING?"string":e.validatorType===r.NUMBER?"number":e.validatorType===r.BOOLEAN?"boolean":e.validatorType===r.BIGINT?"bigint":e.validatorType===r.SYMBOL?"symbol":void 0:void 0;this.__def=Object.freeze({type:"record",valueValidator:e,simpleValueMode:t})}static create(e){return new VldRecordV2(e)}parse(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new t([s("record",n(e),void 0)]);return this.parseKnownRecord(e)}parseKnownRecord(e){const r=this.__def.simpleValueMode,t={},s=Object.keys(e);if(void 0!==r)for(let n=0;n<s.length;n++){const i=s[n],a=e[i];switch(r){case"string":if("string"!=typeof a)throw new Error(`Invalid field ${i}: expected string, received ${typeof a}`);t[i]=a;break;case"number":if("number"!=typeof a||isNaN(a))throw new Error(`Invalid field ${i}: expected number, received ${typeof a}`);t[i]=a;break;case"boolean":if("boolean"!=typeof a)throw new Error(`Invalid field ${i}: expected boolean, received ${typeof a}`);t[i]=a;break;case"bigint":if("bigint"!=typeof a)throw new Error(`Invalid field ${i}: expected bigint, received ${typeof a}`);t[i]=a;break;case"symbol":if("symbol"!=typeof a)throw new Error(`Invalid field ${i}: expected symbol, received ${typeof a}`);t[i]=a}}else{const r=this.__def.valueValidator;for(let n=0;n<s.length;n++){const i=s[n];t[i]=r.parse(e[i])}}return t}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:e instanceof t?e:new t([{code:"custom",path:[],message:String(e)}])}}}get isSimple(){return!1}}class VldAnyV2 extends e{constructor(){super(r.ANY),this.__def=Object.freeze({type:"any"})}static create(){return new VldAnyV2}parse(e){return e}safeParse(e){return{success:!0,data:e}}get isSimple(){return!0}}class VldUnknownV2 extends e{constructor(){super(r.UNKNOWN),this.__def=Object.freeze({type:"unknown"})}static create(){return new VldUnknownV2}parse(e){return e}safeParse(e){return{success:!0,data:e}}get isSimple(){return!0}}class VldVoidV2 extends e{constructor(){super(r.VOID),this.__def=Object.freeze({type:"void"})}static create(){return new VldVoidV2}parse(e){if(void 0!==e)throw new t([s("void",n(e),void 0)])}safeParse(e){return void 0===e?{success:!0,data:void 0}:{success:!1,error:new t([s("void",n(e),void 0)])}}get isSimple(){return!0}}class VldNeverV2 extends e{constructor(){super(r.NEVER),this.__def=Object.freeze({type:"never"})}static create(){return new VldNeverV2}parse(e){throw new t([{code:"custom",path:[],message:"No value can pass never validation"}])}safeParse(e){return{success:!1,error:new t([{code:"custom",path:[],message:"No value can pass never validation"}])}}get isSimple(){return!0}}class VldNullV2 extends e{constructor(){super(r.NULL),this.__def=Object.freeze({type:"null"})}static create(){return new VldNullV2}parse(e){if(null!==e)throw new t([s("null",n(e),void 0)]);return null}safeParse(e){return null===e?{success:!0,data:null}:{success:!1,error:new t([s("null",n(e),void 0)])}}get isSimple(){return!0}}class VldUndefinedV2 extends e{constructor(){super(r.UNDEFINED),this.__def=Object.freeze({type:"undefined"})}static create(){return new VldUndefinedV2}parse(e){if(void 0!==e)throw new t([s("undefined",n(e),void 0)])}safeParse(e){return void 0===e?{success:!0,data:void 0}:{success:!1,error:new t([s("undefined",n(e),void 0)])}}get isSimple(){return!0}}class VldSymbolV2 extends e{constructor(){super(r.SYMBOL),this.__def=Object.freeze({type:"symbol"})}static create(){return new VldSymbolV2}parse(e){if("symbol"!=typeof e)throw new t([s("symbol",n(e),void 0)]);return e}safeParse(e){return"symbol"==typeof e?{success:!0,data:e}:{success:!1,error:new t([s("symbol",n(e),void 0)])}}get isSimple(){return!0}}class VldFunctionV2 extends e{constructor(){super(r.FUNCTION),this.__def=Object.freeze({type:"function"})}static create(){return new VldFunctionV2}parse(e){if("function"!=typeof e)throw new t([s("function",n(e),void 0)]);return e}safeParse(e){return"function"==typeof e?{success:!0,data:e}:{success:!1,error:new t([s("function",n(e),void 0)])}}get isSimple(){return!0}}export{VldAnyV2,VldBooleanV2,VldEnumV2,VldFunctionV2,VldLiteralV2,VldNeverV2,VldNullV2,VldRecordV2,VldSymbolV2,VldUndefinedV2,VldUnknownV2,VldVoidV2};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{getMessages as t}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{getMessages as t}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-BU14-Dk8.js";function a(e){const r=JSON.stringify(e,(e,r)=>"bigint"==typeof r?r.toString():r);return void 0===r?String(e):r}class VldLiteral extends e{constructor(e,t){super(r.LITERAL),this._values=e,this.errorMessage=t}get literal(){return this._values[0]}get value(){return this._values[0]}get values(){return this._values}get isSimple(){return 1===this._values.length}get expectedDisplay(){return a(1===this._values.length?this._values[0]:this._values)}static create(e){const r=Array.isArray(e)?e:[e];if(0===r.length)throw new Error("Cannot create literal schema with no valid values");return new VldLiteral(r)}parse(e){if(!this._values.includes(e))throw this._createLiteralError(e);return e}_createLiteralError(e){const r={code:"invalid_value",path:[],values:[...this._values],message:this.errorMessage||t().literalExpected(this.expectedDisplay,a(e))};return new s([r])}safeParse(e){return this._values.includes(e)?{success:!0,data:e}:{success:!1,error:this._createLiteralError(e)}}}export{VldLiteral};
|
package/dist/validators/map.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{getMessages as t}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{getMessages as t}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-BU14-Dk8.js";class VldMap extends e{constructor(e,t,s,i=[]){super(r.MAP),this.keyValidator=e,this.valueValidator=t,this.errorMessage=s,this._checks=i,this._simpleKeyMode=this.getSimpleItemMode(e),this._simpleValueMode=this.getSimpleItemMode(t),this._simpleKeyValue="literal"===this._simpleKeyMode?e.literal:void 0,this._simpleValueValue="literal"===this._simpleValueMode?t.literal:void 0}get keySchema(){return this.keyValidator}get valueSchema(){return this.valueValidator}get keyType(){return this.keyValidator}get valueType(){return this.valueValidator}static create(e,r){return new VldMap(e,r)}getSimpleItemMode(e){if(!0===e.isSimple)switch(e.validatorType){case r.STRING:return"string";case r.NUMBER:return"number";case r.BOOLEAN:return"boolean";case r.BIGINT:return"bigint";case r.SYMBOL:return"symbol";case r.NULL:return"null";case r.UNDEFINED:case r.VOID:return"undefinedValue";case r.LITERAL:return"literal";case r.ANY:case r.UNKNOWN:return"passthrough";default:return}}getSimpleItemError(e,r,s){switch(e){case"string":return t().invalidString;case"number":return t().invalidNumber;case"boolean":return t().invalidBoolean;case"bigint":return t().invalidBigint;case"symbol":return t().invalidSymbol;case"null":return"Expected null, received "+typeof s;case"undefinedValue":return t().expectedUndefined;case"literal":return t().literalExpected(JSON.stringify(r),JSON.stringify(s));default:return t().invalidMap}}parseSimpleItem(e,r,t){switch(e){case"string":if("string"!=typeof t)throw new Error(this.getSimpleItemError(e,r,t));return t;case"number":if("number"!=typeof t||isNaN(t))throw new Error(this.getSimpleItemError(e,r,t));return t;case"boolean":if("boolean"!=typeof t)throw new Error(this.getSimpleItemError(e,r,t));return t;case"bigint":if("bigint"!=typeof t)throw new Error(this.getSimpleItemError(e,r,t));return t;case"symbol":if("symbol"!=typeof t)throw new Error(this.getSimpleItemError(e,r,t));return t;case"null":if(null!==t)throw new Error(this.getSimpleItemError(e,r,t));return null;case"undefinedValue":if(void 0!==t)throw new Error(this.getSimpleItemError(e,r,t));return;case"literal":if(t!==r)throw new Error(this.getSimpleItemError(e,r,t));return r;case"passthrough":return t}}parse(e){if(!(e instanceof Map))throw new Error(this.errorMessage||t().invalidMap);return this.parseKnownMap(e)}parseKnownMap(e){const r=new Map,t=this._simpleKeyMode,s=this._simpleValueMode;if("string"===t&&"number"===s)for(const[i,a]of e){if("string"!=typeof i)throw new Error(this.getSimpleItemError(t,this._simpleKeyValue,i));if("number"!=typeof a||isNaN(a))throw new Error(this.getSimpleItemError(s,this._simpleValueValue,a));r.set(i,a)}else if("string"===t&&"string"===s)for(const[i,a]of e){if("string"!=typeof i)throw new Error(this.getSimpleItemError(t,this._simpleKeyValue,i));if("string"!=typeof a)throw new Error(this.getSimpleItemError(s,this._simpleValueValue,a));r.set(i,a)}else if(void 0!==t&&void 0!==s){const i=t,a=s;for(const[t,s]of e){const e=this.parseSimpleItem(i,this._simpleKeyValue,t),n=this.parseSimpleItem(a,this._simpleValueValue,s);r.set(e,n)}}else for(const[i,a]of e)try{const e=void 0===t?this.keyValidator.parse(i):this.parseSimpleItem(t,this._simpleKeyValue,i),n=void 0===s?this.valueValidator.parse(a):this.parseSimpleItem(s,this._simpleValueValue,a);r.set(e,n)}catch(e){throw new Error(e.message)}for(const e of this._checks)if(!e.fn(r))throw new Error(e.message);return r}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:(r=e.message,new s([{code:"invalid_type",path:[],message:r}]))}}var r}min(e,r){return new VldMap(this.keyValidator,this.valueValidator,this.errorMessage,[...this._checks,{fn:r=>r.size>=e,message:r||`Map must contain at least ${e} entries`}])}max(e,r){return new VldMap(this.keyValidator,this.valueValidator,this.errorMessage,[...this._checks,{fn:r=>r.size<=e,message:r||`Map must contain at most ${e} entries`}])}size(e,r){return new VldMap(this.keyValidator,this.valueValidator,this.errorMessage,[...this._checks,{fn:r=>r.size===e,message:r||`Map must contain exactly ${e} entries`}])}nonempty(e){return this.min(1,e||"Map cannot be empty")}}export{VldMap};
|
package/dist/validators/nan.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{V as s}from"../chunks/errors-core-
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{V as s}from"../chunks/errors-core-BU14-Dk8.js";class VldNan extends e{constructor(){super(r.NAN)}static create(){return new VldNan}parse(e){if("number"!=typeof e||!Number.isNaN(e))throw new Error("Expected NaN, received "+("number"==typeof e?"a valid number":typeof e));return e}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:(r=e.message,new s([{code:"invalid_number",path:[],message:r}]))}}var r}}export{VldNan};
|
package/dist/validators/never.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{VldBase as e}from"./base.js";import{getMessages as r}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-
|
|
1
|
+
import{VldBase as e}from"./base.js";import{getMessages as r}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-BU14-Dk8.js";class VldNever extends e{static create(){return new VldNever}parse(e){throw new Error(r().neverType)}safeParse(e){return{success:!1,error:new s([{code:"invalid_type",path:[],message:r().neverType}])}}}export{VldNever};
|
package/dist/validators/null.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{V as s}from"../chunks/errors-core-
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{V as s}from"../chunks/errors-core-BU14-Dk8.js";class VldNull extends e{constructor(){super(r.NULL)}static create(){return new VldNull}get isSimple(){return!0}parse(e){if(null!==e)throw new Error("Expected null, received "+typeof e);return null}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:(r=e.message,new s([{code:"invalid_type",path:[],message:r}]))}}var r}}export{VldNull};
|