@nhtio/validation 0.1.0-master-c7157b24 → 0.1.0-master-effacff6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.mjs CHANGED
@@ -93982,6 +93982,87 @@ Settings.defaultWeekSettings = {
93982
93982
  weekend: [6, 7]
93983
93983
  };
93984
93984
  Settings.throwOnInvalid = false;
93985
+ const removeComments = (str) => {
93986
+ str = str.replace(/\/\/.*$/gm, "");
93987
+ str = str.replace(/\/\*[\s\S]*?\*\//g, "");
93988
+ return str;
93989
+ };
93990
+ const extractFunctionBody = (fn$1) => {
93991
+ const fnString = fn$1.toString();
93992
+ if (fnString.includes("[native code]")) throw new Error("Cannot extract body from native function");
93993
+ const cleanedString = removeComments(fnString);
93994
+ const arrowMatch = cleanedString.match(/^\s*(?:async\s+)?(?:\([^)]*\)|[^=]+)\s*=>\s*(.+)$/s);
93995
+ if (arrowMatch && !arrowMatch[1].trim().startsWith("{")) {
93996
+ const expression = arrowMatch[1].trim().replace(/;?\s*$/, "");
93997
+ if (expression.startsWith("(") && expression.endsWith(")")) {
93998
+ const unwrapped = expression.slice(1, -1).trim();
93999
+ if (unwrapped.startsWith("{") && unwrapped.endsWith("}")) return `return ${unwrapped}`;
94000
+ }
94001
+ return `return ${expression}`;
94002
+ }
94003
+ let braceCount = 0;
94004
+ let inString = false;
94005
+ let stringChar = "";
94006
+ let inTemplate = false;
94007
+ let escaped = false;
94008
+ let firstBrace = -1;
94009
+ let lastBrace = -1;
94010
+ for (const [i$3, char] of Array.from(cleanedString).entries()) {
94011
+ if (escaped) {
94012
+ escaped = false;
94013
+ continue;
94014
+ }
94015
+ if (char === "\\") {
94016
+ escaped = true;
94017
+ continue;
94018
+ }
94019
+ if (!inString && !inTemplate) {
94020
+ if (char === "\"" || char === "'") {
94021
+ inString = true;
94022
+ stringChar = char;
94023
+ continue;
94024
+ }
94025
+ if (char === "`") {
94026
+ inTemplate = true;
94027
+ continue;
94028
+ }
94029
+ } else if (inString && char === stringChar) {
94030
+ inString = false;
94031
+ continue;
94032
+ } else if (inTemplate && char === "`") {
94033
+ inTemplate = false;
94034
+ continue;
94035
+ }
94036
+ if (!inString && !inTemplate) {
94037
+ if (char === "{") {
94038
+ if (firstBrace === -1) firstBrace = i$3;
94039
+ braceCount++;
94040
+ } else if (char === "}") {
94041
+ braceCount--;
94042
+ if (braceCount === 0 && firstBrace !== -1) {
94043
+ lastBrace = i$3;
94044
+ break;
94045
+ }
94046
+ }
94047
+ }
94048
+ }
94049
+ if (firstBrace === -1 || lastBrace === -1 || firstBrace >= lastBrace) throw new Error("Unable to extract function body - no valid braces found");
94050
+ const body = cleanedString.slice(firstBrace + 1, lastBrace).trim();
94051
+ if (/\breturn\b/.test(body)) return body;
94052
+ const beforeBrace = cleanedString.slice(0, firstBrace).trim();
94053
+ if (beforeBrace.endsWith("=>") || beforeBrace.endsWith("=> (") || beforeBrace.endsWith("=>(")) {
94054
+ const objectLiteralPattern = /^\s*(?:[a-zA-Z_$][\w$]*\s*:|["'`][^"'`]+["'`]\s*:|get\s+|set\s+|\[)/;
94055
+ if (objectLiteralPattern.test(body)) return `return {${body}}`;
94056
+ }
94057
+ return `return ${body}`;
94058
+ };
94059
+ const compileCallback = (body) => {
94060
+ try {
94061
+ return new Function("dt", "helpers", body);
94062
+ } catch (error) {
94063
+ throw new Error(`Failed to compile callback: ${error instanceof Error ? error.message : String(error)}`);
94064
+ }
94065
+ };
93985
94066
  const messages$2 = {
93986
94067
  "datetime.base": "{{#label}} must be a datetime value",
93987
94068
  "datetime.exactly": "{{#label}} must be a datetime exactly equal to {{#limit}}",
@@ -94050,10 +94131,27 @@ const coerce$2 = (value, helpers) => {
94050
94131
  const { schema, prefs } = helpers;
94051
94132
  if (prefs.convert) {
94052
94133
  let returnable = converted;
94053
- if (schema._flags.setZone && Array.isArray(schema._flags.setZone)) {
94134
+ if (schema._flags.setZone && Array.isArray(schema._flags.setZone)) if (Array.isArray(schema._flags.setZone[0])) Array.from(schema._flags.setZone).forEach((f) => {
94135
+ if (!f) return;
94136
+ const [zone, opts] = Array.from(f);
94137
+ switch (zone) {
94138
+ case "utc":
94139
+ case "UTC":
94140
+ returnable = returnable.toUTC();
94141
+ break;
94142
+ case "local":
94143
+ returnable = returnable.toLocal();
94144
+ break;
94145
+ default:
94146
+ returnable = returnable.setZone(zone, opts);
94147
+ break;
94148
+ }
94149
+ });
94150
+ else {
94054
94151
  const [zone, opts] = Array.from(schema._flags.setZone);
94055
94152
  switch (zone) {
94056
94153
  case "utc":
94154
+ case "UTC":
94057
94155
  returnable = returnable.toUTC();
94058
94156
  break;
94059
94157
  case "local":
@@ -94069,8 +94167,19 @@ const coerce$2 = (value, helpers) => {
94069
94167
  if (Array.isArray(schema._flags.toFormat)) {
94070
94168
  const fmt = Array.from(schema._flags.toFormat);
94071
94169
  const method = fmt.shift();
94072
- returnable = returnable[method](...fmt);
94073
- } else if (typeof schema._flags.toFormat === "string") returnable = returnable.toFormat(schema._flags.toFormat);
94170
+ const processedArgs = fmt.map((arg) => {
94171
+ if (typeof arg === "string" && arg.includes("return")) {
94172
+ const compiledFn = compileCallback(arg);
94173
+ return compiledFn(returnable, helpers);
94174
+ }
94175
+ return arg;
94176
+ });
94177
+ returnable = returnable[method](...processedArgs);
94178
+ } else if (typeof schema._flags.toFormat === "string") if (schema._flags.toFormat.includes("return")) {
94179
+ const compiledFn = compileCallback(schema._flags.toFormat);
94180
+ const result = compiledFn(returnable, helpers);
94181
+ returnable = returnable.toFormat(result);
94182
+ } else returnable = returnable.toFormat(schema._flags.toFormat);
94074
94183
  }
94075
94184
  return { value: returnable };
94076
94185
  }
@@ -94226,22 +94335,27 @@ const datetime = function(joi) {
94226
94335
  return this.$_setFlag("setZone", [zone, opts]);
94227
94336
  } },
94228
94337
  toFormat: { method(format) {
94229
- return this.$_setFlag("toFormat", format, { clone: true });
94338
+ const processedFormat = typeof format === "function" ? extractFunctionBody(format) : format;
94339
+ return this.$_setFlag("toFormat", processedFormat, { clone: true });
94230
94340
  } },
94231
94341
  toLocalizedString: { method(formatOpts) {
94232
- return this.$_setFlag("toFormat", ["toLocaleString", formatOpts], { clone: true });
94342
+ const processedOpts = typeof formatOpts === "function" ? extractFunctionBody(formatOpts) : formatOpts;
94343
+ return this.$_setFlag("toFormat", ["toLocaleString", processedOpts], { clone: true });
94233
94344
  } },
94234
94345
  toISO: { method(opts) {
94235
- return this.$_setFlag("toFormat", opts ? ["toISO", opts] : ["toISO"], { clone: true });
94346
+ const processedOpts = opts && typeof opts === "function" ? extractFunctionBody(opts) : opts;
94347
+ return this.$_setFlag("toFormat", processedOpts ? ["toISO", processedOpts] : ["toISO"], { clone: true });
94236
94348
  } },
94237
94349
  toISODate: { method(opts) {
94238
- return this.$_setFlag("toFormat", opts ? ["toISODate", opts] : ["toISODate"], { clone: true });
94350
+ const processedOpts = opts && typeof opts === "function" ? extractFunctionBody(opts) : opts;
94351
+ return this.$_setFlag("toFormat", processedOpts ? ["toISODate", processedOpts] : ["toISODate"], { clone: true });
94239
94352
  } },
94240
94353
  toISOWeekDate: { method() {
94241
94354
  return this.$_setFlag("toFormat", ["toISOWeekDate"], { clone: true });
94242
94355
  } },
94243
94356
  toISOTime: { method(opts) {
94244
- return this.$_setFlag("toFormat", opts ? ["toISOTime", opts] : ["toISOTime"], { clone: true });
94357
+ const processedOpts = opts && typeof opts === "function" ? extractFunctionBody(opts) : opts;
94358
+ return this.$_setFlag("toFormat", processedOpts ? ["toISOTime", processedOpts] : ["toISOTime"], { clone: true });
94245
94359
  } },
94246
94360
  toRFC2822: { method() {
94247
94361
  return this.$_setFlag("toFormat", ["toRFC2822"], { clone: true });
@@ -94253,10 +94367,12 @@ const datetime = function(joi) {
94253
94367
  return this.$_setFlag("toFormat", ["toSQLDate"], { clone: true });
94254
94368
  } },
94255
94369
  toSQLTime: { method(opts) {
94256
- return this.$_setFlag("toFormat", opts ? ["toSQLTime", opts] : ["toSQLTime"], { clone: true });
94370
+ const processedOpts = opts && typeof opts === "function" ? extractFunctionBody(opts) : opts;
94371
+ return this.$_setFlag("toFormat", processedOpts ? ["toSQLTime", processedOpts] : ["toSQLTime"], { clone: true });
94257
94372
  } },
94258
94373
  toSQL: { method(opts) {
94259
- return this.$_setFlag("toFormat", opts ? ["toSQL", opts] : ["toSQL"], { clone: true });
94374
+ const processedOpts = opts && typeof opts === "function" ? extractFunctionBody(opts) : opts;
94375
+ return this.$_setFlag("toFormat", processedOpts ? ["toSQL", processedOpts] : ["toSQL"], { clone: true });
94260
94376
  } },
94261
94377
  toMillis: { method() {
94262
94378
  return this.$_setFlag("toFormat", ["toMillis"], { clone: true });
@@ -94274,14 +94390,15 @@ const datetime = function(joi) {
94274
94390
  return this.$_setFlag("toFormat", ["toBSON"], { clone: true });
94275
94391
  } },
94276
94392
  toObject: { method(opts) {
94277
- const config = opts?.includeConfig !== void 0 ? { includeConfig: opts.includeConfig } : { includeConfig: false };
94393
+ const config = typeof opts === "function" ? extractFunctionBody(opts) : opts?.includeConfig !== void 0 ? { includeConfig: opts.includeConfig } : { includeConfig: false };
94278
94394
  return this.$_setFlag("toFormat", ["toObject", config], { clone: true });
94279
94395
  } },
94280
94396
  toJSDate: { method() {
94281
94397
  return this.$_setFlag("toFormat", ["toJSDate"], { clone: true });
94282
94398
  } },
94283
94399
  toRelative: { method(opts) {
94284
- return this.$_setFlag("toFormat", opts ? ["toRelative", opts] : ["toRelative"], { clone: true });
94400
+ const processedOpts = opts && typeof opts === "function" ? extractFunctionBody(opts) : opts;
94401
+ return this.$_setFlag("toFormat", processedOpts ? ["toRelative", processedOpts] : ["toRelative"], { clone: true });
94285
94402
  } },
94286
94403
  setLocale: { method(locale) {
94287
94404
  return this.$_setFlag("setLocale", locale);
@@ -109524,7 +109641,7 @@ const encode = (schema) => {
109524
109641
  });
109525
109642
  }
109526
109643
  const json$1 = JSON.stringify({
109527
- version: "0.1.0-master-c7157b24",
109644
+ version: "0.1.0-master-effacff6",
109528
109645
  schema: description$1
109529
109646
  }, (_, value) => {
109530
109647
  if (typeof value === "bigint") return value.toString();
@@ -109540,9 +109657,9 @@ const decode = (base64) => {
109540
109657
  const description$1 = JSON.parse(json$1);
109541
109658
  if (!isPlainObject(description$1) || !("version" in description$1) || !("schema" in description$1) || typeof description$1.version !== "string" || !isPlainObject(description$1.schema)) throw new TypeError("Not a valid encoded schema");
109542
109659
  const { version: schemaVersion, schema } = description$1;
109543
- if (import_semver.valid("0.1.0-master-c7157b24")) {
109660
+ if (import_semver.valid("0.1.0-master-effacff6")) {
109544
109661
  if (!import_semver.valid(import_semver.coerce(schemaVersion))) throw new TypeError(`Invalid schema version: ${schemaVersion}`);
109545
- if (import_semver.gt(import_semver.coerce(schemaVersion), "0.1.0-master-c7157b24")) throw new TypeError(`Schema version ${schemaVersion} is not compatible with current version 0.1.0-master-c7157b24`);
109662
+ if (import_semver.gt(import_semver.coerce(schemaVersion), "0.1.0-master-effacff6")) throw new TypeError(`Schema version ${schemaVersion} is not compatible with current version 0.1.0-master-effacff6`);
109546
109663
  }
109547
109664
  return validator.build(schema);
109548
109665
  };
@@ -112808,7 +112925,7 @@ const tlds = new Set(TLDS.map((tld) => tld.toLowerCase()));
112808
112925
  var import_lib = __toESM(require_lib$1());
112809
112926
  var import_lib$1 = __toESM(require_lib());
112810
112927
  var import_lib$2 = __toESM(require_lib$2());
112811
- const version = "0.1.0-master-c7157b24";
112928
+ const version = "0.1.0-master-effacff6";
112812
112929
  var ValidationError = import_lib$2.ValidationError;
112813
112930
  var location = import_lib$1.location;
112814
112931
  export { ValidationError, esm_exports as address, decode, encode, import_lib as formula, location, tlds, validator, version };