@oxog/vld 2.4.0 → 3.0.1

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.
Files changed (127) hide show
  1. package/CHANGELOG.md +68 -1
  2. package/README.md +113 -1
  3. package/dist/chunks/errors-core-BU14-Dk8.js +1 -0
  4. package/dist/cjs/chunks/errors-core-C0mA6tfa.cjs +1 -0
  5. package/dist/cjs/cli/bin.cjs +1 -1
  6. package/dist/cjs/codecs/index.cjs +1 -1
  7. package/dist/cjs/coercion/index.cjs +1 -1
  8. package/dist/cjs/compile.cjs +1 -1
  9. package/dist/cjs/errors.cjs +1 -1
  10. package/dist/cjs/index.cjs +1 -1
  11. package/dist/cjs/mini.cjs +1 -1
  12. package/dist/cjs/v3/index.cjs +1 -1
  13. package/dist/cjs/v4/core/index.cjs +1 -1
  14. package/dist/cjs/v4/index.cjs +1 -1
  15. package/dist/cjs/v4/mini/index.cjs +1 -1
  16. package/dist/cjs/v4-mini/index.cjs +1 -1
  17. package/dist/cjs/validators/any.cjs +1 -1
  18. package/dist/cjs/validators/array-v2.cjs +1 -0
  19. package/dist/cjs/validators/array.cjs +1 -1
  20. package/dist/cjs/validators/base.cjs +1 -1
  21. package/dist/cjs/validators/base64.cjs +1 -1
  22. package/dist/cjs/validators/bigint-v2.cjs +1 -0
  23. package/dist/cjs/validators/bigint.cjs +1 -1
  24. package/dist/cjs/validators/boolean.cjs +1 -1
  25. package/dist/cjs/validators/codec.cjs +1 -1
  26. package/dist/cjs/validators/composite-v2.cjs +1 -0
  27. package/dist/cjs/validators/custom.cjs +1 -1
  28. package/dist/cjs/validators/date-v2.cjs +1 -0
  29. package/dist/cjs/validators/date.cjs +1 -1
  30. package/dist/cjs/validators/discriminated-union.cjs +1 -1
  31. package/dist/cjs/validators/enum.cjs +1 -1
  32. package/dist/cjs/validators/file.cjs +1 -1
  33. package/dist/cjs/validators/function.cjs +1 -1
  34. package/dist/cjs/validators/hex.cjs +1 -1
  35. package/dist/cjs/validators/index.cjs +1 -1
  36. package/dist/cjs/validators/intersection.cjs +1 -1
  37. package/dist/cjs/validators/json.cjs +1 -1
  38. package/dist/cjs/validators/lazy.cjs +1 -1
  39. package/dist/cjs/validators/leaf-v2.cjs +1 -0
  40. package/dist/cjs/validators/literal.cjs +1 -1
  41. package/dist/cjs/validators/map.cjs +1 -1
  42. package/dist/cjs/validators/nan.cjs +1 -1
  43. package/dist/cjs/validators/never.cjs +1 -1
  44. package/dist/cjs/validators/null.cjs +1 -1
  45. package/dist/cjs/validators/number-v2.cjs +1 -0
  46. package/dist/cjs/validators/number.cjs +1 -1
  47. package/dist/cjs/validators/object-v2.cjs +1 -0
  48. package/dist/cjs/validators/object.cjs +1 -1
  49. package/dist/cjs/validators/promise.cjs +1 -1
  50. package/dist/cjs/validators/record.cjs +1 -1
  51. package/dist/cjs/validators/set.cjs +1 -1
  52. package/dist/cjs/validators/string-bool.cjs +1 -1
  53. package/dist/cjs/validators/string-formats.cjs +1 -1
  54. package/dist/cjs/validators/string-v2.cjs +1 -0
  55. package/dist/cjs/validators/string.cjs +1 -1
  56. package/dist/cjs/validators/symbol.cjs +1 -1
  57. package/dist/cjs/validators/template-literal.cjs +1 -1
  58. package/dist/cjs/validators/tuple.cjs +1 -1
  59. package/dist/cjs/validators/uint8array.cjs +1 -1
  60. package/dist/cjs/validators/undefined.cjs +1 -1
  61. package/dist/cjs/validators/union-v2.cjs +1 -0
  62. package/dist/cjs/validators/union.cjs +1 -1
  63. package/dist/cjs/validators/unknown.cjs +1 -1
  64. package/dist/cjs/validators/void.cjs +1 -1
  65. package/dist/cjs/validators/wrapper-v2.cjs +1 -0
  66. package/dist/cjs/validators/xor.cjs +1 -1
  67. package/dist/errors-core.d.ts +3 -0
  68. package/dist/errors.js +1 -1
  69. package/dist/index.d.ts +168 -12
  70. package/dist/index.js +1 -1
  71. package/dist/v3/index.js +1 -1
  72. package/dist/v4/core/index.js +1 -1
  73. package/dist/v4/index.js +1 -1
  74. package/dist/v4/mini/index.js +1 -1
  75. package/dist/v4-mini/index.js +1 -1
  76. package/dist/validators/array-v2.d.ts +113 -0
  77. package/dist/validators/array-v2.js +1 -0
  78. package/dist/validators/array.js +1 -1
  79. package/dist/validators/base.js +1 -1
  80. package/dist/validators/base64.js +1 -1
  81. package/dist/validators/bigint-v2.d.ts +105 -0
  82. package/dist/validators/bigint-v2.js +1 -0
  83. package/dist/validators/bigint.js +1 -1
  84. package/dist/validators/boolean.js +1 -1
  85. package/dist/validators/codec.js +1 -1
  86. package/dist/validators/composite-v2.d.ts +59 -0
  87. package/dist/validators/composite-v2.js +1 -0
  88. package/dist/validators/date-v2.d.ts +140 -0
  89. package/dist/validators/date-v2.js +1 -0
  90. package/dist/validators/date.js +1 -1
  91. package/dist/validators/enum.js +1 -1
  92. package/dist/validators/file.js +1 -1
  93. package/dist/validators/function.js +1 -1
  94. package/dist/validators/hex.js +1 -1
  95. package/dist/validators/intersection.js +1 -1
  96. package/dist/validators/leaf-v2.d.ts +138 -0
  97. package/dist/validators/leaf-v2.js +1 -0
  98. package/dist/validators/literal.js +1 -1
  99. package/dist/validators/map.js +1 -1
  100. package/dist/validators/nan.js +1 -1
  101. package/dist/validators/never.js +1 -1
  102. package/dist/validators/null.js +1 -1
  103. package/dist/validators/number-v2.d.ts +252 -0
  104. package/dist/validators/number-v2.js +1 -0
  105. package/dist/validators/number.js +1 -1
  106. package/dist/validators/object-v2.d.ts +40 -0
  107. package/dist/validators/object-v2.js +1 -0
  108. package/dist/validators/object.js +1 -1
  109. package/dist/validators/record.js +1 -1
  110. package/dist/validators/set.js +1 -1
  111. package/dist/validators/string-formats.js +1 -1
  112. package/dist/validators/string-v2.d.ts +274 -0
  113. package/dist/validators/string-v2.js +1 -0
  114. package/dist/validators/string.js +1 -1
  115. package/dist/validators/symbol.js +1 -1
  116. package/dist/validators/tuple.js +1 -1
  117. package/dist/validators/uint8array.js +1 -1
  118. package/dist/validators/undefined.js +1 -1
  119. package/dist/validators/union-v2.d.ts +28 -0
  120. package/dist/validators/union-v2.js +1 -0
  121. package/dist/validators/void.js +1 -1
  122. package/dist/validators/wrapper-v2.d.ts +74 -0
  123. package/dist/validators/wrapper-v2.js +1 -0
  124. package/dist/zod-error.d.ts +84 -0
  125. package/package.json +1 -1
  126. package/dist/chunks/errors-core-CerMg2rq.js +0 -1
  127. package/dist/cjs/chunks/errors-core-DrydF46s.cjs +0 -1
@@ -1 +1 @@
1
- import{VldBase as e,VLD_VALIDATOR_TYPES as t,VldOptional as s,VldExactOptional as i}from"./base.js";import{getMessages as r}from"../locales/runtime.js";import{VldEnum as n}from"./enum.js";import{i as o}from"../chunks/security-Dy24T8N7.js";import{V as a}from"../chunks/errors-core-CerMg2rq.js";class VldObject extends e{createSafeParseError(e,t){if(e instanceof a)return void 0!==t?new a(e.issues.map(e=>({...e,path:e.path?[t,...e.path]:[t],message:e.message.startsWith("Invalid field")?e.message:r().objectField(String(t),e.message)}))):e;const s=e instanceof Error?e.message:String(e);return new a([{code:"invalid_type",path:void 0!==t?[t]:[],message:void 0===t||s.startsWith("Invalid field")?s:r().objectField(String(t),s)}])}constructor(s){super(t.OBJECT),this._config=s,this._shapeKeys=Object.keys(s.shape),this._shapeKeysSet=new Set(this._shapeKeys),this._validators=this._shapeKeys.map(e=>this.tryGetFieldValidator(e)),this._validatorTypes=this._shapeKeys.map((e,t)=>this._validators[t]?.validatorType||this.getValidatorType(e)),this._simpleFieldModes=this._validators.map((e,t)=>this.getSimpleFieldMode(e,this._validatorTypes[t])),this._simpleFieldValues=this._validators.map((e,t)=>"literal"===this._simpleFieldModes[t]?e.literal:void 0),this._canUseSimpleObjectFastPath=!s.strict&&!s.passthrough&&!s.catchall&&this._shapeKeys.length>0&&this._simpleFieldModes.every(e=>void 0!==e),this._canUseSafeParseFastPath=this._validators.every(t=>t instanceof e)&&(void 0===s.catchall||s.catchall instanceof e)}tryGetFieldValidator(e){try{const t=Object.getOwnPropertyDescriptor(this._config.shape,e);if(t?.get)return;return this.resolveFieldValidator(e)}catch{return}}resolveFieldValidator(e){try{const t=this._config.shape[e];if(!t||"function"!=typeof t.safeParse)return;return t}catch{return}}getFieldValidator(e,t){const s=(void 0===t?void 0:this._validators[t])||this.resolveFieldValidator(e);if(!s)throw new Error(`Invalid validator for field "${e}"`);return s}getValidatorType(e){try{const s=this._config.shape[e];return s?.validatorType||t.UNKNOWN}catch{return t.UNKNOWN}}getSimpleFieldMode(e,s){if(e&&!0===e.isSimple)switch(s){case t.STRING:return"string";case t.NUMBER:return"number";case t.BOOLEAN:return"boolean";case t.BIGINT:return"bigint";case t.SYMBOL:return"symbol";case t.NULL:return"null";case t.UNDEFINED:case t.VOID:return"undefinedValue";case t.LITERAL:return"literal";case t.ANY:case t.UNKNOWN:return"passthrough";default:return}}getSimpleFieldError(e,t,s){switch(e){case"string":return r().invalidString;case"number":return r().invalidNumber;case"boolean":return r().invalidBoolean;case"bigint":return r().invalidBigint;case"symbol":return r().invalidSymbol;case"null":return"Expected null, received "+typeof s;case"undefinedValue":return r().expectedUndefined;case"literal":return r().literalExpected(JSON.stringify(t),JSON.stringify(s));default:return r().invalidObject}}parseCheckedField(e,s,i){if(s===t.STRING&&"string"==typeof i){const t=e.parseKnownString;if("function"==typeof t)return t.call(e,i)}if(s===t.NUMBER&&"number"==typeof i&&!isNaN(i)){const t=e.parseKnownNumber;if("function"==typeof t)return t.call(e,i)}if(s===t.BOOLEAN&&"boolean"==typeof i){const t=e.parseKnownBoolean;if("function"==typeof t)return t.call(e,i)}if(s===t.BIGINT&&"bigint"==typeof i){const t=e.parseKnownBigInt;if("function"==typeof t)return t.call(e,i)}if(s===t.SYMBOL&&"symbol"==typeof i){const t=e.parseKnownSymbol;if("function"==typeof t)return t.call(e,i)}if(s===t.FUNCTION&&"function"==typeof i){const t=e.parseKnownFunction;if("function"==typeof t)return t.call(e,i)}if(s===t.FILE&&"object"==typeof i&&null!==i&&("size"in i&&"type"in i||"undefined"!=typeof File&&i instanceof File)){const t=e.parseKnownFile;if("function"==typeof t)return t.call(e,i)}if(s===t.DATE&&i instanceof Date){const t=e.parseKnownDate;if("function"==typeof t)return t.call(e,i)}if(s===t.OBJECT&&"object"==typeof i&&null!==i&&!Array.isArray(i)){const t=e.parseKnownObject;if("function"==typeof t)return t.call(e,i)}if(s===t.ARRAY&&Array.isArray(i)){const t=e.parseKnownArray;if("function"==typeof t)return t.call(e,i)}if(s===t.TUPLE&&Array.isArray(i)){const t=e.parseKnownTuple;if("function"==typeof t)return t.call(e,i)}if(s===t.RECORD&&"object"==typeof i&&null!==i&&!Array.isArray(i)){const t=e.parseKnownRecord;if("function"==typeof t)return t.call(e,i)}if(s===t.SET&&i instanceof Set){const t=e.parseKnownSet;if("function"==typeof t)return t.call(e,i)}if(s===t.MAP&&i instanceof Map){const t=e.parseKnownMap;if("function"==typeof t)return t.call(e,i)}return e.parse(i)}parseSimpleObjectValue(e,t,s=!1){if(2===this._shapeKeys.length&&"string"===this._simpleFieldModes[0]&&"number"===this._simpleFieldModes[1]){const t=this._shapeKeys[0],s=e[t];if("string"!=typeof s)throw new Error(r().objectField(t,r().invalidString));const i=this._shapeKeys[1],n=e[i];if("number"!=typeof n||isNaN(n))throw new Error(r().objectField(i,r().invalidNumber));const o={};return o[t]=s,o[i]=n,o}if(3===this._shapeKeys.length&&"literal"===this._simpleFieldModes[0]&&"string"===this._simpleFieldModes[1]&&"number"===this._simpleFieldModes[2]){const i=this._shapeKeys[0],n=e[i],o=this._simpleFieldValues[0];if((!s||i!==t)&&n!==o)throw new Error(r().objectField(i,this.getSimpleFieldError("literal",o,n)));const a=this._shapeKeys[1],c=e[a];if("string"!=typeof c)throw new Error(r().objectField(a,r().invalidString));const l=this._shapeKeys[2],h=e[l];if("number"!=typeof h||isNaN(h))throw new Error(r().objectField(l,r().invalidNumber));const f={};return f[i]=o,f[a]=c,f[l]=h,f}const i={};for(let n=0;n<this._shapeKeys.length;n++){const o=this._shapeKeys[n],a=e[o],c=this._simpleFieldModes[n];switch(c){case"string":if("string"!=typeof a)throw new Error(r().objectField(o,r().invalidString));i[o]=a;break;case"number":if("number"!=typeof a||isNaN(a))throw new Error(r().objectField(o,r().invalidNumber));i[o]=a;break;case"boolean":if("boolean"!=typeof a)throw new Error(r().objectField(o,r().invalidBoolean));i[o]=a;break;case"bigint":if("bigint"!=typeof a)throw new Error(r().objectField(o,r().invalidBigint));i[o]=a;break;case"symbol":if("symbol"!=typeof a)throw new Error(r().objectField(o,r().invalidSymbol));i[o]=a;break;case"null":if(null!==a)throw new Error(r().objectField(o,this.getSimpleFieldError(c,void 0,a)));i[o]=null;break;case"undefinedValue":if(void 0!==a)throw new Error(r().objectField(o,r().expectedUndefined));i[o]=void 0;break;case"literal":if(s&&o===t){i[o]=this._simpleFieldValues[n];break}if(a!==this._simpleFieldValues[n])throw new Error(r().objectField(o,this.getSimpleFieldError(c,this._simpleFieldValues[n],a)));i[o]=this._simpleFieldValues[n];break;case"passthrough":i[o]=a;break;default:throw new Error(r().objectField(o,r().invalidObject))}}return i}get config(){return this._config}get shapeKeys(){return this._shapeKeys}get shapeKeysSet(){return this._shapeKeysSet}static create(e){return new VldObject({shape:e})}parse(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(this._config.errorMessage||r().invalidObject);return this.parseObjectValue(e)}parseKnownObject(e,t){return this.parseObjectValue(e,t)}parseTrustedKnownObject(e,t){return this.parseObjectValue(e,t,!0)}parseObjectValue(e,t,s=!1){if(this._canUseSimpleObjectFastPath)return this.parseSimpleObjectValue(e,t,s);const i={};let n="";try{for(let o=0;o<this._shapeKeys.length;o++){if(n=this._shapeKeys[o],s&&n===t&&"literal"===this._simpleFieldModes[o]){i[n]=this._simpleFieldValues[o];continue}const a=this._simpleFieldModes[o];if(void 0!==a){const t=e[n];switch(a){case"string":if("string"!=typeof t)throw new Error(r().invalidString);i[n]=t;continue;case"number":if("number"!=typeof t||isNaN(t))throw new Error(r().invalidNumber);i[n]=t;continue;case"boolean":if("boolean"!=typeof t)throw new Error(r().invalidBoolean);i[n]=t;continue;case"bigint":if("bigint"!=typeof t)throw new Error(r().invalidBigint);i[n]=t;continue;case"symbol":if("symbol"!=typeof t)throw new Error(r().invalidSymbol);i[n]=t;continue;case"null":if(null!==t)throw new Error(this.getSimpleFieldError(a,void 0,t));i[n]=null;continue;case"undefinedValue":if(void 0!==t)throw new Error(r().expectedUndefined);i[n]=void 0;continue;case"literal":if(t!==this._simpleFieldValues[o])throw new Error(this.getSimpleFieldError(a,this._simpleFieldValues[o],t));i[n]=this._simpleFieldValues[o];continue;case"passthrough":i[n]=t;continue}continue}const c=this.getFieldValidator(n,o);i[n]=this.parseCheckedField(c,this._validatorTypes[o],e[n])}}catch(e){if(e instanceof a)throw new a(e.issues.map(e=>({...e,path:e.path?[n,...e.path]:[n],message:r().objectField(n,e.message.startsWith("Invalid field")?e.message.replace(/^Invalid field "[^"]+": /,""):e.message)})));const t=e instanceof Error?e.message:String(e);throw new a([{code:"invalid_type",path:[n],message:t.startsWith("Invalid field")?t:r().objectField(n,t)}])}if(this._config.strict||this._config.passthrough||this._config.catchall){const t=Object.keys(e);if(this._config.strict){const e=[];for(let s=0;s<t.length;s++){const i=t[s];this._shapeKeysSet.has(i)||e.push(i)}if(e.length>0)throw new Error(r().unexpectedKeys(e))}if(this._config.passthrough)for(let s=0;s<t.length;s++){const r=t[s];this._shapeKeysSet.has(r)||o(r)||(i[r]=e[r])}if(this._config.catchall)for(let s=0;s<t.length;s++){const n=t[s];if(!this._shapeKeysSet.has(n)&&!o(n))try{i[n]=this._config.catchall.parse(e[n])}catch(e){throw new Error(r().objectField(n,e.message))}}}return i}safeParse(t){if("object"!=typeof t||null===t||Array.isArray(t))return{success:!1,error:this.createSafeParseError(this._config.errorMessage||r().invalidObject)};const s=t;if(this._canUseSimpleObjectFastPath)try{return{success:!0,data:this.parseSimpleObjectValue(s)}}catch{}if(this._canUseSafeParseFastPath)try{return{success:!0,data:this.parseObjectValue(s)}}catch{}const i={},n=[];for(let t=0;t<this._shapeKeys.length;t++){const o=this._shapeKeys[t],a=s[o],c=this._simpleFieldModes[t];if(void 0!==c){if("string"===c&&"string"==typeof a||"number"===c&&"number"==typeof a&&!isNaN(a)||"boolean"===c&&"boolean"==typeof a||"bigint"===c&&"bigint"==typeof a||"symbol"===c&&"symbol"==typeof a||"null"===c&&null===a||"undefinedValue"===c&&void 0===a||"literal"===c&&a===this._simpleFieldValues[t]||"passthrough"===c){i[o]="literal"===c?this._simpleFieldValues[t]:"null"===c?null:a;continue}n.push({code:"invalid_type",path:[o],message:r().objectField(o,this.getSimpleFieldError(c,this._simpleFieldValues[t],a))});continue}let l;try{l=this.getFieldValidator(o,t)}catch(e){const t=this.createSafeParseError(e,o);n.push(...t.issues);continue}try{if(l instanceof e)i[o]=this.parseCheckedField(l,this._validatorTypes[t],a);else{const e=l.safeParse(a);if(e.success)i[o]=e.data;else{const t=this.createSafeParseError(e.error,o);n.push(...t.issues)}}}catch(e){const t=this.createSafeParseError(e,o);n.push(...t.issues)}}if(this._config.strict||this._config.passthrough||this._config.catchall){const t=Object.keys(s);if(this._config.strict){const e=[];for(let s=0;s<t.length;s++){const i=t[s];this._shapeKeysSet.has(i)||e.push(i)}e.length>0&&n.push(...e.map(e=>({code:"unrecognized_keys",path:[e],message:r().unexpectedKeys([e])})))}if(this._config.passthrough)for(let e=0;e<t.length;e++){const r=t[e];this._shapeKeysSet.has(r)||o(r)||(i[r]=s[r])}if(this._config.catchall){const r=this._config.catchall;for(let a=0;a<t.length;a++){const c=t[a];if(!this._shapeKeysSet.has(c)&&!o(c))try{if(r instanceof e)i[c]=r.parse(s[c]);else{const e=r.safeParse(s[c]);if(e.success)i[c]=e.data;else{const t=this.createSafeParseError(e.error,c);n.push(...t.issues)}}}catch(e){const t=this.createSafeParseError(e,c);n.push(...t.issues)}}}}return n.length>0?{success:!1,error:new a(n)}:{success:!0,data:i}}encode(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(this._config.errorMessage||r().invalidObject);return this.encodeObjectSync(e)}safeEncode(e){try{return{success:!0,data:this.encode(e)}}catch(e){return{success:!1,error:this.createSafeParseError(e.message)}}}async encodeAsync(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(this._config.errorMessage||r().invalidObject);return this.encodeObjectAsync(e)}async safeEncodeAsync(e){try{return{success:!0,data:await this.encodeAsync(e)}}catch(e){return{success:!1,error:this.createSafeParseError(e.message)}}}encodeObjectSync(e){const t={};for(let s=0;s<this._shapeKeys.length;s++){const i=this._shapeKeys[s],r=this.getFieldValidator(i,s).encode(e[i]);(void 0!==r||Object.prototype.hasOwnProperty.call(e,i))&&(t[i]=r)}return this.encodeExtraKeysSync(e,t),t}async encodeObjectAsync(e){const t={};for(let s=0;s<this._shapeKeys.length;s++){const i=this._shapeKeys[s],r=await this.getFieldValidator(i,s).encodeAsync(e[i]);(void 0!==r||Object.prototype.hasOwnProperty.call(e,i))&&(t[i]=r)}const s=this.getEncodedExtraKeys(e);for(const i of s)o(i)||(this._config.catchall?t[i]=await this._config.catchall.encodeAsync(e[i]):this._config.passthrough&&(t[i]=e[i]));return t}getEncodedExtraKeys(e){const t=Object.keys(e).filter(e=>!this._shapeKeysSet.has(e));if(this._config.strict&&t.length>0)throw new Error(r().unexpectedKeys(t));return t}encodeExtraKeysSync(e,t){for(const s of this.getEncodedExtraKeys(e))o(s)||(this._config.catchall?t[s]=this._config.catchall.encode(e[s]):this._config.passthrough&&(t[s]=e[s]))}strict(e){return new VldObject({...this._config,strict:!0,passthrough:!1,errorMessage:e})}passthrough(){return new VldObject({...this._config,strict:!1,passthrough:!0})}loose(){return this.passthrough()}strip(){const e={...this._config,strict:!1,passthrough:!1};return delete e.catchall,new VldObject(e)}partial(){const e={};for(const t in this._config.shape)e[t]=new s(this._config.shape[t]);return new VldObject({...this._config,shape:e})}deepPartial(){const e={};for(const t in this._config.shape){const i=this._config.shape[t];e[t]=new s(i instanceof VldObject?i.deepPartial():i)}return new VldObject({...this._config,shape:e})}exactPartial(){const e={};for(const t in this._config.shape)e[t]=new i(this._config.shape[t]);return new VldObject({...this._config,shape:e})}pick(...e){const t={};let s=[];s=1!==e.length||"object"!=typeof e[0]||null===e[0]||Array.isArray(e[0])?e.flat():Object.keys(e[0]).filter(t=>e[0][t]);for(const e of s)e in this._config.shape&&(t[e]=this._config.shape[e]);return new VldObject({...this._config,shape:t})}omit(...e){let t;t=1!==e.length||"object"!=typeof e[0]||null===e[0]||Array.isArray(e[0])?new Set(e.flat()):new Set(Object.keys(e[0]).filter(t=>e[0][t]));const s={};for(const e in this._config.shape)t.has(e)||(s[e]=this._config.shape[e]);return new VldObject({...this._config,shape:s})}extend(e){return new VldObject({...this._config,shape:{...this._config.shape,...e}})}merge(e){return new VldObject({...this._config,shape:{...this._config.shape,...e.config.shape}})}required(){const e={};for(const t in this._config.shape){const i=this._config.shape[t];if(i instanceof s){const s=i.baseValidator;if(!s||"function"!=typeof s.parse)throw new Error(`Invalid VldOptional structure for field "${t}": missing or invalid baseValidator`);e[t]=s}else e[t]=i}return new VldObject({...this._config,shape:e})}catchall(e){return new VldObject({...this._config,catchall:e,passthrough:!1})}get shape(){return this._config.shape}keyof(){const e=Object.keys(this._config.shape);if(0===e.length)throw new Error("Cannot create keyof enum from empty object");return n.create(e)}safeExtend(e){const t=new Set(Object.keys(this._config.shape)),s=Object.keys(e),i=[];for(const e of s)t.has(e)&&i.push(e);if(i.length>0)throw new Error(`safeExtend: ${r().safeExtendOverlap(i)}`);return new VldObject({...this._config,shape:{...this._config.shape,...e}})}}export{VldObject};
1
+ import{VldBase as e,VLD_VALIDATOR_TYPES as t,VldOptional as s,VldExactOptional as i}from"./base.js";import{getMessages as r}from"../locales/runtime.js";import{VldEnum as n}from"./enum.js";import{i as o}from"../chunks/security-Dy24T8N7.js";import{V as a}from"../chunks/errors-core-BU14-Dk8.js";class VldObject extends e{createSafeParseError(e,t){if(e instanceof a)return void 0!==t?new a(e.issues.map(e=>({...e,path:e.path?[t,...e.path]:[t],message:e.message.startsWith("Invalid field")?e.message:r().objectField(String(t),e.message)}))):e;const s=e instanceof Error?e.message:String(e);return new a([{code:"invalid_type",path:void 0!==t?[t]:[],message:void 0===t||s.startsWith("Invalid field")?s:r().objectField(String(t),s)}])}constructor(s){super(t.OBJECT),this._config=s,this._shapeKeys=Object.keys(s.shape),this._shapeKeysSet=new Set(this._shapeKeys),this._validators=this._shapeKeys.map(e=>this.tryGetFieldValidator(e)),this._validatorTypes=this._shapeKeys.map((e,t)=>this._validators[t]?.validatorType||this.getValidatorType(e)),this._simpleFieldModes=this._validators.map((e,t)=>this.getSimpleFieldMode(e,this._validatorTypes[t])),this._simpleFieldValues=this._validators.map((e,t)=>"literal"===this._simpleFieldModes[t]?e.literal:void 0),this._canUseSimpleObjectFastPath=!s.strict&&!s.passthrough&&!s.catchall&&this._shapeKeys.length>0&&this._simpleFieldModes.every(e=>void 0!==e),this._canUseSafeParseFastPath=this._validators.every(t=>t instanceof e)&&(void 0===s.catchall||s.catchall instanceof e)}tryGetFieldValidator(e){try{const t=Object.getOwnPropertyDescriptor(this._config.shape,e);if(t?.get)return;return this.resolveFieldValidator(e)}catch{return}}resolveFieldValidator(e){try{const t=this._config.shape[e];if(!t||"function"!=typeof t.safeParse)return;return t}catch{return}}getFieldValidator(e,t){const s=(void 0===t?void 0:this._validators[t])||this.resolveFieldValidator(e);if(!s)throw new Error(`Invalid validator for field "${e}"`);return s}getValidatorType(e){try{const s=this._config.shape[e];return s?.validatorType||t.UNKNOWN}catch{return t.UNKNOWN}}getSimpleFieldMode(e,s){if(e&&!0===e.isSimple)switch(s){case t.STRING:return"string";case t.NUMBER:return"number";case t.BOOLEAN:return"boolean";case t.BIGINT:return"bigint";case t.SYMBOL:return"symbol";case t.NULL:return"null";case t.UNDEFINED:case t.VOID:return"undefinedValue";case t.LITERAL:return"literal";case t.ANY:case t.UNKNOWN:return"passthrough";default:return}}getSimpleFieldError(e,t,s){switch(e){case"string":return r().invalidString;case"number":return r().invalidNumber;case"boolean":return r().invalidBoolean;case"bigint":return r().invalidBigint;case"symbol":return r().invalidSymbol;case"null":return"Expected null, received "+typeof s;case"undefinedValue":return r().expectedUndefined;case"literal":return r().literalExpected(JSON.stringify(t),JSON.stringify(s));default:return r().invalidObject}}parseCheckedField(e,s,i){if(s===t.STRING&&"string"==typeof i){const t=e.parseKnownString;if("function"==typeof t)return t.call(e,i)}if(s===t.NUMBER&&"number"==typeof i&&!isNaN(i)){const t=e.parseKnownNumber;if("function"==typeof t)return t.call(e,i)}if(s===t.BOOLEAN&&"boolean"==typeof i){const t=e.parseKnownBoolean;if("function"==typeof t)return t.call(e,i)}if(s===t.BIGINT&&"bigint"==typeof i){const t=e.parseKnownBigInt;if("function"==typeof t)return t.call(e,i)}if(s===t.SYMBOL&&"symbol"==typeof i){const t=e.parseKnownSymbol;if("function"==typeof t)return t.call(e,i)}if(s===t.FUNCTION&&"function"==typeof i){const t=e.parseKnownFunction;if("function"==typeof t)return t.call(e,i)}if(s===t.FILE&&"object"==typeof i&&null!==i&&("size"in i&&"type"in i||"undefined"!=typeof File&&i instanceof File)){const t=e.parseKnownFile;if("function"==typeof t)return t.call(e,i)}if(s===t.DATE&&i instanceof Date){const t=e.parseKnownDate;if("function"==typeof t)return t.call(e,i)}if(s===t.OBJECT&&"object"==typeof i&&null!==i&&!Array.isArray(i)){const t=e.parseKnownObject;if("function"==typeof t)return t.call(e,i)}if(s===t.ARRAY&&Array.isArray(i)){const t=e.parseKnownArray;if("function"==typeof t)return t.call(e,i)}if(s===t.TUPLE&&Array.isArray(i)){const t=e.parseKnownTuple;if("function"==typeof t)return t.call(e,i)}if(s===t.RECORD&&"object"==typeof i&&null!==i&&!Array.isArray(i)){const t=e.parseKnownRecord;if("function"==typeof t)return t.call(e,i)}if(s===t.SET&&i instanceof Set){const t=e.parseKnownSet;if("function"==typeof t)return t.call(e,i)}if(s===t.MAP&&i instanceof Map){const t=e.parseKnownMap;if("function"==typeof t)return t.call(e,i)}return e.parse(i)}parseSimpleObjectValue(e,t,s=!1){if(2===this._shapeKeys.length&&"string"===this._simpleFieldModes[0]&&"number"===this._simpleFieldModes[1]){const t=this._shapeKeys[0],s=e[t];if("string"!=typeof s)throw new Error(r().objectField(t,r().invalidString));const i=this._shapeKeys[1],n=e[i];if("number"!=typeof n||isNaN(n))throw new Error(r().objectField(i,r().invalidNumber));const o={};return o[t]=s,o[i]=n,o}if(3===this._shapeKeys.length&&"literal"===this._simpleFieldModes[0]&&"string"===this._simpleFieldModes[1]&&"number"===this._simpleFieldModes[2]){const i=this._shapeKeys[0],n=e[i],o=this._simpleFieldValues[0];if((!s||i!==t)&&n!==o)throw new Error(r().objectField(i,this.getSimpleFieldError("literal",o,n)));const a=this._shapeKeys[1],c=e[a];if("string"!=typeof c)throw new Error(r().objectField(a,r().invalidString));const l=this._shapeKeys[2],h=e[l];if("number"!=typeof h||isNaN(h))throw new Error(r().objectField(l,r().invalidNumber));const f={};return f[i]=o,f[a]=c,f[l]=h,f}const i={};for(let n=0;n<this._shapeKeys.length;n++){const o=this._shapeKeys[n],a=e[o],c=this._simpleFieldModes[n];switch(c){case"string":if("string"!=typeof a)throw new Error(r().objectField(o,r().invalidString));i[o]=a;break;case"number":if("number"!=typeof a||isNaN(a))throw new Error(r().objectField(o,r().invalidNumber));i[o]=a;break;case"boolean":if("boolean"!=typeof a)throw new Error(r().objectField(o,r().invalidBoolean));i[o]=a;break;case"bigint":if("bigint"!=typeof a)throw new Error(r().objectField(o,r().invalidBigint));i[o]=a;break;case"symbol":if("symbol"!=typeof a)throw new Error(r().objectField(o,r().invalidSymbol));i[o]=a;break;case"null":if(null!==a)throw new Error(r().objectField(o,this.getSimpleFieldError(c,void 0,a)));i[o]=null;break;case"undefinedValue":if(void 0!==a)throw new Error(r().objectField(o,r().expectedUndefined));i[o]=void 0;break;case"literal":if(s&&o===t){i[o]=this._simpleFieldValues[n];break}if(a!==this._simpleFieldValues[n])throw new Error(r().objectField(o,this.getSimpleFieldError(c,this._simpleFieldValues[n],a)));i[o]=this._simpleFieldValues[n];break;case"passthrough":i[o]=a;break;default:throw new Error(r().objectField(o,r().invalidObject))}}return i}get config(){return this._config}get shapeKeys(){return this._shapeKeys}get shapeKeysSet(){return this._shapeKeysSet}static create(e){return new VldObject({shape:e})}parse(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(this._config.errorMessage||r().invalidObject);return this.parseObjectValue(e)}parseKnownObject(e,t){return this.parseObjectValue(e,t)}parseTrustedKnownObject(e,t){return this.parseObjectValue(e,t,!0)}parseObjectValue(e,t,s=!1){if(this._canUseSimpleObjectFastPath)return this.parseSimpleObjectValue(e,t,s);const i={};let n="";try{for(let o=0;o<this._shapeKeys.length;o++){if(n=this._shapeKeys[o],s&&n===t&&"literal"===this._simpleFieldModes[o]){i[n]=this._simpleFieldValues[o];continue}const a=this._simpleFieldModes[o];if(void 0!==a){const t=e[n];switch(a){case"string":if("string"!=typeof t)throw new Error(r().invalidString);i[n]=t;continue;case"number":if("number"!=typeof t||isNaN(t))throw new Error(r().invalidNumber);i[n]=t;continue;case"boolean":if("boolean"!=typeof t)throw new Error(r().invalidBoolean);i[n]=t;continue;case"bigint":if("bigint"!=typeof t)throw new Error(r().invalidBigint);i[n]=t;continue;case"symbol":if("symbol"!=typeof t)throw new Error(r().invalidSymbol);i[n]=t;continue;case"null":if(null!==t)throw new Error(this.getSimpleFieldError(a,void 0,t));i[n]=null;continue;case"undefinedValue":if(void 0!==t)throw new Error(r().expectedUndefined);i[n]=void 0;continue;case"literal":if(t!==this._simpleFieldValues[o])throw new Error(this.getSimpleFieldError(a,this._simpleFieldValues[o],t));i[n]=this._simpleFieldValues[o];continue;case"passthrough":i[n]=t;continue}continue}const c=this.getFieldValidator(n,o);i[n]=this.parseCheckedField(c,this._validatorTypes[o],e[n])}}catch(e){if(e instanceof a)throw new a(e.issues.map(e=>({...e,path:e.path?[n,...e.path]:[n],message:r().objectField(n,e.message.startsWith("Invalid field")?e.message.replace(/^Invalid field "[^"]+": /,""):e.message)})));const t=e instanceof Error?e.message:String(e);throw new a([{code:"invalid_type",path:[n],message:t.startsWith("Invalid field")?t:r().objectField(n,t)}])}if(this._config.strict||this._config.passthrough||this._config.catchall){const t=Object.keys(e);if(this._config.strict){const e=[];for(let s=0;s<t.length;s++){const i=t[s];this._shapeKeysSet.has(i)||e.push(i)}if(e.length>0)throw new Error(r().unexpectedKeys(e))}if(this._config.passthrough)for(let s=0;s<t.length;s++){const r=t[s];this._shapeKeysSet.has(r)||o(r)||(i[r]=e[r])}if(this._config.catchall)for(let s=0;s<t.length;s++){const n=t[s];if(!this._shapeKeysSet.has(n)&&!o(n))try{i[n]=this._config.catchall.parse(e[n])}catch(e){throw new Error(r().objectField(n,e.message))}}}return i}safeParse(t){if("object"!=typeof t||null===t||Array.isArray(t))return{success:!1,error:this.createSafeParseError(this._config.errorMessage||r().invalidObject)};const s=t;if(this._canUseSimpleObjectFastPath)try{return{success:!0,data:this.parseSimpleObjectValue(s)}}catch{}if(this._canUseSafeParseFastPath)try{return{success:!0,data:this.parseObjectValue(s)}}catch{}const i={},n=[];for(let t=0;t<this._shapeKeys.length;t++){const o=this._shapeKeys[t],a=s[o],c=this._simpleFieldModes[t];if(void 0!==c){if("string"===c&&"string"==typeof a||"number"===c&&"number"==typeof a&&!isNaN(a)||"boolean"===c&&"boolean"==typeof a||"bigint"===c&&"bigint"==typeof a||"symbol"===c&&"symbol"==typeof a||"null"===c&&null===a||"undefinedValue"===c&&void 0===a||"literal"===c&&a===this._simpleFieldValues[t]||"passthrough"===c){i[o]="literal"===c?this._simpleFieldValues[t]:"null"===c?null:a;continue}n.push({code:"invalid_type",path:[o],message:r().objectField(o,this.getSimpleFieldError(c,this._simpleFieldValues[t],a))});continue}let l;try{l=this.getFieldValidator(o,t)}catch(e){const t=this.createSafeParseError(e,o);n.push(...t.issues);continue}try{if(l instanceof e)i[o]=this.parseCheckedField(l,this._validatorTypes[t],a);else{const e=l.safeParse(a);if(e.success)i[o]=e.data;else{const t=this.createSafeParseError(e.error,o);n.push(...t.issues)}}}catch(e){const t=this.createSafeParseError(e,o);n.push(...t.issues)}}if(this._config.strict||this._config.passthrough||this._config.catchall){const t=Object.keys(s);if(this._config.strict){const e=[];for(let s=0;s<t.length;s++){const i=t[s];this._shapeKeysSet.has(i)||e.push(i)}e.length>0&&n.push(...e.map(e=>({code:"unrecognized_keys",path:[e],message:r().unexpectedKeys([e])})))}if(this._config.passthrough)for(let e=0;e<t.length;e++){const r=t[e];this._shapeKeysSet.has(r)||o(r)||(i[r]=s[r])}if(this._config.catchall){const r=this._config.catchall;for(let a=0;a<t.length;a++){const c=t[a];if(!this._shapeKeysSet.has(c)&&!o(c))try{if(r instanceof e)i[c]=r.parse(s[c]);else{const e=r.safeParse(s[c]);if(e.success)i[c]=e.data;else{const t=this.createSafeParseError(e.error,c);n.push(...t.issues)}}}catch(e){const t=this.createSafeParseError(e,c);n.push(...t.issues)}}}}return n.length>0?{success:!1,error:new a(n)}:{success:!0,data:i}}encode(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(this._config.errorMessage||r().invalidObject);return this.encodeObjectSync(e)}safeEncode(e){try{return{success:!0,data:this.encode(e)}}catch(e){return{success:!1,error:this.createSafeParseError(e.message)}}}async encodeAsync(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(this._config.errorMessage||r().invalidObject);return this.encodeObjectAsync(e)}async safeEncodeAsync(e){try{return{success:!0,data:await this.encodeAsync(e)}}catch(e){return{success:!1,error:this.createSafeParseError(e.message)}}}encodeObjectSync(e){const t={};for(let s=0;s<this._shapeKeys.length;s++){const i=this._shapeKeys[s],r=this.getFieldValidator(i,s).encode(e[i]);(void 0!==r||Object.prototype.hasOwnProperty.call(e,i))&&(t[i]=r)}return this.encodeExtraKeysSync(e,t),t}async encodeObjectAsync(e){const t={};for(let s=0;s<this._shapeKeys.length;s++){const i=this._shapeKeys[s],r=await this.getFieldValidator(i,s).encodeAsync(e[i]);(void 0!==r||Object.prototype.hasOwnProperty.call(e,i))&&(t[i]=r)}const s=this.getEncodedExtraKeys(e);for(const i of s)o(i)||(this._config.catchall?t[i]=await this._config.catchall.encodeAsync(e[i]):this._config.passthrough&&(t[i]=e[i]));return t}getEncodedExtraKeys(e){const t=Object.keys(e).filter(e=>!this._shapeKeysSet.has(e));if(this._config.strict&&t.length>0)throw new Error(r().unexpectedKeys(t));return t}encodeExtraKeysSync(e,t){for(const s of this.getEncodedExtraKeys(e))o(s)||(this._config.catchall?t[s]=this._config.catchall.encode(e[s]):this._config.passthrough&&(t[s]=e[s]))}strict(e){return new VldObject({...this._config,strict:!0,passthrough:!1,errorMessage:e})}passthrough(){return new VldObject({...this._config,strict:!1,passthrough:!0})}loose(){return this.passthrough()}strip(){const e={...this._config,strict:!1,passthrough:!1};return delete e.catchall,new VldObject(e)}partial(){const e={};for(const t in this._config.shape)e[t]=new s(this._config.shape[t]);return new VldObject({...this._config,shape:e})}deepPartial(){const e={};for(const t in this._config.shape){const i=this._config.shape[t];e[t]=new s(i instanceof VldObject?i.deepPartial():i)}return new VldObject({...this._config,shape:e})}exactPartial(){const e={};for(const t in this._config.shape)e[t]=new i(this._config.shape[t]);return new VldObject({...this._config,shape:e})}pick(...e){const t={};let s=[];s=1!==e.length||"object"!=typeof e[0]||null===e[0]||Array.isArray(e[0])?e.flat():Object.keys(e[0]).filter(t=>e[0][t]);for(const e of s)e in this._config.shape&&(t[e]=this._config.shape[e]);return new VldObject({...this._config,shape:t})}omit(...e){let t;t=1!==e.length||"object"!=typeof e[0]||null===e[0]||Array.isArray(e[0])?new Set(e.flat()):new Set(Object.keys(e[0]).filter(t=>e[0][t]));const s={};for(const e in this._config.shape)t.has(e)||(s[e]=this._config.shape[e]);return new VldObject({...this._config,shape:s})}extend(e){return new VldObject({...this._config,shape:{...this._config.shape,...e}})}merge(e){return new VldObject({...this._config,shape:{...this._config.shape,...e.config.shape}})}required(){const e={};for(const t in this._config.shape){const i=this._config.shape[t];if(i instanceof s){const s=i.baseValidator;if(!s||"function"!=typeof s.parse)throw new Error(`Invalid VldOptional structure for field "${t}": missing or invalid baseValidator`);e[t]=s}else e[t]=i}return new VldObject({...this._config,shape:e})}catchall(e){return new VldObject({...this._config,catchall:e,passthrough:!1})}get shape(){return this._config.shape}keyof(){const e=Object.keys(this._config.shape);if(0===e.length)throw new Error("Cannot create keyof enum from empty object");return n.create(e)}safeExtend(e){const t=new Set(Object.keys(this._config.shape)),s=Object.keys(e),i=[];for(const e of s)t.has(e)&&i.push(e);if(i.length>0)throw new Error(`safeExtend: ${r().safeExtendOverlap(i)}`);return new VldObject({...this._config,shape:{...this._config.shape,...e}})}}export{VldObject};
@@ -1 +1 @@
1
- import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{getMessages as t}from"../locales/runtime.js";import{i}from"../chunks/security-Dy24T8N7.js";import{V as a}from"../chunks/errors-core-CerMg2rq.js";function o(e){return new a([{code:"invalid_object",path:[],message:e}])}class VldRecord extends e{constructor(e,t,i){super(r.RECORD),this.valueValidator=e,this.keyValidator=t,this.errorMessage=i,this._simpleValueMode=this.getSimpleValueMode(e),this._simpleValue="literal"===this._simpleValueMode?e.literal:void 0}get valueSchema(){return this.valueValidator}get keySchema(){return this.keyValidator}get valueType(){return this.valueValidator}get keyType(){return this.keyValidator}static create(e,r){return new VldRecord(e,r)}getSimpleValueMode(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}}getSimpleValueError(e){switch(this._simpleValueMode){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 e;case"undefinedValue":return t().expectedUndefined;case"literal":return t().literalExpected(JSON.stringify(this._simpleValue),JSON.stringify(e));default:return t().invalidRecord}}parse(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(this.errorMessage||t().invalidRecord);return this.parseKnownRecord(e)}parseKnownRecord(e){if(void 0!==this.keyValidator)return this.parseKeyedRecord(e);const r={},a=Object.keys(e),o=this._simpleValueMode;for(let s=0;s<a.length;s++){const n=a[s];if(i(n))continue;const l=e[n];if(void 0!==o)switch(o){case"string":if("string"!=typeof l)throw new Error(t().objectField(n,t().invalidString));r[n]=l;continue;case"number":if("number"!=typeof l||isNaN(l))throw new Error(t().objectField(n,t().invalidNumber));r[n]=l;continue;case"boolean":if("boolean"!=typeof l)throw new Error(t().objectField(n,t().invalidBoolean));r[n]=l;continue;case"bigint":if("bigint"!=typeof l)throw new Error(t().objectField(n,t().invalidBigint));r[n]=l;continue;case"symbol":if("symbol"!=typeof l)throw new Error(t().objectField(n,t().invalidSymbol));r[n]=l;continue;case"null":if(null!==l)throw new Error(t().objectField(n,this.getSimpleValueError(l)));r[n]=null;continue;case"undefinedValue":if(void 0!==l)throw new Error(t().objectField(n,t().expectedUndefined));r[n]=void 0;continue;case"literal":if(l!==this._simpleValue)throw new Error(t().objectField(n,this.getSimpleValueError(l)));r[n]=this._simpleValue;continue;case"passthrough":r[n]=l;continue}try{r[n]=this.valueValidator.parse(l)}catch(e){throw new Error(t().objectField(n,e.message))}}return r}parseKeyedRecord(e){const r={},o=Reflect.ownKeys(e);for(const s of o){if(!Object.prototype.propertyIsEnumerable.call(e,s))continue;if("string"==typeof s&&i(s))continue;const o=this.keyValidator.safeParse(s);if(!o.success)throw new a([{code:"invalid_key",path:[String(s)],message:o.error.message}]);if("string"!=typeof o.data&&"number"!=typeof o.data&&"symbol"!=typeof o.data)throw new a([{code:"invalid_key",path:[String(s)],message:"Record key transform must return a property key"}]);if("string"!=typeof o.data||!i(o.data))try{r[o.data]=this.valueValidator.parse(e[s])}catch(e){throw new Error(t().objectField(String(s),e.message))}}return r}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return e instanceof a?{success:!1,error:e}:{success:!1,error:o(e.message)}}}partial(){const e=this.valueValidator.optional();return new VldRecord(e,this.keyValidator)}loose(){return new VldLooseRecord(this.valueValidator,this.keyValidator)}}class VldLooseRecord extends e{constructor(e,t){super(r.RECORD),this.valueValidator=e,this.keyValidator=t}get valueSchema(){return this.valueValidator}parse(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(t().invalidRecord);const r={},a=e;for(const e of Reflect.ownKeys(a)){if(!Object.prototype.propertyIsEnumerable.call(a,e))continue;if("string"==typeof e&&i(e))continue;const t=this.keyValidator?.safeParse(e);if(t&&!t.success)continue;const o=t?.data??e;if("string"!=typeof o&&"number"!=typeof o&&"symbol"!=typeof o)continue;if("string"==typeof o&&i(o))continue;const s=this.valueValidator.safeParse(a[e]);s.success&&(r[o]=s.data)}return r}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:o(e.message)}}}partial(){const e=this.valueValidator.optional();return new VldLooseRecord(e,this.keyValidator)}loose(){return this}}export{VldRecord};
1
+ import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{getMessages as t}from"../locales/runtime.js";import{i}from"../chunks/security-Dy24T8N7.js";import{V as a}from"../chunks/errors-core-BU14-Dk8.js";function o(e){return new a([{code:"invalid_object",path:[],message:e}])}class VldRecord extends e{constructor(e,t,i){super(r.RECORD),this.valueValidator=e,this.keyValidator=t,this.errorMessage=i,this._simpleValueMode=this.getSimpleValueMode(e),this._simpleValue="literal"===this._simpleValueMode?e.literal:void 0}get valueSchema(){return this.valueValidator}get keySchema(){return this.keyValidator}get valueType(){return this.valueValidator}get keyType(){return this.keyValidator}static create(e,r){return new VldRecord(e,r)}getSimpleValueMode(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}}getSimpleValueError(e){switch(this._simpleValueMode){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 e;case"undefinedValue":return t().expectedUndefined;case"literal":return t().literalExpected(JSON.stringify(this._simpleValue),JSON.stringify(e));default:return t().invalidRecord}}parse(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(this.errorMessage||t().invalidRecord);return this.parseKnownRecord(e)}parseKnownRecord(e){if(void 0!==this.keyValidator)return this.parseKeyedRecord(e);const r={},a=Object.keys(e),o=this._simpleValueMode;for(let s=0;s<a.length;s++){const n=a[s];if(i(n))continue;const l=e[n];if(void 0!==o)switch(o){case"string":if("string"!=typeof l)throw new Error(t().objectField(n,t().invalidString));r[n]=l;continue;case"number":if("number"!=typeof l||isNaN(l))throw new Error(t().objectField(n,t().invalidNumber));r[n]=l;continue;case"boolean":if("boolean"!=typeof l)throw new Error(t().objectField(n,t().invalidBoolean));r[n]=l;continue;case"bigint":if("bigint"!=typeof l)throw new Error(t().objectField(n,t().invalidBigint));r[n]=l;continue;case"symbol":if("symbol"!=typeof l)throw new Error(t().objectField(n,t().invalidSymbol));r[n]=l;continue;case"null":if(null!==l)throw new Error(t().objectField(n,this.getSimpleValueError(l)));r[n]=null;continue;case"undefinedValue":if(void 0!==l)throw new Error(t().objectField(n,t().expectedUndefined));r[n]=void 0;continue;case"literal":if(l!==this._simpleValue)throw new Error(t().objectField(n,this.getSimpleValueError(l)));r[n]=this._simpleValue;continue;case"passthrough":r[n]=l;continue}try{r[n]=this.valueValidator.parse(l)}catch(e){throw new Error(t().objectField(n,e.message))}}return r}parseKeyedRecord(e){const r={},o=Reflect.ownKeys(e);for(const s of o){if(!Object.prototype.propertyIsEnumerable.call(e,s))continue;if("string"==typeof s&&i(s))continue;const o=this.keyValidator.safeParse(s);if(!o.success)throw new a([{code:"invalid_key",path:[String(s)],message:o.error.message}]);if("string"!=typeof o.data&&"number"!=typeof o.data&&"symbol"!=typeof o.data)throw new a([{code:"invalid_key",path:[String(s)],message:"Record key transform must return a property key"}]);if("string"!=typeof o.data||!i(o.data))try{r[o.data]=this.valueValidator.parse(e[s])}catch(e){throw new Error(t().objectField(String(s),e.message))}}return r}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return e instanceof a?{success:!1,error:e}:{success:!1,error:o(e.message)}}}partial(){const e=this.valueValidator.optional();return new VldRecord(e,this.keyValidator)}loose(){return new VldLooseRecord(this.valueValidator,this.keyValidator)}}class VldLooseRecord extends e{constructor(e,t){super(r.RECORD),this.valueValidator=e,this.keyValidator=t}get valueSchema(){return this.valueValidator}parse(e){if("object"!=typeof e||null===e||Array.isArray(e))throw new Error(t().invalidRecord);const r={},a=e;for(const e of Reflect.ownKeys(a)){if(!Object.prototype.propertyIsEnumerable.call(a,e))continue;if("string"==typeof e&&i(e))continue;const t=this.keyValidator?.safeParse(e);if(t&&!t.success)continue;const o=t?.data??e;if("string"!=typeof o&&"number"!=typeof o&&"symbol"!=typeof o)continue;if("string"==typeof o&&i(o))continue;const s=this.valueValidator.safeParse(a[e]);s.success&&(r[o]=s.data)}return r}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:o(e.message)}}}partial(){const e=this.valueValidator.optional();return new VldLooseRecord(e,this.keyValidator)}loose(){return this}}export{VldRecord};
@@ -1 +1 @@
1
- import{VldBase as e,VLD_VALIDATOR_TYPES as t}from"./base.js";import{getMessages as r}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-CerMg2rq.js";class VldSet extends e{constructor(e,r,s=[]){super(t.SET),this.itemValidator=e,this.errorMessage=r,this._checks=s,this._simpleItemMode=this.getSimpleItemMode(e),this._simpleItemValue="literal"===this._simpleItemMode?e.literal:void 0}get itemSchema(){return this.itemValidator}static create(e){return new VldSet(e)}getSimpleItemMode(e){if(!0===e.isSimple)switch(e.validatorType){case t.STRING:return"string";case t.NUMBER:return"number";case t.BOOLEAN:return"boolean";case t.BIGINT:return"bigint";case t.SYMBOL:return"symbol";case t.NULL:return"null";case t.UNDEFINED:case t.VOID:return"undefinedValue";case t.LITERAL:return"literal";case t.ANY:case t.UNKNOWN:return"passthrough";default:return}}getSimpleItemError(e){switch(this._simpleItemMode){case"string":return r().invalidString;case"number":return r().invalidNumber;case"boolean":return r().invalidBoolean;case"bigint":return r().invalidBigint;case"symbol":return r().invalidSymbol;case"null":return"Expected null, received "+typeof e;case"undefinedValue":return r().expectedUndefined;case"literal":return r().literalExpected(JSON.stringify(this._simpleItemValue),JSON.stringify(e));default:return r().invalidSet}}parse(e){if(!(e instanceof Set))throw new Error(this.errorMessage||r().invalidSet);return this.parseKnownSet(e)}parseKnownSet(e){const t=new Set,r=this._simpleItemMode;if("string"===r)for(const r of e){if("string"!=typeof r)throw new Error(this.getSimpleItemError(r));t.add(r)}else if("number"===r)for(const r of e){if("number"!=typeof r||isNaN(r))throw new Error(this.getSimpleItemError(r));t.add(r)}else if("boolean"===r)for(const r of e){if("boolean"!=typeof r)throw new Error(this.getSimpleItemError(r));t.add(r)}else if("bigint"===r)for(const r of e){if("bigint"!=typeof r)throw new Error(this.getSimpleItemError(r));t.add(r)}else if("symbol"===r)for(const r of e){if("symbol"!=typeof r)throw new Error(this.getSimpleItemError(r));t.add(r)}else if("null"===r)for(const r of e){if(null!==r)throw new Error(this.getSimpleItemError(r));t.add(null)}else if("undefinedValue"===r)for(const r of e){if(void 0!==r)throw new Error(this.getSimpleItemError(r));t.add(void 0)}else if("literal"===r)for(const r of e){if(r!==this._simpleItemValue)throw new Error(this.getSimpleItemError(r));t.add(this._simpleItemValue)}else if("passthrough"===r)for(const r of e)t.add(r);else for(const r of e)try{t.add(this.itemValidator.parse(r))}catch(e){throw new Error(e.message)}for(const e of this._checks)if(!e.fn(t))throw new Error(e.message);return t}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:(t=e.message,new s([{code:"invalid_type",path:[],message:t}]))}}var t}min(e,t){return new VldSet(this.itemValidator,this.errorMessage,[...this._checks,{fn:t=>t.size>=e,message:t||`Set must contain at least ${e} elements`}])}max(e,t){return new VldSet(this.itemValidator,this.errorMessage,[...this._checks,{fn:t=>t.size<=e,message:t||`Set must contain at most ${e} elements`}])}size(e,t){return new VldSet(this.itemValidator,this.errorMessage,[...this._checks,{fn:t=>t.size===e,message:t||`Set must contain exactly ${e} elements`}])}nonempty(e){return this.min(1,e||"Set cannot be empty")}}export{VldSet};
1
+ import{VldBase as e,VLD_VALIDATOR_TYPES as t}from"./base.js";import{getMessages as r}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-BU14-Dk8.js";class VldSet extends e{constructor(e,r,s=[]){super(t.SET),this.itemValidator=e,this.errorMessage=r,this._checks=s,this._simpleItemMode=this.getSimpleItemMode(e),this._simpleItemValue="literal"===this._simpleItemMode?e.literal:void 0}get itemSchema(){return this.itemValidator}static create(e){return new VldSet(e)}getSimpleItemMode(e){if(!0===e.isSimple)switch(e.validatorType){case t.STRING:return"string";case t.NUMBER:return"number";case t.BOOLEAN:return"boolean";case t.BIGINT:return"bigint";case t.SYMBOL:return"symbol";case t.NULL:return"null";case t.UNDEFINED:case t.VOID:return"undefinedValue";case t.LITERAL:return"literal";case t.ANY:case t.UNKNOWN:return"passthrough";default:return}}getSimpleItemError(e){switch(this._simpleItemMode){case"string":return r().invalidString;case"number":return r().invalidNumber;case"boolean":return r().invalidBoolean;case"bigint":return r().invalidBigint;case"symbol":return r().invalidSymbol;case"null":return"Expected null, received "+typeof e;case"undefinedValue":return r().expectedUndefined;case"literal":return r().literalExpected(JSON.stringify(this._simpleItemValue),JSON.stringify(e));default:return r().invalidSet}}parse(e){if(!(e instanceof Set))throw new Error(this.errorMessage||r().invalidSet);return this.parseKnownSet(e)}parseKnownSet(e){const t=new Set,r=this._simpleItemMode;if("string"===r)for(const r of e){if("string"!=typeof r)throw new Error(this.getSimpleItemError(r));t.add(r)}else if("number"===r)for(const r of e){if("number"!=typeof r||isNaN(r))throw new Error(this.getSimpleItemError(r));t.add(r)}else if("boolean"===r)for(const r of e){if("boolean"!=typeof r)throw new Error(this.getSimpleItemError(r));t.add(r)}else if("bigint"===r)for(const r of e){if("bigint"!=typeof r)throw new Error(this.getSimpleItemError(r));t.add(r)}else if("symbol"===r)for(const r of e){if("symbol"!=typeof r)throw new Error(this.getSimpleItemError(r));t.add(r)}else if("null"===r)for(const r of e){if(null!==r)throw new Error(this.getSimpleItemError(r));t.add(null)}else if("undefinedValue"===r)for(const r of e){if(void 0!==r)throw new Error(this.getSimpleItemError(r));t.add(void 0)}else if("literal"===r)for(const r of e){if(r!==this._simpleItemValue)throw new Error(this.getSimpleItemError(r));t.add(this._simpleItemValue)}else if("passthrough"===r)for(const r of e)t.add(r);else for(const r of e)try{t.add(this.itemValidator.parse(r))}catch(e){throw new Error(e.message)}for(const e of this._checks)if(!e.fn(t))throw new Error(e.message);return t}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:(t=e.message,new s([{code:"invalid_type",path:[],message:t}]))}}var t}min(e,t){return new VldSet(this.itemValidator,this.errorMessage,[...this._checks,{fn:t=>t.size>=e,message:t||`Set must contain at least ${e} elements`}])}max(e,t){return new VldSet(this.itemValidator,this.errorMessage,[...this._checks,{fn:t=>t.size<=e,message:t||`Set must contain at most ${e} elements`}])}size(e,t){return new VldSet(this.itemValidator,this.errorMessage,[...this._checks,{fn:t=>t.size===e,message:t||`Set must contain exactly ${e} elements`}])}nonempty(e){return this.min(1,e||"Set cannot be empty")}}export{VldSet};
@@ -1 +1 @@
1
- import{VldBase as t,VLD_VALIDATOR_TYPES as a}from"./base.js";import{getMessages as e}from"../locales/runtime.js";import{i as r}from"../chunks/ip-validation-g5LWgy9t.js";import{V as i,c as n,g as d}from"../chunks/errors-core-CerMg2rq.js";const o="(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))";function s(t,a){return t.lastIndex=0,t.test(a)}function c(t,a){return new RegExp(`^[A-Za-z0-9+/]{${t}}${a}$`)}function u(t){return new RegExp(`^[A-Za-z0-9_-]{${t}}$`)}function f(t){const a="(?:[01]\\d|2[0-3]):[0-5]\\d";return"number"!=typeof t.precision?`${a}(?::[0-5]\\d(?:\\.\\d+)?)?`:-1===t.precision?a:0===t.precision?`${a}:[0-5]\\d`:`${a}:[0-5]\\d\\.\\d{${t.precision}}`}function m(t){return void 0===t?/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/:new RegExp(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${t}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`)}const l={cuid:/^[cC][0-9a-z]{6,}$/,cuid2:/^[0-9a-z]+$/,ulid:/^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$/,xid:/^[0-9a-vA-V]{20}$/,ksuid:/^[A-Za-z0-9]{27}$/,nanoid:/^[a-zA-Z0-9_-]{21}$/,nanoidOfLength:t=>new RegExp(`^[a-zA-Z0-9_-]{${t}}$`),duration:/^P(?:(\d+W)|(?!.*W)(?=\d|T\d)(\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+([.,]\d+)?S)?)?)$/,extendedDuration:/^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/,guid:/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/,creditCard:/^\d(?:[ -]?\d){11,18}$/,uuid:m,uuid4:m(4),uuid6:m(6),uuid7:m(7),email:/^(?!\.)(?!.*\.\.)([A-Za-z0-9_'+.-]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9-]*\.)+[A-Za-z]{2,}$/,html5Email:/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,rfc5322Email:/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}))$/,unicodeEmail:/^[^\s@"]{1,64}@[^\s@]{1,255}$/u,idnEmail:/^[^\s@"]{1,64}@[^\s@]{1,255}$/u,browserEmail:/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,emoji:()=>/^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u,ipv4:/^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/,ipv6:/^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$/,mac:function(t=":"){const a=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return new RegExp(`^(?:[0-9A-F]{2}${a}){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}${a}){5}[0-9a-f]{2}$`)},cidrv4:/^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/,cidrv6:/^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:?){0,6})\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,base64:/^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/,base64url:/^[A-Za-z0-9_-]*$/,hostname:/^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/,domain:/^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/,httpProtocol:/^https?$/,e164:/^\+[1-9]\d{6,14}$/,date:new RegExp(`^${o}$`),time:function(t={}){return new RegExp(`^${f(t)}$`)},datetime:function(t={}){const a=["Z"];return t.local&&a.push(""),t.offset&&a.push("([+-](?:[01]\\d|2[0-3]):[0-5]\\d)"),new RegExp(`^${o}T${f(t)}(?:${a.join("|")})$`)},string:function(t){const a=t?`[\\s\\S]{${t.minimum??0},${t.maximum??""}}`:"[\\s\\S]*";return new RegExp(`^${a}$`)},bigint:/^-?\d+n?$/,integer:/^-?\d+$/,number:/^-?\d+(?:\.\d+)?$/,boolean:/^(?:true|false)$/i,null:/^null$/i,undefined:/^undefined$/i,lowercase:/^[^A-Z]*$/,uppercase:/^[^a-z]*$/,hex:/^[0-9a-fA-F]*$/,md5_hex:/^[0-9a-fA-F]{32}$/,md5_base64:c(22,"=="),md5_base64url:u(22),sha1_hex:/^[0-9a-fA-F]{40}$/,sha1_base64:c(27,"="),sha1_base64url:u(27),sha256_hex:/^[0-9a-fA-F]{64}$/,sha256_base64:c(43,"="),sha256_base64url:u(43),sha384_hex:/^[0-9a-fA-F]{96}$/,sha384_base64:c(64,""),sha384_base64url:u(64),sha512_hex:/^[0-9a-fA-F]{128}$/,sha512_base64:c(86,"=="),sha512_base64url:u(86),jwt:/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/,httpUrl:/^https?:\/\/[^\s/$.?#].[^\s]*$/,md5:/^[a-f0-9]{32}$/i,sha1:/^[a-f0-9]{40}$/i,sha256:/^[a-f0-9]{64}$/i,sha384:/^[a-f0-9]{96}$/i,sha512:/^[a-f0-9]{128}$/i};class VldStringFormat extends t{constructor(t,e,r,i=t=>t,n){super(a.STRING_FORMAT),this._format=t,this._validator=e,this._errorMessage=r,this._normalize=i,this._patternSource=n}static create(t,a,e,r,i){return new VldStringFormat(t,a,e,r,i)}_createFormatIssue(){const t={code:"invalid_format",path:[],origin:"string",format:this._format,message:this._errorMessage||`Invalid ${this._format}`};return this._patternSource&&(t.pattern=this._patternSource),new i([t])}parse(t){if("string"!=typeof t)throw new i([n("string",d(t),e().invalidString)]);if(!this._validator(t))throw this._createFormatIssue();return this._normalize(t)}safeParse(t){return"string"!=typeof t?{success:!1,error:new i([n("string",d(t),e().invalidString)])}:this._validator(t)?{success:!0,data:this._normalize(t)}:{success:!1,error:this._createFormatIssue()}}}const $=t=>{const a=t?.pattern??l.email;return VldStringFormat.create("email",t=>s(a,t),void 0,void 0,a.source)},A=(t={})=>VldStringFormat.create("url",a=>{const e=(t=>{try{return new URL(t)}catch{return}})(a);return void 0!==e&&(!t.hostname||s(t.hostname,e.hostname))&&(!t.protocol||s(t.protocol,e.protocol.slice(0,-1)))},void 0,t.normalize?t=>new URL(t).href:void 0),F=t=>{const a=m(void 0===t?.version?void 0:Number(t.version.slice(1)));return VldStringFormat.create("uuid",t=>s(a,t))},g=()=>VldStringFormat.create("uuid",t=>s(l.uuid4,t)),h=()=>VldStringFormat.create("uuid",t=>s(l.uuid6,t)),p=()=>VldStringFormat.create("uuid",t=>s(l.uuid7,t)),S=()=>VldStringFormat.create("hostname",t=>s(l.hostname,t)),z=()=>VldStringFormat.create("emoji",t=>s(l.emoji(),t)),Z=()=>VldStringFormat.create("base64",t=>s(l.base64,t)),_=()=>VldStringFormat.create("base64url",t=>s(l.base64url,t)),V=()=>VldStringFormat.create("hex",t=>s(l.hex,t)),v=()=>VldStringFormat.create("jwt",t=>s(l.jwt,t)),x=()=>VldStringFormat.create("nanoid",t=>s(l.nanoid,t)),b=()=>VldStringFormat.create("cuid",t=>s(l.cuid,t)),w=()=>VldStringFormat.create("cuid2",t=>s(l.cuid2,t)),E=()=>VldStringFormat.create("ulid",t=>s(l.ulid,t)),R=()=>VldStringFormat.create("ipv4",t=>s(l.ipv4,t)),j=()=>VldStringFormat.create("ipv6",t=>s(l.ipv6,t)),I=t=>{const a=l.mac(t?.delimiter);return VldStringFormat.create("mac",t=>s(a,t))},M=()=>VldStringFormat.create("cidrv4",t=>s(l.cidrv4,t)),T=()=>VldStringFormat.create("cidrv6",t=>{const a=t.lastIndexOf("/");if(a<=0)return!1;const e=Number(t.slice(a+1));return Number.isInteger(e)&&e>=0&&e<=128&&r(t.slice(0,a))}),D=()=>VldStringFormat.create("e164",t=>s(l.e164,t)),O=()=>VldStringFormat.create("xid",t=>s(l.xid,t)),k=()=>VldStringFormat.create("guid",t=>s(l.guid,t)),C=()=>VldStringFormat.create("httpUrl",t=>s(l.httpUrl,t)),P=()=>VldStringFormat.create("ksuid",t=>s(l.ksuid,t)),N=t=>VldStringFormat.create("hash",a=>l[t]?.test(a)??!1,`Invalid ${t} hash`),U=/[ -]/g;function y(t){return!!l.creditCard.test(t)&&function(t){let a=0,e=!1;for(let r=t.length-1;r>=0;r--){let i=t.charCodeAt(r)-48;e&&(i*=2,i>9&&(i-=9)),a+=i,e=!e}return a%10==0}(t.replace(U,""))}const H=t=>VldStringFormat.create("credit_card",y,t?.message,void 0,l.creditCard.source),W={ZodISODate:VldStringFormat,ZodISODateTime:VldStringFormat,ZodISODuration:VldStringFormat,ZodISOTime:VldStringFormat,date:()=>VldStringFormat.create("date",t=>s(l.date,t)),time:(t={})=>{const a=l.time(t);return VldStringFormat.create("time",t=>s(a,t))},datetime:(t={})=>{const a=l.datetime(t);return VldStringFormat.create("datetime",t=>s(a,t))},dateTime:(t={local:!0})=>{const a=l.datetime(t);return VldStringFormat.create("datetime",t=>s(a,t))},duration:()=>VldStringFormat.create("duration",t=>s(l.duration,t))},L=(t,a)=>{const e=a instanceof RegExp?t=>s(a,t):a;return VldStringFormat.create(t,e)};export{VldStringFormat,Z as base64,_ as base64url,M as cidrv4,T as cidrv6,H as creditCard,b as cuid,w as cuid2,D as e164,$ as email,z as emoji,k as guid,N as hash,V as hex,S as hostname,C as httpUrl,R as ipv4,j as ipv6,y as isValidCreditCard,W as iso,v as jwt,P as ksuid,I as mac,x as nanoid,l as regexes,L as stringFormat,E as ulid,A as url,F as uuid,g as uuidv4,h as uuidv6,p as uuidv7,O as xid};
1
+ import{VldBase as t,VLD_VALIDATOR_TYPES as a}from"./base.js";import{getMessages as e}from"../locales/runtime.js";import{i as r}from"../chunks/ip-validation-g5LWgy9t.js";import{V as i,c as n,g as d}from"../chunks/errors-core-BU14-Dk8.js";const o="(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))";function s(t,a){return t.lastIndex=0,t.test(a)}function c(t,a){return new RegExp(`^[A-Za-z0-9+/]{${t}}${a}$`)}function u(t){return new RegExp(`^[A-Za-z0-9_-]{${t}}$`)}function f(t){const a="(?:[01]\\d|2[0-3]):[0-5]\\d";return"number"!=typeof t.precision?`${a}(?::[0-5]\\d(?:\\.\\d+)?)?`:-1===t.precision?a:0===t.precision?`${a}:[0-5]\\d`:`${a}:[0-5]\\d\\.\\d{${t.precision}}`}function m(t){return void 0===t?/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/:new RegExp(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${t}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`)}const l={cuid:/^[cC][0-9a-z]{6,}$/,cuid2:/^[0-9a-z]+$/,ulid:/^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$/,xid:/^[0-9a-vA-V]{20}$/,ksuid:/^[A-Za-z0-9]{27}$/,nanoid:/^[a-zA-Z0-9_-]{21}$/,nanoidOfLength:t=>new RegExp(`^[a-zA-Z0-9_-]{${t}}$`),duration:/^P(?:(\d+W)|(?!.*W)(?=\d|T\d)(\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+([.,]\d+)?S)?)?)$/,extendedDuration:/^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/,guid:/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/,creditCard:/^\d(?:[ -]?\d){11,18}$/,uuid:m,uuid4:m(4),uuid6:m(6),uuid7:m(7),email:/^(?!\.)(?!.*\.\.)([A-Za-z0-9_'+.-]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9-]*\.)+[A-Za-z]{2,}$/,html5Email:/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,rfc5322Email:/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}))$/,unicodeEmail:/^[^\s@"]{1,64}@[^\s@]{1,255}$/u,idnEmail:/^[^\s@"]{1,64}@[^\s@]{1,255}$/u,browserEmail:/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,emoji:()=>/^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u,ipv4:/^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/,ipv6:/^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$/,mac:function(t=":"){const a=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return new RegExp(`^(?:[0-9A-F]{2}${a}){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}${a}){5}[0-9a-f]{2}$`)},cidrv4:/^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/,cidrv6:/^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:?){0,6})\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,base64:/^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/,base64url:/^[A-Za-z0-9_-]*$/,hostname:/^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/,domain:/^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/,httpProtocol:/^https?$/,e164:/^\+[1-9]\d{6,14}$/,date:new RegExp(`^${o}$`),time:function(t={}){return new RegExp(`^${f(t)}$`)},datetime:function(t={}){const a=["Z"];return t.local&&a.push(""),t.offset&&a.push("([+-](?:[01]\\d|2[0-3]):[0-5]\\d)"),new RegExp(`^${o}T${f(t)}(?:${a.join("|")})$`)},string:function(t){const a=t?`[\\s\\S]{${t.minimum??0},${t.maximum??""}}`:"[\\s\\S]*";return new RegExp(`^${a}$`)},bigint:/^-?\d+n?$/,integer:/^-?\d+$/,number:/^-?\d+(?:\.\d+)?$/,boolean:/^(?:true|false)$/i,null:/^null$/i,undefined:/^undefined$/i,lowercase:/^[^A-Z]*$/,uppercase:/^[^a-z]*$/,hex:/^[0-9a-fA-F]*$/,md5_hex:/^[0-9a-fA-F]{32}$/,md5_base64:c(22,"=="),md5_base64url:u(22),sha1_hex:/^[0-9a-fA-F]{40}$/,sha1_base64:c(27,"="),sha1_base64url:u(27),sha256_hex:/^[0-9a-fA-F]{64}$/,sha256_base64:c(43,"="),sha256_base64url:u(43),sha384_hex:/^[0-9a-fA-F]{96}$/,sha384_base64:c(64,""),sha384_base64url:u(64),sha512_hex:/^[0-9a-fA-F]{128}$/,sha512_base64:c(86,"=="),sha512_base64url:u(86),jwt:/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/,httpUrl:/^https?:\/\/[^\s/$.?#].[^\s]*$/,md5:/^[a-f0-9]{32}$/i,sha1:/^[a-f0-9]{40}$/i,sha256:/^[a-f0-9]{64}$/i,sha384:/^[a-f0-9]{96}$/i,sha512:/^[a-f0-9]{128}$/i};class VldStringFormat extends t{constructor(t,e,r,i=t=>t,n){super(a.STRING_FORMAT),this._format=t,this._validator=e,this._errorMessage=r,this._normalize=i,this._patternSource=n}static create(t,a,e,r,i){return new VldStringFormat(t,a,e,r,i)}_createFormatIssue(){const t={code:"invalid_format",path:[],origin:"string",format:this._format,message:this._errorMessage||`Invalid ${this._format}`};return this._patternSource&&(t.pattern=this._patternSource),new i([t])}parse(t){if("string"!=typeof t)throw new i([n("string",d(t),e().invalidString)]);if(!this._validator(t))throw this._createFormatIssue();return this._normalize(t)}safeParse(t){return"string"!=typeof t?{success:!1,error:new i([n("string",d(t),e().invalidString)])}:this._validator(t)?{success:!0,data:this._normalize(t)}:{success:!1,error:this._createFormatIssue()}}}const $=t=>{const a=t?.pattern??l.email;return VldStringFormat.create("email",t=>s(a,t),void 0,void 0,a.source)},A=(t={})=>VldStringFormat.create("url",a=>{const e=(t=>{try{return new URL(t)}catch{return}})(a);return void 0!==e&&(!t.hostname||s(t.hostname,e.hostname))&&(!t.protocol||s(t.protocol,e.protocol.slice(0,-1)))},void 0,t.normalize?t=>new URL(t).href:void 0),F=t=>{const a=m(void 0===t?.version?void 0:Number(t.version.slice(1)));return VldStringFormat.create("uuid",t=>s(a,t))},g=()=>VldStringFormat.create("uuid",t=>s(l.uuid4,t)),h=()=>VldStringFormat.create("uuid",t=>s(l.uuid6,t)),p=()=>VldStringFormat.create("uuid",t=>s(l.uuid7,t)),S=()=>VldStringFormat.create("hostname",t=>s(l.hostname,t)),z=()=>VldStringFormat.create("emoji",t=>s(l.emoji(),t)),Z=()=>VldStringFormat.create("base64",t=>s(l.base64,t)),_=()=>VldStringFormat.create("base64url",t=>s(l.base64url,t)),V=()=>VldStringFormat.create("hex",t=>s(l.hex,t)),v=()=>VldStringFormat.create("jwt",t=>s(l.jwt,t)),x=()=>VldStringFormat.create("nanoid",t=>s(l.nanoid,t)),b=()=>VldStringFormat.create("cuid",t=>s(l.cuid,t)),w=()=>VldStringFormat.create("cuid2",t=>s(l.cuid2,t)),E=()=>VldStringFormat.create("ulid",t=>s(l.ulid,t)),R=()=>VldStringFormat.create("ipv4",t=>s(l.ipv4,t)),j=()=>VldStringFormat.create("ipv6",t=>s(l.ipv6,t)),I=t=>{const a=l.mac(t?.delimiter);return VldStringFormat.create("mac",t=>s(a,t))},M=()=>VldStringFormat.create("cidrv4",t=>s(l.cidrv4,t)),T=()=>VldStringFormat.create("cidrv6",t=>{const a=t.lastIndexOf("/");if(a<=0)return!1;const e=Number(t.slice(a+1));return Number.isInteger(e)&&e>=0&&e<=128&&r(t.slice(0,a))}),D=()=>VldStringFormat.create("e164",t=>s(l.e164,t)),O=()=>VldStringFormat.create("xid",t=>s(l.xid,t)),k=()=>VldStringFormat.create("guid",t=>s(l.guid,t)),C=()=>VldStringFormat.create("httpUrl",t=>s(l.httpUrl,t)),P=()=>VldStringFormat.create("ksuid",t=>s(l.ksuid,t)),N=t=>VldStringFormat.create("hash",a=>l[t]?.test(a)??!1,`Invalid ${t} hash`),U=/[ -]/g;function y(t){return!!l.creditCard.test(t)&&function(t){let a=0,e=!1;for(let r=t.length-1;r>=0;r--){let i=t.charCodeAt(r)-48;e&&(i*=2,i>9&&(i-=9)),a+=i,e=!e}return a%10==0}(t.replace(U,""))}const H=t=>VldStringFormat.create("credit_card",y,t?.message,void 0,l.creditCard.source),W={ZodISODate:VldStringFormat,ZodISODateTime:VldStringFormat,ZodISODuration:VldStringFormat,ZodISOTime:VldStringFormat,date:()=>VldStringFormat.create("date",t=>s(l.date,t)),time:(t={})=>{const a=l.time(t);return VldStringFormat.create("time",t=>s(a,t))},datetime:(t={})=>{const a=l.datetime(t);return VldStringFormat.create("datetime",t=>s(a,t))},dateTime:(t={local:!0})=>{const a=l.datetime(t);return VldStringFormat.create("datetime",t=>s(a,t))},duration:()=>VldStringFormat.create("duration",t=>s(l.duration,t))},L=(t,a)=>{const e=a instanceof RegExp?t=>s(a,t):a;return VldStringFormat.create(t,e)};export{VldStringFormat,Z as base64,_ as base64url,M as cidrv4,T as cidrv6,H as creditCard,b as cuid,w as cuid2,D as e164,$ as email,z as emoji,k as guid,N as hash,V as hex,S as hostname,C as httpUrl,R as ipv4,j as ipv6,y as isValidCreditCard,W as iso,v as jwt,P as ksuid,I as mac,x as nanoid,l as regexes,L as stringFormat,E as ulid,A as url,F as uuid,g as uuidv4,h as uuidv6,p as uuidv7,O as xid};
@@ -0,0 +1,274 @@
1
+ /**
2
+ * VldStringV2 — OPTIMIZED: check classes return Issue | null (no per-call
3
+ * payload allocation), hot path uses early-return, isSimple is precomputed.
4
+ *
5
+ * Same public API as the legacy VldString.
6
+ */
7
+ import { VldBase, type ErrorParam, type ParseResult } from './base.js';
8
+ import { type VldIssue } from '../errors-core.js';
9
+ export declare abstract class VldStringCheck {
10
+ abstract readonly kind: string;
11
+ abstract check(value: string): VldIssue | null;
12
+ abstract meta(): Record<string, any>;
13
+ }
14
+ export declare class VldCheckMin extends VldStringCheck {
15
+ readonly length: number;
16
+ private _msg?;
17
+ readonly kind = "min";
18
+ constructor(length: number, _msg?: string | undefined);
19
+ get message(): string | undefined;
20
+ check(value: string): VldIssue | null;
21
+ meta(): {
22
+ kind: string;
23
+ value: number;
24
+ message: string | undefined;
25
+ };
26
+ }
27
+ export declare class VldCheckMax extends VldStringCheck {
28
+ readonly length: number;
29
+ private _msg?;
30
+ readonly kind = "max";
31
+ constructor(length: number, _msg?: string | undefined);
32
+ get message(): string | undefined;
33
+ check(value: string): VldIssue | null;
34
+ meta(): {
35
+ kind: string;
36
+ value: number;
37
+ message: string | undefined;
38
+ };
39
+ }
40
+ export declare class VldCheckLength extends VldStringCheck {
41
+ readonly length: number;
42
+ private _msg?;
43
+ readonly kind = "length";
44
+ constructor(length: number, _msg?: string | undefined);
45
+ get message(): string | undefined;
46
+ check(value: string): VldIssue | null;
47
+ meta(): {
48
+ kind: string;
49
+ value: number;
50
+ message: string | undefined;
51
+ };
52
+ }
53
+ export declare class VldCheckEmail extends VldStringCheck {
54
+ private _msg?;
55
+ readonly kind = "format";
56
+ private static RE;
57
+ constructor(_msg?: string | undefined);
58
+ get message(): string | undefined;
59
+ check(value: string): VldIssue | null;
60
+ meta(): {
61
+ kind: string;
62
+ format: string;
63
+ message: string | undefined;
64
+ };
65
+ }
66
+ export declare class VldCheckUrl extends VldStringCheck {
67
+ private _msg?;
68
+ readonly kind = "format";
69
+ private static RE;
70
+ constructor(_msg?: string | undefined);
71
+ get message(): string | undefined;
72
+ check(value: string): VldIssue | null;
73
+ meta(): {
74
+ kind: string;
75
+ format: string;
76
+ message: string | undefined;
77
+ };
78
+ }
79
+ export declare class VldCheckUuid extends VldStringCheck {
80
+ private _msg?;
81
+ readonly kind = "format";
82
+ private static RE;
83
+ constructor(_msg?: string | undefined);
84
+ get message(): string | undefined;
85
+ check(value: string): VldIssue | null;
86
+ meta(): {
87
+ kind: string;
88
+ format: string;
89
+ message: string | undefined;
90
+ };
91
+ }
92
+ export declare class VldCheckRegex extends VldStringCheck {
93
+ readonly pattern: RegExp;
94
+ private _msg?;
95
+ readonly kind = "regex";
96
+ constructor(pattern: RegExp, _msg?: string | undefined);
97
+ get message(): string | undefined;
98
+ check(value: string): VldIssue | null;
99
+ meta(): {
100
+ kind: string;
101
+ pattern: string;
102
+ message: string | undefined;
103
+ };
104
+ }
105
+ export declare class VldCheckStartsWith extends VldStringCheck {
106
+ readonly prefix: string;
107
+ private _msg?;
108
+ readonly kind = "startsWith";
109
+ constructor(prefix: string, _msg?: string | undefined);
110
+ get message(): string | undefined;
111
+ check(value: string): VldIssue | null;
112
+ meta(): {
113
+ kind: string;
114
+ value: string;
115
+ message: string | undefined;
116
+ };
117
+ }
118
+ export declare class VldCheckEndsWith extends VldStringCheck {
119
+ readonly suffix: string;
120
+ private _msg?;
121
+ readonly kind = "endsWith";
122
+ constructor(suffix: string, _msg?: string | undefined);
123
+ get message(): string | undefined;
124
+ check(value: string): VldIssue | null;
125
+ meta(): {
126
+ kind: string;
127
+ value: string;
128
+ message: string | undefined;
129
+ };
130
+ }
131
+ export declare class VldCheckIncludes extends VldStringCheck {
132
+ readonly substring: string;
133
+ private _msg?;
134
+ readonly kind = "includes";
135
+ constructor(substring: string, _msg?: string | undefined);
136
+ get message(): string | undefined;
137
+ check(value: string): VldIssue | null;
138
+ meta(): {
139
+ kind: string;
140
+ value: string;
141
+ message: string | undefined;
142
+ };
143
+ }
144
+ export declare class VldCheckRegexFormat extends VldStringCheck {
145
+ readonly pattern: RegExp;
146
+ readonly formatName: string;
147
+ private _msg?;
148
+ readonly kind = "format";
149
+ constructor(pattern: RegExp, formatName: string, _msg?: string | undefined);
150
+ get message(): string | undefined;
151
+ check(value: string): VldIssue | null;
152
+ meta(): {
153
+ kind: string;
154
+ format: string;
155
+ pattern: string;
156
+ message: string | undefined;
157
+ };
158
+ }
159
+ export declare class VldCheckIp extends VldStringCheck {
160
+ private _msg?;
161
+ readonly kind = "format";
162
+ private static V4;
163
+ constructor(_msg?: string | undefined);
164
+ get message(): string | undefined;
165
+ check(value: string): VldIssue | null;
166
+ meta(): {
167
+ kind: string;
168
+ format: string;
169
+ message: string | undefined;
170
+ };
171
+ }
172
+ export declare class VldCheckIpv4 extends VldStringCheck {
173
+ private _msg?;
174
+ readonly kind = "format";
175
+ private static RE;
176
+ constructor(_msg?: string | undefined);
177
+ get message(): string | undefined;
178
+ check(value: string): VldIssue | null;
179
+ meta(): {
180
+ kind: string;
181
+ format: string;
182
+ message: string | undefined;
183
+ };
184
+ }
185
+ export declare class VldCheckIpv6 extends VldStringCheck {
186
+ private _msg?;
187
+ readonly kind = "format";
188
+ constructor(_msg?: string | undefined);
189
+ get message(): string | undefined;
190
+ check(value: string): VldIssue | null;
191
+ meta(): {
192
+ kind: string;
193
+ format: string;
194
+ message: string | undefined;
195
+ };
196
+ }
197
+ export interface VldStringDef {
198
+ readonly type: 'string';
199
+ readonly checks: ReadonlyArray<VldStringCheck>;
200
+ readonly transforms: ReadonlyArray<(v: string) => string>;
201
+ readonly jsonSchema?: {
202
+ minLength?: number;
203
+ maxLength?: number;
204
+ format?: string;
205
+ pattern?: string;
206
+ } | undefined;
207
+ readonly errorMessage?: string | undefined;
208
+ readonly isSimple: boolean;
209
+ }
210
+ export declare class VldStringV2 extends VldBase<string, string> {
211
+ readonly __def: VldStringDef;
212
+ constructor(def?: VldStringDef);
213
+ static create(): VldStringV2;
214
+ get isSimple(): boolean;
215
+ get minLength(): number | null;
216
+ get maxLength(): number | null;
217
+ get getFormat(): string | null;
218
+ get isCoercion(): boolean;
219
+ parse(value: unknown): string;
220
+ /** Hot path — early-return, no payload allocation. */
221
+ parseKnownString(value: string): string;
222
+ safeParse(value: unknown): ParseResult<string>;
223
+ protected withDef(def: Partial<VldStringDef> & {
224
+ type: 'string';
225
+ }): VldStringV2;
226
+ min(length: number, message?: ErrorParam): VldStringV2;
227
+ max(length: number, message?: ErrorParam): VldStringV2;
228
+ length(length: number, message?: ErrorParam): VldStringV2;
229
+ email(message?: ErrorParam): VldStringV2;
230
+ url(message?: ErrorParam): VldStringV2;
231
+ uuid(message?: ErrorParam): VldStringV2;
232
+ regex(pattern: RegExp, message?: ErrorParam): VldStringV2;
233
+ trim(): VldStringV2;
234
+ toLowerCase(): VldStringV2;
235
+ lowercase(): VldStringV2;
236
+ toUpperCase(): VldStringV2;
237
+ uppercase(): VldStringV2;
238
+ normalize(form?: 'NFC' | 'NFD' | 'NFKC' | 'NFKD'): VldStringV2;
239
+ slugify(): VldStringV2;
240
+ startsWith(str: string, message?: ErrorParam): VldStringV2;
241
+ endsWith(str: string, message?: ErrorParam): VldStringV2;
242
+ includes(str: string, message?: ErrorParam): VldStringV2;
243
+ ip(message?: ErrorParam): VldStringV2;
244
+ ipv4(message?: ErrorParam): VldStringV2;
245
+ ipv6(message?: ErrorParam): VldStringV2;
246
+ nonempty(message?: ErrorParam): VldStringV2;
247
+ private addFormat;
248
+ uuidv4(message?: ErrorParam): VldStringV2;
249
+ uuidv6(message?: ErrorParam): VldStringV2;
250
+ uuidv7(message?: ErrorParam): VldStringV2;
251
+ emoji(message?: ErrorParam): VldStringV2;
252
+ base64(message?: ErrorParam): VldStringV2;
253
+ base64url(message?: ErrorParam): VldStringV2;
254
+ jwt(message?: ErrorParam): VldStringV2;
255
+ nanoid(message?: ErrorParam): VldStringV2;
256
+ cuid(message?: ErrorParam): VldStringV2;
257
+ cuid2(message?: ErrorParam): VldStringV2;
258
+ ulid(message?: ErrorParam): VldStringV2;
259
+ cidrv4(message?: ErrorParam): VldStringV2;
260
+ cidrv6(message?: ErrorParam): VldStringV2;
261
+ e164(message?: ErrorParam): VldStringV2;
262
+ xid(message?: ErrorParam): VldStringV2;
263
+ guid(message?: ErrorParam): VldStringV2;
264
+ ksuid(message?: ErrorParam): VldStringV2;
265
+ date(message?: ErrorParam): VldStringV2;
266
+ time(message?: ErrorParam): VldStringV2;
267
+ datetime(message?: ErrorParam): VldStringV2;
268
+ duration(message?: ErrorParam): VldStringV2;
269
+ }
270
+ export declare class VldCoerceStringV2 extends VldStringV2 {
271
+ constructor(def?: VldStringDef);
272
+ static create(): VldCoerceStringV2;
273
+ parse(value: unknown): string;
274
+ }
@@ -0,0 +1 @@
1
+ import{VLD_VALIDATOR_TYPES as e,VldBase as t,resolveErrorMessage as s}from"./base.js";import{V as r,c as i,g as n}from"../chunks/errors-core-BU14-Dk8.js";import{getMessages as a}from"../locales/runtime.js";import{i as h}from"../chunks/ip-validation-g5LWgy9t.js";class VldStringCheck{}class VldCheckMin extends VldStringCheck{constructor(e,t){super(),this.length=e,this._msg=t,this.kind="min"}get message(){return this._msg}check(e){return e.length<this.length?{code:"too_small",path:[],origin:"string",minimum:this.length,inclusive:!0,message:this._msg||`Too small: expected string to have >=${this.length} characters`}:null}meta(){return{kind:"min",value:this.length,message:this._msg}}}class VldCheckMax extends VldStringCheck{constructor(e,t){super(),this.length=e,this._msg=t,this.kind="max"}get message(){return this._msg}check(e){return e.length>this.length?{code:"too_big",path:[],origin:"string",maximum:this.length,inclusive:!0,message:this._msg||`Too big: expected string to have <=${this.length} characters`}:null}meta(){return{kind:"max",value:this.length,message:this._msg}}}class VldCheckLength extends VldStringCheck{constructor(e,t){super(),this.length=e,this._msg=t,this.kind="length"}get message(){return this._msg}check(e){return e.length!==this.length?{code:"too_big",path:[],origin:"string",exact:this.length,message:this._msg||`Too big: expected string to have exactly ${this.length} characters`}:null}meta(){return{kind:"length",value:this.length,message:this._msg}}}class VldCheckEmail extends VldStringCheck{constructor(e){super(),this._msg=e,this.kind="format"}get message(){return this._msg}check(e){return VldCheckEmail.RE.test(e)?null:{code:"invalid_format",path:[],origin:"string",format:"email",message:this._msg||"Invalid email"}}meta(){return{kind:"format",format:"email",message:this._msg}}}VldCheckEmail.RE=/^[^\s@]+@[^\s@]+\.[^\s@]+$/;class VldCheckUrl extends VldStringCheck{constructor(e){super(),this._msg=e,this.kind="format"}get message(){return this._msg}check(e){return VldCheckUrl.RE.test(e)?null:{code:"invalid_format",path:[],origin:"string",format:"url",message:this._msg||"Invalid url"}}meta(){return{kind:"format",format:"url",message:this._msg}}}VldCheckUrl.RE=/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)$/;class VldCheckUuid extends VldStringCheck{constructor(e){super(),this._msg=e,this.kind="format"}get message(){return this._msg}check(e){return VldCheckUuid.RE.test(e)?null:{code:"invalid_format",path:[],origin:"string",format:"uuid",message:this._msg||"Invalid uuid"}}meta(){return{kind:"format",format:"uuid",message:this._msg}}}VldCheckUuid.RE=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;class VldCheckRegex extends VldStringCheck{constructor(e,t){super(),this.pattern=e,this._msg=t,this.kind="regex"}get message(){return this._msg}check(e){return this.pattern.test(e)?null:{code:"invalid_format",path:[],origin:"string",format:"regex",pattern:this.pattern.source,message:this._msg||"Invalid string: does not match pattern"}}meta(){return{kind:"regex",pattern:this.pattern.source,message:this._msg}}}class VldCheckStartsWith extends VldStringCheck{constructor(e,t){super(),this.prefix=e,this._msg=t,this.kind="startsWith"}get message(){return this._msg}check(e){return e.startsWith(this.prefix)?null:{code:"invalid_string",path:[],message:this._msg||`Invalid string: must start with "${this.prefix}"`}}meta(){return{kind:"startsWith",value:this.prefix,message:this._msg}}}class VldCheckEndsWith extends VldStringCheck{constructor(e,t){super(),this.suffix=e,this._msg=t,this.kind="endsWith"}get message(){return this._msg}check(e){return e.endsWith(this.suffix)?null:{code:"invalid_string",path:[],message:this._msg||`Invalid string: must end with "${this.suffix}"`}}meta(){return{kind:"endsWith",value:this.suffix,message:this._msg}}}class VldCheckIncludes extends VldStringCheck{constructor(e,t){super(),this.substring=e,this._msg=t,this.kind="includes"}get message(){return this._msg}check(e){return e.includes(this.substring)?null:{code:"invalid_string",path:[],message:this._msg||`Invalid string: must include "${this.substring}"`}}meta(){return{kind:"includes",value:this.substring,message:this._msg}}}class VldCheckRegexFormat extends VldStringCheck{constructor(e,t,s){super(),this.pattern=e,this.formatName=t,this._msg=s,this.kind="format"}get message(){return this._msg}check(e){return this.pattern.test(e)?null:{code:"invalid_format",path:[],origin:"string",format:this.formatName,pattern:this.pattern.source,message:this._msg||`Invalid ${this.formatName}`}}meta(){return{kind:"format",format:this.formatName,pattern:this.pattern.source,message:this._msg}}}class VldCheckIp extends VldStringCheck{constructor(e){super(),this._msg=e,this.kind="format"}get message(){return this._msg}check(e){return e.length>100||!VldCheckIp.V4.test(e)&&!h(e)?{code:"invalid_format",path:[],origin:"string",format:"ip",message:this._msg||"Invalid ip"}:null}meta(){return{kind:"format",format:"ip",message:this._msg}}}VldCheckIp.V4=/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;class VldCheckIpv4 extends VldStringCheck{constructor(e){super(),this._msg=e,this.kind="format"}get message(){return this._msg}check(e){return VldCheckIpv4.RE.test(e)?null:{code:"invalid_format",path:[],origin:"string",format:"ipv4",message:this._msg||"Invalid ipv4"}}meta(){return{kind:"format",format:"ipv4",message:this._msg}}}VldCheckIpv4.RE=/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;class VldCheckIpv6 extends VldStringCheck{constructor(e){super(),this._msg=e,this.kind="format"}get message(){return this._msg}check(e){return h(e)?null:{code:"invalid_format",path:[],origin:"string",format:"ipv6",message:this._msg||"Invalid ipv6"}}meta(){return{kind:"format",format:"ipv6",message:this._msg}}}const c=Object.freeze({type:"string",checks:Object.freeze([]),transforms:Object.freeze([]),isSimple:!0});class VldStringV2 extends t{constructor(t=c){super(e.STRING),this.__def=t}static create(){return new VldStringV2(c)}get isSimple(){return this.__def.isSimple}get minLength(){return this.__def.jsonSchema?.minLength??null}get maxLength(){return this.__def.jsonSchema?.maxLength??null}get getFormat(){return this.__def.jsonSchema?.format??null}get isCoercion(){return this.validatorType===e.COERCE_STRING}parse(e){if("string"!=typeof e)throw new r([i("string",n(e),this.__def.errorMessage)]);return this.__def.isSimple?e:this.parseKnownString(e)}parseKnownString(e){const t=this.__def,s=t.transforms;let i=e;switch(s.length){case 0:break;case 1:i=s[0](i);break;case 2:i=s[1](s[0](i));break;case 3:i=s[2](s[1](s[0](i)));break;default:for(let e=0;e<s.length;e++)i=s[e](i)}const n=t.checks;for(let e=0;e<n.length;e++){const t=n[e].check(i);if(null!==t)throw new r([t])}return i}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:e instanceof r?e:new r([{code:"custom",path:[],message:String(e)}])}}}withDef(e){const t=(s=e.checks??this.__def.checks,r=e.transforms??this.__def.transforms,i=e.jsonSchema??this.__def.jsonSchema,n=e.errorMessage??this.__def.errorMessage,Object.freeze({type:"string",checks:s,transforms:r,jsonSchema:i,errorMessage:n,isSimple:0===s.length&&0===r.length}));var s,r,i,n;return new this.constructor(t)}min(e,t){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckMin(e,s(t,a().stringMin(e)))],jsonSchema:{...this.__def.jsonSchema,minLength:e},errorMessage:s(t,a().stringMin(e))})}max(e,t){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckMax(e,s(t,a().stringMax(e)))],jsonSchema:{...this.__def.jsonSchema,maxLength:e},errorMessage:s(t,a().stringMax(e))})}length(e,t){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckLength(e,s(t,a().stringLength(e)))],jsonSchema:{...this.__def.jsonSchema,exactLength:e},errorMessage:s(t,a().stringLength(e))})}email(e){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckEmail(s(e,a().stringEmail))],jsonSchema:{...this.__def.jsonSchema,format:"email"},errorMessage:s(e,a().stringEmail)})}url(e){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckUrl(s(e,a().stringUrl))],jsonSchema:{...this.__def.jsonSchema,format:"uri"},errorMessage:s(e,a().stringUrl)})}uuid(e){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckUuid(s(e,a().stringUuid))],jsonSchema:{...this.__def.jsonSchema,format:"uuid"},errorMessage:s(e,a().stringUuid)})}regex(e,t){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckRegex(e,s(t,a().stringRegex))],jsonSchema:{...this.__def.jsonSchema,pattern:e.source},errorMessage:s(t,a().stringRegex)})}trim(){return this.withDef({type:"string",transforms:[...this.__def.transforms,e=>e.trim()]})}toLowerCase(){return this.withDef({type:"string",transforms:[...this.__def.transforms,e=>e.toLowerCase()]})}lowercase(){return this.toLowerCase()}toUpperCase(){return this.withDef({type:"string",transforms:[...this.__def.transforms,e=>e.toUpperCase()]})}uppercase(){return this.toUpperCase()}normalize(e){return this.withDef({type:"string",transforms:[...this.__def.transforms,t=>t.normalize(e)]})}slugify(){return this.withDef({type:"string",transforms:[...this.__def.transforms,e=>e.toLowerCase().trim().replace(/[^\w\s-]/g,"").replace(/[\s_-]+/g,"-").replace(/^-+|-+$/g,"")]})}startsWith(e,t){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckStartsWith(e,s(t,a().stringStartsWith(e)))],errorMessage:s(t,a().stringStartsWith(e))})}endsWith(e,t){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckEndsWith(e,s(t,a().stringEndsWith(e)))],errorMessage:s(t,a().stringEndsWith(e))})}includes(e,t){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckIncludes(e,s(t,a().stringIncludes(e)))],errorMessage:s(t,a().stringIncludes(e))})}ip(e){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckIp(s(e,a().stringIp))],jsonSchema:{...this.__def.jsonSchema,format:"ip"},errorMessage:s(e,a().stringIp)})}ipv4(e){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckIpv4(s(e,a().stringIpv4))],jsonSchema:{...this.__def.jsonSchema,format:"ipv4"},errorMessage:s(e,a().stringIpv4)})}ipv6(e){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckIpv6(s(e,a().stringIpv6))],jsonSchema:{...this.__def.jsonSchema,format:"ipv6"},errorMessage:s(e,a().stringIpv6)})}nonempty(e){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckMin(1,s(e,a().stringEmpty))],jsonSchema:{...this.__def.jsonSchema,minLength:Math.max(this.__def.jsonSchema?.minLength||0,1)},errorMessage:s(e,a().stringEmpty)})}addFormat(e,t,r){return this.withDef({type:"string",checks:[...this.__def.checks,new VldCheckRegexFormat(e,t,s(r,`Invalid ${t}`))],jsonSchema:{...this.__def.jsonSchema,format:t,pattern:e.source},errorMessage:s(r,`Invalid ${t}`)})}uuidv4(e){return this.addFormat(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,"uuid",e)}uuidv6(e){return this.addFormat(/^[0-9a-f]{8}-[0-9a-f]{4}-6[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,"uuid",e)}uuidv7(e){return this.addFormat(/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,"uuid",e)}emoji(e){return this.addFormat(/^(?:\p{Emoji_Presentation}|\p{Emoji}\uFE0F)(?:\u200D(?:\p{Emoji_Presentation}|\p{Emoji}\uFE0F))*$/u,"emoji",e)}base64(e){return this.addFormat(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/,"base64",e)}base64url(e){return this.addFormat(/^[A-Za-z0-9_-]*$/,"base64url",e)}jwt(e){return this.addFormat(/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/,"jwt",e)}nanoid(e){return this.addFormat(/^[A-Za-z0-9_-]{21}$/,"nanoid",e)}cuid(e){return this.addFormat(/^c[^\s-]{8,}$/i,"cuid",e)}cuid2(e){return this.addFormat(/^[0-9a-z]+$/,"cuid2",e)}ulid(e){return this.addFormat(/^[0-9A-HJKMNP-TV-Z]{26}$/,"ulid",e)}cidrv4(e){return this.addFormat(/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(?:3[0-2]|[12]?[0-9])$/,"cidrv4",e)}cidrv6(e){return this.addFormat(/^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^::(?:[0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^(?:[0-9a-fA-F]{1,4}:){1,7}:\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,"cidrv6",e)}e164(e){return this.addFormat(/^\+[1-9]\d{1,14}$/,"e164",e)}xid(e){return this.addFormat(/^[A-HJKMNP-TV-Z0-9]{20}$/,"xid",e)}guid(e){return this.addFormat(/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/i,"guid",e)}ksuid(e){return this.addFormat(/^[0-9A-Za-z]{27}$/,"ksuid",e)}date(e){return this.addFormat(/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/,"date",e)}time(e){return this.addFormat(/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d+)?)?$/,"time",e)}datetime(e){return this.addFormat(/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?Z?$/,"datetime",e)}duration(e){return this.addFormat(/^-?P(?!$)(?:\d+(?:\.\d+)?Y)?(?:\d+(?:\.\d+)?M)?(?:\d+(?:\.\d+)?W)?(?:\d+(?:\.\d+)?D)?(?:T(?=\d)(?:\d+(?:\.\d+)?H)?(?:\d+(?:\.\d+)?M)?(?:\d+(?:\.\d+)?S)?)?$/,"duration",e)}}class VldCoerceStringV2 extends VldStringV2{constructor(t=c){super(t),this.validatorType=e.COERCE_STRING}static create(){return new VldCoerceStringV2}parse(e){let t;if("string"==typeof e)t=e;else if("number"==typeof e)t=String(e);else if("boolean"==typeof e)t=e?"true":"false";else if("bigint"==typeof e)t=e.toString();else if("symbol"==typeof e)t=String(e);else{if(null==e)throw new Error(a().coercionFailed("string",e));t=e instanceof Date?e.toISOString():Array.isArray(e)?e.join(","):e instanceof RegExp?e.toString():e instanceof Error?e.message||e.toString():String(e)}if(t.length>1e6)throw new Error(a().coercionFailed("string",e));const s=t.replace(/[\x00-\x1F\x7F]/g,"");return super.parse(s)}}export{VldCheckEmail,VldCheckEndsWith,VldCheckIncludes,VldCheckIp,VldCheckIpv4,VldCheckIpv6,VldCheckLength,VldCheckMax,VldCheckMin,VldCheckRegex,VldCheckRegexFormat,VldCheckStartsWith,VldCheckUrl,VldCheckUuid,VldCoerceStringV2,VldStringCheck,VldStringV2};
@@ -1 +1 @@
1
- import{VldBase as s,VLD_VALIDATOR_TYPES as e,resolveErrorMessage as t}from"./base.js";import{getMessages as r}from"../locales/runtime.js";import{i}from"../chunks/ip-validation-g5LWgy9t.js";import{V as n,c as a,g as c}from"../chunks/errors-core-CerMg2rq.js";const o={email:/^[^\s@]+@[^\s@]+\.[^\s@]+$/,uuid:/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,url:/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)$/,ipv4:/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,uuidv4:/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,uuidv6:/^[0-9a-f]{8}-[0-9a-f]{4}-6[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,uuidv7:/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,emoji:/^(?:\p{Emoji_Presentation}|\p{Emoji}\uFE0F)(?:\u200D(?:\p{Emoji_Presentation}|\p{Emoji}\uFE0F))*$/u,base64:/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/,base64url:/^[A-Za-z0-9_-]*$/,jwt:/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/,nanoid:/^[A-Za-z0-9_-]{21}$/,cuid:/^c[^\s-]{8,}$/i,cuid2:/^[0-9a-z]+$/,ulid:/^[0-9A-HJKMNP-TV-Z]{26}$/,cidrv4:/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(?:3[0-2]|[12]?[0-9])$/,cidrv6:/^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^::(?:[0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^(?:[0-9a-fA-F]{1,4}:){1,7}:\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,e164:/^\+[1-9]\d{1,14}$/,xid:/^[A-HJKMNP-TV-Z0-9]{20}$/,guid:/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/i,ksuid:/^[0-9A-Za-z]{27}$/,isoDate:/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/,isoTime:/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d+)?)?$/,isoDateTime:/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?Z?$/,isoDuration:/^-?P(?!$)(?:\d+(?:\.\d+)?Y)?(?:\d+(?:\.\d+)?M)?(?:\d+(?:\.\d+)?W)?(?:\d+(?:\.\d+)?D)?(?:T(?=\d)(?:\d+(?:\.\d+)?H)?(?:\d+(?:\.\d+)?M)?(?:\d+(?:\.\d+)?S)?)?$/};class VldString extends s{constructor(s){super(s?.validatorType||e.STRING),this.config={checks:s?.checks||[],transforms:s?.transforms||[],errorMessage:s?.errorMessage,jsonSchema:s?.jsonSchema,checkMetas:s?.checkMetas},this._checks=this.config.checks,this._transforms=this.config.transforms,this._isSimple=0===this._checks.length&&0===this._transforms.length,this._checkMetas=this.config.checkMetas}static create(){return new VldString}get isSimple(){return this._isSimple}get minLength(){return this.config.jsonSchema?.minLength??null}get maxLength(){return this.config.jsonSchema?.maxLength??null}_createCheckIssue(s){switch(s.kind){case"min":return{code:"too_small",path:[],origin:"string",minimum:s.value,inclusive:!0,message:s.message||`Too small: expected string to have >=${s.value} characters`};case"max":return{code:"too_big",path:[],origin:"string",maximum:s.value,inclusive:!0,message:s.message||`Too big: expected string to have <=${s.value} characters`};case"length":return{code:"too_big",path:[],origin:"string",exact:s.value,message:s.message||`Too big: expected string to have exactly ${s.value} characters`};case"format":{const e={code:"invalid_format",path:[],origin:"string",format:s.format,message:s.message||`Invalid ${s.format}`};return void 0!==s.pattern&&(e.pattern=s.pattern),e}case"regex":{const e={code:"invalid_format",path:[],origin:"string",format:"regex",message:s.message||"Invalid string: does not match pattern"};return void 0!==s.pattern&&(e.pattern=s.pattern),e}default:return{code:"custom",path:[],message:s.message||"Invalid string"}}}_runChecks(s){const e=this._checks,t=this._checkMetas;for(let r=0;r<e.length;r++)if(!e[r](s))return t?.[r]??this._fallbackMeta();return null}_fallbackMeta(){return{kind:"other",message:this.config.errorMessage}}parse(s){if("string"!=typeof s)throw new n([a("string",c(s),this.config.errorMessage)]);return this._isSimple?s:this.parseKnownString(s)}parseKnownString(s){if(this._isSimple)return s;const e=this._transforms;let t=s;switch(e.length){case 0:break;case 1:t=e[0](t);break;case 2:t=e[1](e[0](t));break;case 3:t=e[2](e[1](e[0](t)));break;default:for(let s=0;s<e.length;s++)t=e[s](t)}const r=this._runChecks(t);if(r)throw new n([this._createCheckIssue(r)]);return t}safeParse(s){if("string"!=typeof s)return{success:!1,error:new n([a("string",c(s),this.config.errorMessage)])};if(this._isSimple)return{success:!0,data:s};try{return{success:!0,data:this.parseKnownString(s)}}catch(s){return s instanceof n?{success:!1,error:s}:{success:!1,error:(e=s.message,new n([{code:"invalid_string",path:[],message:e}]))}}var e}min(s,e){return new VldString({checks:[...this.config.checks,e=>e.length>=s],transforms:this.config.transforms,errorMessage:t(e,r().stringMin(s)),jsonSchema:{...this.config.jsonSchema,minLength:s},checkMetas:[...this.config.checkMetas??[],{kind:"min",value:s,message:t(e,r().stringMin(s))}]})}max(s,e){return new VldString({checks:[...this.config.checks,e=>e.length<=s],transforms:this.config.transforms,errorMessage:t(e,r().stringMax(s)),jsonSchema:{...this.config.jsonSchema,maxLength:s},checkMetas:[...this.config.checkMetas??[],{kind:"max",value:s,message:t(e,r().stringMax(s))}]})}length(s,e){return new VldString({checks:[...this.config.checks,e=>e.length===s],transforms:this.config.transforms,errorMessage:t(e,r().stringLength(s)),jsonSchema:{...this.config.jsonSchema,exactLength:s},checkMetas:[...this.config.checkMetas??[],{kind:"length",value:s,message:t(e,r().stringLength(s))}]})}email(s){return new VldString({checks:[...this.config.checks,s=>o.email.test(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringEmail),jsonSchema:{...this.config.jsonSchema,format:"email"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"email",message:t(s,r().stringEmail)}]})}url(s){return new VldString({checks:[...this.config.checks,s=>o.url.test(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringUrl),jsonSchema:{...this.config.jsonSchema,format:"uri"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"url",message:t(s,r().stringUrl)}]})}uuid(s){return new VldString({checks:[...this.config.checks,s=>o.uuid.test(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringUuid),jsonSchema:{...this.config.jsonSchema,format:"uuid"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"uuid",message:t(s,r().stringUuid)}]})}format(s,e,r){return new VldString({checks:[...this.config.checks,e=>s.test(e)],transforms:this.config.transforms,errorMessage:t(r,`Invalid ${e}`),jsonSchema:{...this.config.jsonSchema,format:e,pattern:s.source},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:e,pattern:s.source,message:t(r,`Invalid ${e}`)}]})}uuidv4(s){return this.format(o.uuidv4,"uuid",s)}uuidv6(s){return this.format(o.uuidv6,"uuid",s)}uuidv7(s){return this.format(o.uuidv7,"uuid",s)}emoji(s){return this.format(o.emoji,"emoji",s)}base64(s){return this.format(o.base64,"base64",s)}base64url(s){return this.format(o.base64url,"base64url",s)}jwt(s){return this.format(o.jwt,"jwt",s)}nanoid(s){return this.format(o.nanoid,"nanoid",s)}cuid(s){return this.format(o.cuid,"cuid",s)}cuid2(s){return this.format(o.cuid2,"cuid2",s)}ulid(s){return this.format(o.ulid,"ulid",s)}cidrv4(s){return this.format(o.cidrv4,"cidrv4",s)}cidrv6(s){return this.format(o.cidrv6,"cidrv6",s)}e164(s){return this.format(o.e164,"e164",s)}xid(s){return this.format(o.xid,"xid",s)}guid(s){return this.format(o.guid,"guid",s)}ksuid(s){return this.format(o.ksuid,"ksuid",s)}date(s){return this.format(o.isoDate,"date",s)}time(s){return this.format(o.isoTime,"time",s)}datetime(s){return this.format(o.isoDateTime,"datetime",s)}duration(s){return this.format(o.isoDuration,"duration",s)}regex(s,e){return new VldString({checks:[...this.config.checks,e=>s.test(e)],transforms:this.config.transforms,errorMessage:t(e,r().stringRegex),jsonSchema:{...this.config.jsonSchema,pattern:s.source},checkMetas:[...this.config.checkMetas??[],{kind:"regex",pattern:s.source,message:t(e,r().stringRegex)}]})}trim(){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,s=>s.trim()],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}toLowerCase(){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,s=>s.toLowerCase()],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}lowercase(){return this.toLowerCase()}toUpperCase(){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,s=>s.toUpperCase()],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}uppercase(){return this.toUpperCase()}normalize(s){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,e=>e.normalize(s)],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}slugify(){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,s=>s.toLowerCase().trim().replace(/[^\w\s-]/g,"").replace(/[\s_-]+/g,"-").replace(/^-+|-+$/g,"")],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}startsWith(s,e){return new VldString({checks:[...this.config.checks,e=>e.startsWith(s)],transforms:this.config.transforms,errorMessage:t(e,r().stringStartsWith(s)),jsonSchema:this.config.jsonSchema})}endsWith(s,e){return new VldString({checks:[...this.config.checks,e=>e.endsWith(s)],transforms:this.config.transforms,errorMessage:t(e,r().stringEndsWith(s)),jsonSchema:this.config.jsonSchema})}includes(s,e){return new VldString({checks:[...this.config.checks,e=>e.includes(s)],transforms:this.config.transforms,errorMessage:t(e,r().stringIncludes(s)),jsonSchema:this.config.jsonSchema})}ip(s){return new VldString({checks:[...this.config.checks,s=>!(s.length>100)&&(o.ipv4.test(s)||i(s))],transforms:this.config.transforms,errorMessage:t(s,r().stringIp),jsonSchema:{...this.config.jsonSchema,format:"ip"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"ip",message:t(s,r().stringIp)}]})}ipv4(s){return new VldString({checks:[...this.config.checks,s=>o.ipv4.test(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringIpv4),jsonSchema:{...this.config.jsonSchema,format:"ipv4"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"ipv4",message:t(s,r().stringIpv4)}]})}ipv6(s){return new VldString({checks:[...this.config.checks,s=>i(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringIpv6),jsonSchema:{...this.config.jsonSchema,format:"ipv6"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"ipv6",message:t(s,r().stringIpv6)}]})}nonempty(s){return new VldString({checks:[...this.config.checks,s=>s.length>0],transforms:this.config.transforms,errorMessage:t(s,r().stringEmpty),jsonSchema:{...this.config.jsonSchema,minLength:Math.max(this.config.jsonSchema?.minLength||0,1)},checkMetas:[...this.config.checkMetas??[],{kind:"min",value:1,message:t(s,r().stringEmpty)}]})}}export{VldString};
1
+ import{VldBase as s,VLD_VALIDATOR_TYPES as e,resolveErrorMessage as t}from"./base.js";import{getMessages as r}from"../locales/runtime.js";import{i}from"../chunks/ip-validation-g5LWgy9t.js";import{V as n,c as a,g as c}from"../chunks/errors-core-BU14-Dk8.js";const o={email:/^[^\s@]+@[^\s@]+\.[^\s@]+$/,uuid:/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,url:/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)$/,ipv4:/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,uuidv4:/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,uuidv6:/^[0-9a-f]{8}-[0-9a-f]{4}-6[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,uuidv7:/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,emoji:/^(?:\p{Emoji_Presentation}|\p{Emoji}\uFE0F)(?:\u200D(?:\p{Emoji_Presentation}|\p{Emoji}\uFE0F))*$/u,base64:/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/,base64url:/^[A-Za-z0-9_-]*$/,jwt:/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/,nanoid:/^[A-Za-z0-9_-]{21}$/,cuid:/^c[^\s-]{8,}$/i,cuid2:/^[0-9a-z]+$/,ulid:/^[0-9A-HJKMNP-TV-Z]{26}$/,cidrv4:/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(?:3[0-2]|[12]?[0-9])$/,cidrv6:/^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^::(?:[0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^(?:[0-9a-fA-F]{1,4}:){1,7}:\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,e164:/^\+[1-9]\d{1,14}$/,xid:/^[A-HJKMNP-TV-Z0-9]{20}$/,guid:/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/i,ksuid:/^[0-9A-Za-z]{27}$/,isoDate:/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/,isoTime:/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d+)?)?$/,isoDateTime:/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?Z?$/,isoDuration:/^-?P(?!$)(?:\d+(?:\.\d+)?Y)?(?:\d+(?:\.\d+)?M)?(?:\d+(?:\.\d+)?W)?(?:\d+(?:\.\d+)?D)?(?:T(?=\d)(?:\d+(?:\.\d+)?H)?(?:\d+(?:\.\d+)?M)?(?:\d+(?:\.\d+)?S)?)?$/};class VldString extends s{constructor(s){super(s?.validatorType||e.STRING),this.config={checks:s?.checks||[],transforms:s?.transforms||[],errorMessage:s?.errorMessage,jsonSchema:s?.jsonSchema,checkMetas:s?.checkMetas},this._checks=this.config.checks,this._transforms=this.config.transforms,this._isSimple=0===this._checks.length&&0===this._transforms.length,this._checkMetas=this.config.checkMetas}static create(){return new VldString}get isSimple(){return this._isSimple}get minLength(){return this.config.jsonSchema?.minLength??null}get maxLength(){return this.config.jsonSchema?.maxLength??null}_createCheckIssue(s){switch(s.kind){case"min":return{code:"too_small",path:[],origin:"string",minimum:s.value,inclusive:!0,message:s.message||`Too small: expected string to have >=${s.value} characters`};case"max":return{code:"too_big",path:[],origin:"string",maximum:s.value,inclusive:!0,message:s.message||`Too big: expected string to have <=${s.value} characters`};case"length":return{code:"too_big",path:[],origin:"string",exact:s.value,message:s.message||`Too big: expected string to have exactly ${s.value} characters`};case"format":{const e={code:"invalid_format",path:[],origin:"string",format:s.format,message:s.message||`Invalid ${s.format}`};return void 0!==s.pattern&&(e.pattern=s.pattern),e}case"regex":{const e={code:"invalid_format",path:[],origin:"string",format:"regex",message:s.message||"Invalid string: does not match pattern"};return void 0!==s.pattern&&(e.pattern=s.pattern),e}default:return{code:"custom",path:[],message:s.message||"Invalid string"}}}_runChecks(s){const e=this._checks,t=this._checkMetas;for(let r=0;r<e.length;r++)if(!e[r](s))return t?.[r]??this._fallbackMeta();return null}_fallbackMeta(){return{kind:"other",message:this.config.errorMessage}}parse(s){if("string"!=typeof s)throw new n([a("string",c(s),this.config.errorMessage)]);return this._isSimple?s:this.parseKnownString(s)}parseKnownString(s){if(this._isSimple)return s;const e=this._transforms;let t=s;switch(e.length){case 0:break;case 1:t=e[0](t);break;case 2:t=e[1](e[0](t));break;case 3:t=e[2](e[1](e[0](t)));break;default:for(let s=0;s<e.length;s++)t=e[s](t)}const r=this._runChecks(t);if(r)throw new n([this._createCheckIssue(r)]);return t}safeParse(s){if("string"!=typeof s)return{success:!1,error:new n([a("string",c(s),this.config.errorMessage)])};if(this._isSimple)return{success:!0,data:s};try{return{success:!0,data:this.parseKnownString(s)}}catch(s){return s instanceof n?{success:!1,error:s}:{success:!1,error:(e=s.message,new n([{code:"invalid_string",path:[],message:e}]))}}var e}min(s,e){return new VldString({checks:[...this.config.checks,e=>e.length>=s],transforms:this.config.transforms,errorMessage:t(e,r().stringMin(s)),jsonSchema:{...this.config.jsonSchema,minLength:s},checkMetas:[...this.config.checkMetas??[],{kind:"min",value:s,message:t(e,r().stringMin(s))}]})}max(s,e){return new VldString({checks:[...this.config.checks,e=>e.length<=s],transforms:this.config.transforms,errorMessage:t(e,r().stringMax(s)),jsonSchema:{...this.config.jsonSchema,maxLength:s},checkMetas:[...this.config.checkMetas??[],{kind:"max",value:s,message:t(e,r().stringMax(s))}]})}length(s,e){return new VldString({checks:[...this.config.checks,e=>e.length===s],transforms:this.config.transforms,errorMessage:t(e,r().stringLength(s)),jsonSchema:{...this.config.jsonSchema,exactLength:s},checkMetas:[...this.config.checkMetas??[],{kind:"length",value:s,message:t(e,r().stringLength(s))}]})}email(s){return new VldString({checks:[...this.config.checks,s=>o.email.test(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringEmail),jsonSchema:{...this.config.jsonSchema,format:"email"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"email",message:t(s,r().stringEmail)}]})}url(s){return new VldString({checks:[...this.config.checks,s=>o.url.test(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringUrl),jsonSchema:{...this.config.jsonSchema,format:"uri"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"url",message:t(s,r().stringUrl)}]})}uuid(s){return new VldString({checks:[...this.config.checks,s=>o.uuid.test(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringUuid),jsonSchema:{...this.config.jsonSchema,format:"uuid"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"uuid",message:t(s,r().stringUuid)}]})}format(s,e,r){return new VldString({checks:[...this.config.checks,e=>s.test(e)],transforms:this.config.transforms,errorMessage:t(r,`Invalid ${e}`),jsonSchema:{...this.config.jsonSchema,format:e,pattern:s.source},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:e,pattern:s.source,message:t(r,`Invalid ${e}`)}]})}uuidv4(s){return this.format(o.uuidv4,"uuid",s)}uuidv6(s){return this.format(o.uuidv6,"uuid",s)}uuidv7(s){return this.format(o.uuidv7,"uuid",s)}emoji(s){return this.format(o.emoji,"emoji",s)}base64(s){return this.format(o.base64,"base64",s)}base64url(s){return this.format(o.base64url,"base64url",s)}jwt(s){return this.format(o.jwt,"jwt",s)}nanoid(s){return this.format(o.nanoid,"nanoid",s)}cuid(s){return this.format(o.cuid,"cuid",s)}cuid2(s){return this.format(o.cuid2,"cuid2",s)}ulid(s){return this.format(o.ulid,"ulid",s)}cidrv4(s){return this.format(o.cidrv4,"cidrv4",s)}cidrv6(s){return this.format(o.cidrv6,"cidrv6",s)}e164(s){return this.format(o.e164,"e164",s)}xid(s){return this.format(o.xid,"xid",s)}guid(s){return this.format(o.guid,"guid",s)}ksuid(s){return this.format(o.ksuid,"ksuid",s)}date(s){return this.format(o.isoDate,"date",s)}time(s){return this.format(o.isoTime,"time",s)}datetime(s){return this.format(o.isoDateTime,"datetime",s)}duration(s){return this.format(o.isoDuration,"duration",s)}regex(s,e){return new VldString({checks:[...this.config.checks,e=>s.test(e)],transforms:this.config.transforms,errorMessage:t(e,r().stringRegex),jsonSchema:{...this.config.jsonSchema,pattern:s.source},checkMetas:[...this.config.checkMetas??[],{kind:"regex",pattern:s.source,message:t(e,r().stringRegex)}]})}trim(){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,s=>s.trim()],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}toLowerCase(){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,s=>s.toLowerCase()],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}lowercase(){return this.toLowerCase()}toUpperCase(){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,s=>s.toUpperCase()],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}uppercase(){return this.toUpperCase()}normalize(s){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,e=>e.normalize(s)],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}slugify(){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,s=>s.toLowerCase().trim().replace(/[^\w\s-]/g,"").replace(/[\s_-]+/g,"-").replace(/^-+|-+$/g,"")],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}startsWith(s,e){return new VldString({checks:[...this.config.checks,e=>e.startsWith(s)],transforms:this.config.transforms,errorMessage:t(e,r().stringStartsWith(s)),jsonSchema:this.config.jsonSchema})}endsWith(s,e){return new VldString({checks:[...this.config.checks,e=>e.endsWith(s)],transforms:this.config.transforms,errorMessage:t(e,r().stringEndsWith(s)),jsonSchema:this.config.jsonSchema})}includes(s,e){return new VldString({checks:[...this.config.checks,e=>e.includes(s)],transforms:this.config.transforms,errorMessage:t(e,r().stringIncludes(s)),jsonSchema:this.config.jsonSchema})}ip(s){return new VldString({checks:[...this.config.checks,s=>!(s.length>100)&&(o.ipv4.test(s)||i(s))],transforms:this.config.transforms,errorMessage:t(s,r().stringIp),jsonSchema:{...this.config.jsonSchema,format:"ip"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"ip",message:t(s,r().stringIp)}]})}ipv4(s){return new VldString({checks:[...this.config.checks,s=>o.ipv4.test(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringIpv4),jsonSchema:{...this.config.jsonSchema,format:"ipv4"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"ipv4",message:t(s,r().stringIpv4)}]})}ipv6(s){return new VldString({checks:[...this.config.checks,s=>i(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringIpv6),jsonSchema:{...this.config.jsonSchema,format:"ipv6"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"ipv6",message:t(s,r().stringIpv6)}]})}nonempty(s){return new VldString({checks:[...this.config.checks,s=>s.length>0],transforms:this.config.transforms,errorMessage:t(s,r().stringEmpty),jsonSchema:{...this.config.jsonSchema,minLength:Math.max(this.config.jsonSchema?.minLength||0,1)},checkMetas:[...this.config.checkMetas??[],{kind:"min",value:1,message:t(s,r().stringEmpty)}]})}}export{VldString};
@@ -1 +1 @@
1
- import{VldBase as r,VLD_VALIDATOR_TYPES as e}from"./base.js";import{getMessages as s}from"../locales/runtime.js";import{V as o}from"../chunks/errors-core-CerMg2rq.js";class VldSymbol extends r{constructor(r){super(e.SYMBOL),this.errorMessage=r}static create(){return new VldSymbol}get isSimple(){return!0}parse(r){if("symbol"!=typeof r)throw new Error(this.errorMessage||s().invalidSymbol);return r}parseKnownSymbol(r){return r}safeParse(r){return"symbol"==typeof r?{success:!0,data:r}:{success:!1,error:(e=this.errorMessage||s().invalidSymbol,new o([{code:"invalid_type",path:[],message:e}]))};var e}}export{VldSymbol};
1
+ import{VldBase as r,VLD_VALIDATOR_TYPES as e}from"./base.js";import{getMessages as s}from"../locales/runtime.js";import{V as o}from"../chunks/errors-core-BU14-Dk8.js";class VldSymbol extends r{constructor(r){super(e.SYMBOL),this.errorMessage=r}static create(){return new VldSymbol}get isSimple(){return!0}parse(r){if("symbol"!=typeof r)throw new Error(this.errorMessage||s().invalidSymbol);return r}parseKnownSymbol(r){return r}safeParse(r){return"symbol"==typeof r?{success:!0,data:r}:{success:!1,error:(e=this.errorMessage||s().invalidSymbol,new o([{code:"invalid_type",path:[],message:e}]))};var e}}export{VldSymbol};
@@ -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-CerMg2rq.js";function i(e){return new s([{code:"invalid_array",path:[],message:e}])}class VldTuple extends e{constructor(e,t,s=null){super(r.TUPLE),this.validators=e,this.errorMessage=t,this.restValidator=s,this._length=e.length,this._validatorTypes=e.map(e=>e.validatorType),this._simpleItemModes=e.map((e,r)=>this.getSimpleItemMode(e,this._validatorTypes[r])),this._simpleItemValues=e.map((e,r)=>"literal"===this._simpleItemModes[r]?e.literal:void 0)}get items(){return this.validators}getSimpleItemMode(e,t){if(!0===e.isSimple)switch(t){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().invalidTuple}}static create(...e){return new VldTuple(e)}rest(e){return new VldTuple(this.validators,this.errorMessage,e)}parse(e){if(!Array.isArray(e))throw new Error(this.errorMessage||t().invalidTuple);return this.parseKnownTuple(e)}parseKnownTuple(e){if(null===this.restValidator&&e.length!==this._length||null!==this.restValidator&&e.length<this._length)throw new Error(this.errorMessage||t().tupleLength(this._length,e.length));const r=new Array(e.length);for(let s=0;s<this._length;s++){const i=this._simpleItemModes[s],n=e[s];if(void 0!==i)switch(i){case"string":if("string"!=typeof n)throw new Error(t().arrayItem(s,t().invalidString));r[s]=n;continue;case"number":if("number"!=typeof n||isNaN(n))throw new Error(t().arrayItem(s,t().invalidNumber));r[s]=n;continue;case"boolean":if("boolean"!=typeof n)throw new Error(t().arrayItem(s,t().invalidBoolean));r[s]=n;continue;case"bigint":if("bigint"!=typeof n)throw new Error(t().arrayItem(s,t().invalidBigint));r[s]=n;continue;case"symbol":if("symbol"!=typeof n)throw new Error(t().arrayItem(s,t().invalidSymbol));r[s]=n;continue;case"null":if(null!==n)throw new Error(t().arrayItem(s,this.getSimpleItemError(i,void 0,n)));r[s]=null;continue;case"undefinedValue":if(void 0!==n)throw new Error(t().arrayItem(s,t().expectedUndefined));r[s]=void 0;continue;case"literal":{const e=this._simpleItemValues[s];if(n!==e)throw new Error(t().arrayItem(s,this.getSimpleItemError(i,e,n)));r[s]=e;continue}case"passthrough":r[s]=n;continue}try{const e=this.validators[s];r[s]=e.parse(n)}catch(e){throw new Error(t().arrayItem(s,e.message))}}if(this.restValidator)for(let s=this._length;s<e.length;s++)try{r[s]=this.restValidator.parse(e[s])}catch(e){throw new Error(t().arrayItem(s,e.message))}return r}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:i(e.message)}}}encode(e){if(!Array.isArray(e))throw new Error(this.errorMessage||t().invalidTuple);this.validateEncodedLength(e.length);const r=new Array(e.length);for(let t=0;t<this._length;t++)r[t]=this.validators[t].encode(e[t]);if(this.restValidator)for(let t=this._length;t<e.length;t++)r[t]=this.restValidator.encode(e[t]);return r}safeEncode(e){try{return{success:!0,data:this.encode(e)}}catch(e){return{success:!1,error:i(e.message)}}}async encodeAsync(e){if(!Array.isArray(e))throw new Error(this.errorMessage||t().invalidTuple);this.validateEncodedLength(e.length);const r=new Array(e.length);for(let t=0;t<this._length;t++)r[t]=await this.validators[t].encodeAsync(e[t]);if(this.restValidator)for(let t=this._length;t<e.length;t++)r[t]=await this.restValidator.encodeAsync(e[t]);return r}async safeEncodeAsync(e){try{return{success:!0,data:await this.encodeAsync(e)}}catch(e){return{success:!1,error:i(e.message)}}}validateEncodedLength(e){if(null===this.restValidator&&e!==this._length||null!==this.restValidator&&e<this._length)throw new Error(this.errorMessage||t().tupleLength(this._length,e))}}export{VldTuple};
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 i(e){return new s([{code:"invalid_array",path:[],message:e}])}class VldTuple extends e{constructor(e,t,s=null){super(r.TUPLE),this.validators=e,this.errorMessage=t,this.restValidator=s,this._length=e.length,this._validatorTypes=e.map(e=>e.validatorType),this._simpleItemModes=e.map((e,r)=>this.getSimpleItemMode(e,this._validatorTypes[r])),this._simpleItemValues=e.map((e,r)=>"literal"===this._simpleItemModes[r]?e.literal:void 0)}get items(){return this.validators}getSimpleItemMode(e,t){if(!0===e.isSimple)switch(t){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().invalidTuple}}static create(...e){return new VldTuple(e)}rest(e){return new VldTuple(this.validators,this.errorMessage,e)}parse(e){if(!Array.isArray(e))throw new Error(this.errorMessage||t().invalidTuple);return this.parseKnownTuple(e)}parseKnownTuple(e){if(null===this.restValidator&&e.length!==this._length||null!==this.restValidator&&e.length<this._length)throw new Error(this.errorMessage||t().tupleLength(this._length,e.length));const r=new Array(e.length);for(let s=0;s<this._length;s++){const i=this._simpleItemModes[s],n=e[s];if(void 0!==i)switch(i){case"string":if("string"!=typeof n)throw new Error(t().arrayItem(s,t().invalidString));r[s]=n;continue;case"number":if("number"!=typeof n||isNaN(n))throw new Error(t().arrayItem(s,t().invalidNumber));r[s]=n;continue;case"boolean":if("boolean"!=typeof n)throw new Error(t().arrayItem(s,t().invalidBoolean));r[s]=n;continue;case"bigint":if("bigint"!=typeof n)throw new Error(t().arrayItem(s,t().invalidBigint));r[s]=n;continue;case"symbol":if("symbol"!=typeof n)throw new Error(t().arrayItem(s,t().invalidSymbol));r[s]=n;continue;case"null":if(null!==n)throw new Error(t().arrayItem(s,this.getSimpleItemError(i,void 0,n)));r[s]=null;continue;case"undefinedValue":if(void 0!==n)throw new Error(t().arrayItem(s,t().expectedUndefined));r[s]=void 0;continue;case"literal":{const e=this._simpleItemValues[s];if(n!==e)throw new Error(t().arrayItem(s,this.getSimpleItemError(i,e,n)));r[s]=e;continue}case"passthrough":r[s]=n;continue}try{const e=this.validators[s];r[s]=e.parse(n)}catch(e){throw new Error(t().arrayItem(s,e.message))}}if(this.restValidator)for(let s=this._length;s<e.length;s++)try{r[s]=this.restValidator.parse(e[s])}catch(e){throw new Error(t().arrayItem(s,e.message))}return r}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:i(e.message)}}}encode(e){if(!Array.isArray(e))throw new Error(this.errorMessage||t().invalidTuple);this.validateEncodedLength(e.length);const r=new Array(e.length);for(let t=0;t<this._length;t++)r[t]=this.validators[t].encode(e[t]);if(this.restValidator)for(let t=this._length;t<e.length;t++)r[t]=this.restValidator.encode(e[t]);return r}safeEncode(e){try{return{success:!0,data:this.encode(e)}}catch(e){return{success:!1,error:i(e.message)}}}async encodeAsync(e){if(!Array.isArray(e))throw new Error(this.errorMessage||t().invalidTuple);this.validateEncodedLength(e.length);const r=new Array(e.length);for(let t=0;t<this._length;t++)r[t]=await this.validators[t].encodeAsync(e[t]);if(this.restValidator)for(let t=this._length;t<e.length;t++)r[t]=await this.restValidator.encodeAsync(e[t]);return r}async safeEncodeAsync(e){try{return{success:!0,data:await this.encodeAsync(e)}}catch(e){return{success:!1,error:i(e.message)}}}validateEncodedLength(e){if(null===this.restValidator&&e!==this._length||null!==this.restValidator&&e<this._length)throw new Error(this.errorMessage||t().tupleLength(this._length,e))}}export{VldTuple};
@@ -1 +1 @@
1
- import{VldBase as t,VLD_VALIDATOR_TYPES as r}from"./base.js";import{createIssue as n}from"../errors.js";import{getMessages as e}from"../locales/runtime.js";import{V as i}from"../chunks/errors-core-CerMg2rq.js";class VldUint8Array extends t{constructor(t){super(r.UINT8_ARRAY),this.config=t||{}}parse(t){const r=this.safeParse(t);if(!r.success)throw r.error;return r.data}safeParse(t){return t instanceof Uint8Array?void 0!==this.config.exactLength&&t.length!==this.config.exactLength?{success:!1,error:new i([n("custom",[],e().uint8ArrayExactLength(this.config.exactLength))])}:void 0!==this.config.minLength&&t.length<this.config.minLength?{success:!1,error:new i([n("custom",[],e().uint8ArrayMinLength(this.config.minLength))])}:void 0!==this.config.maxLength&&t.length>this.config.maxLength?{success:!1,error:new i([n("custom",[],e().uint8ArrayMaxLength(this.config.maxLength))])}:{success:!0,data:t}:{success:!1,error:new i([n("invalid_type",[],e().expectedUint8Array)])}}min(t){return new VldUint8Array({...this.config,minLength:t})}max(t){return new VldUint8Array({...this.config,maxLength:t})}length(t){return new VldUint8Array({exactLength:t,minLength:void 0,maxLength:void 0})}static create(){return new VldUint8Array}}export{VldUint8Array};
1
+ import{VldBase as t,VLD_VALIDATOR_TYPES as r}from"./base.js";import{createIssue as n}from"../errors.js";import{getMessages as e}from"../locales/runtime.js";import{V as i}from"../chunks/errors-core-BU14-Dk8.js";class VldUint8Array extends t{constructor(t){super(r.UINT8_ARRAY),this.config=t||{}}parse(t){const r=this.safeParse(t);if(!r.success)throw r.error;return r.data}safeParse(t){return t instanceof Uint8Array?void 0!==this.config.exactLength&&t.length!==this.config.exactLength?{success:!1,error:new i([n("custom",[],e().uint8ArrayExactLength(this.config.exactLength))])}:void 0!==this.config.minLength&&t.length<this.config.minLength?{success:!1,error:new i([n("custom",[],e().uint8ArrayMinLength(this.config.minLength))])}:void 0!==this.config.maxLength&&t.length>this.config.maxLength?{success:!1,error:new i([n("custom",[],e().uint8ArrayMaxLength(this.config.maxLength))])}:{success:!0,data:t}:{success:!1,error:new i([n("invalid_type",[],e().expectedUint8Array)])}}min(t){return new VldUint8Array({...this.config,minLength:t})}max(t){return new VldUint8Array({...this.config,maxLength:t})}length(t){return new VldUint8Array({exactLength:t,minLength:void 0,maxLength:void 0})}static create(){return new VldUint8Array}}export{VldUint8Array};
@@ -1 +1 @@
1
- import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{V as s}from"../chunks/errors-core-CerMg2rq.js";class VldUndefined extends e{constructor(){super(r.UNDEFINED)}static create(){return new VldUndefined}get isSimple(){return!0}parse(e){if(void 0!==e)throw new Error("Expected undefined, received "+typeof e)}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{VldUndefined};
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 VldUndefined extends e{constructor(){super(r.UNDEFINED)}static create(){return new VldUndefined}get isSimple(){return!0}parse(e){if(void 0!==e)throw new Error("Expected undefined, received "+typeof e)}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{VldUndefined};
@@ -0,0 +1,28 @@
1
+ /**
2
+ * VldUnionV2 — single-def union validator.
3
+ * The legacy VldUnion has 5 own properties (validators, errorMessage,
4
+ * typeCheckers, simpleModes, simpleValues). V2 collapses them into a
5
+ * single `__def`, reducing per-instance shape size and letting V8 use
6
+ * a more uniform hidden class.
7
+ *
8
+ * Public API mirrors VldUnion 1:1. Exposed via v.unionV2().
9
+ */
10
+ import { VldBase, type ParseResult } from './base.js';
11
+ type SimpleUnionMode = 'string' | 'number' | 'boolean' | 'bigint' | 'symbol' | 'null' | 'undefined' | 'literal' | 'passthrough' | undefined;
12
+ export interface VldUnionDef {
13
+ readonly type: 'union';
14
+ readonly validators: ReadonlyArray<VldBase<any, any>>;
15
+ readonly typeCheckers: ReadonlyArray<(value: unknown) => boolean>;
16
+ readonly simpleModes: ReadonlyArray<SimpleUnionMode>;
17
+ readonly simpleValues: ReadonlyArray<unknown>;
18
+ readonly errorMessage?: string | undefined;
19
+ }
20
+ export declare class VldUnionV2<T extends readonly VldBase<any, any>[]> extends VldBase<T[number] extends VldBase<any, infer U> ? U : never, T[number] extends VldBase<any, infer U> ? U : never> {
21
+ readonly __def: VldUnionDef;
22
+ constructor(validators: T, errorMessage?: string);
23
+ static create<T extends readonly VldBase<any, any>[]>(...validators: T): VldUnionV2<T>;
24
+ get options(): T;
25
+ parse(value: unknown): T[number] extends VldBase<any, infer U> ? U : never;
26
+ safeParse(value: unknown): ParseResult<T[number] extends VldBase<any, infer U> ? U : never>;
27
+ }
28
+ export {};