const DEFAULT_CONFIG = {
lang: void 0,
message: void 0,
abortEarly: void 0,
abortPipeEarly: void 0,
};
/**
* Returns the global configuration.
*
* @param config The config to merge.
*
* @returns The configuration.
*/
/* @__NO_SIDE_EFFECTS__ */
function getGlobalConfig(config$1) {
if (!config$1 && true) return DEFAULT_CONFIG;
return {
lang: config$1?.lang ?? void 0,
message: config$1?.message,
abortEarly: config$1?.abortEarly ?? void 0,
abortPipeEarly: config$1?.abortPipeEarly ?? void 0,
};
}
/**
* Stringifies an unknown input to a literal or type string.
*
* @param input The unknown input.
*
* @returns A literal or type string.
*
* @internal
*/
/* @__NO_SIDE_EFFECTS__ */
function _stringify(input) {
const type = typeof input;
if (type === "string") return `"${input}"`;
if (type === "number" || type === "bigint" || type === "boolean") return `${input}`;
if (type === "object" || type === "function")
return (input && Object.getPrototypeOf(input)?.constructor?.name) ?? "null";
return type;
}
/**
* Adds an issue to the dataset.
*
* @param context The issue context.
* @param label The issue label.
* @param dataset The input dataset.
* @param config The configuration.
* @param other The optional props.
*
* @internal
*/
function _addIssue(context, label, dataset, config$1, other) {
const input = other && "input" in other ? other.input : dataset.value;
const expected = other?.expected ?? context.expects ?? null;
const received = other?.received ?? /* @__PURE__ */ _stringify(input);
const issue = {
kind: context.kind,
type: context.type,
input,
expected,
received,
message: `Invalid ${label}: ${expected ? `Expected ${expected} but r` : "R"}eceived ${received}`,
requirement: context.requirement,
path: other?.path,
issues: other?.issues,
lang: config$1.lang,
abortEarly: config$1.abortEarly,
abortPipeEarly: config$1.abortPipeEarly,
};
const isSchema = context.kind === "schema";
const message$1 =
other?.message ??
context.message ??
(context.reference, issue.lang, void 0) ??
(isSchema ? (issue.lang, void 0) : null) ??
config$1.message ??
(issue.lang, void 0);
if (message$1 !== void 0)
issue.message = typeof message$1 === "function" ? message$1(issue) : message$1;
if (isSchema) dataset.typed = false;
if (dataset.issues) dataset.issues.push(issue);
else dataset.issues = [issue];
}
const _standardCache = /* @__PURE__ */ new WeakMap();
/**
* Returns the Standard Schema properties.
*
* @param context The schema context.
*
* @returns The Standard Schema properties.
*/
/* @__NO_SIDE_EFFECTS__ */
function _getStandardProps(context) {
let cached = _standardCache.get(context);
if (!cached) {
cached = {
version: 1,
vendor: "valibot",
validate(value$1) {
return context["~run"]({ value: value$1 }, /* @__PURE__ */ getGlobalConfig());
},
};
_standardCache.set(context, cached);
}
return cached;
}
/**
* Joins multiple `expects` values with the given separator.
*
* @param values The `expects` values.
* @param separator The separator.
*
* @returns The joined `expects` property.
*
* @internal
*/
/* @__NO_SIDE_EFFECTS__ */
function _joinExpects(values$1, separator) {
const list = [...new Set(values$1)];
if (list.length > 1) return `(${list.join(` ${separator} `)})`;
return list[0] ?? "never";
}
/**
* A Valibot error with useful information.
*/
var ValiError = class extends Error {
/**
* Creates a Valibot error with useful information.
*
* @param issues The error issues.
*/
constructor(issues) {
super(issues[0].message);
this.name = "ValiError";
this.issues = issues;
}
};
/* @__NO_SIDE_EFFECTS__ */
function maxLength(requirement, message$1) {
return {
kind: "validation",
type: "max_length",
reference: maxLength,
async: false,
expects: `<=${requirement}`,
requirement,
message: message$1,
"~run"(dataset, config$1) {
if (dataset.typed && dataset.value.length > this.requirement)
_addIssue(this, "length", dataset, config$1, { received: `${dataset.value.length}` });
return dataset;
},
};
}
/* @__NO_SIDE_EFFECTS__ */
function maxValue(requirement, message$1) {
return {
kind: "validation",
type: "max_value",
reference: maxValue,
async: false,
expects: `<=${requirement instanceof Date ? requirement.toJSON() : /* @__PURE__ */ _stringify(requirement)}`,
requirement,
message: message$1,
"~run"(dataset, config$1) {
if (dataset.typed && !(dataset.value <= this.requirement))
_addIssue(this, "value", dataset, config$1, {
received:
dataset.value instanceof Date
? dataset.value.toJSON()
: /* @__PURE__ */ _stringify(dataset.value),
});
return dataset;
},
};
}
/* @__NO_SIDE_EFFECTS__ */
function minLength(requirement, message$1) {
return {
kind: "validation",
type: "min_length",
reference: minLength,
async: false,
expects: `>=${requirement}`,
requirement,
message: message$1,
"~run"(dataset, config$1) {
if (dataset.typed && dataset.value.length < this.requirement)
_addIssue(this, "length", dataset, config$1, { received: `${dataset.value.length}` });
return dataset;
},
};
}
/* @__NO_SIDE_EFFECTS__ */
function minValue(requirement, message$1) {
return {
kind: "validation",
type: "min_value",
reference: minValue,
async: false,
expects: `>=${requirement instanceof Date ? requirement.toJSON() : /* @__PURE__ */ _stringify(requirement)}`,
requirement,
message: message$1,
"~run"(dataset, config$1) {
if (dataset.typed && !(dataset.value >= this.requirement))
_addIssue(this, "value", dataset, config$1, {
received:
dataset.value instanceof Date
? dataset.value.toJSON()
: /* @__PURE__ */ _stringify(dataset.value),
});
return dataset;
},
};
}
/* @__NO_SIDE_EFFECTS__ */
function url(message$1) {
return {
kind: "validation",
type: "url",
reference: url,
async: false,
expects: null,
requirement(input) {
try {
new URL(input);
return true;
} catch {
return false;
}
},
message: message$1,
"~run"(dataset, config$1) {
if (dataset.typed && !this.requirement(dataset.value))
_addIssue(this, "URL", dataset, config$1);
return dataset;
},
};
}
/**
* Returns the fallback value of the schema.
*
* @param schema The schema to get it from.
* @param dataset The output dataset if available.
* @param config The config if available.
*
* @returns The fallback value.
*/
/* @__NO_SIDE_EFFECTS__ */
function getFallback(schema, dataset, config$1) {
return typeof schema.fallback === "function"
? schema.fallback(dataset, config$1)
: schema.fallback;
}
/**
* Returns the default value of the schema.
*
* @param schema The schema to get it from.
* @param dataset The input dataset if available.
* @param config The config if available.
*
* @returns The default value.
*/
/* @__NO_SIDE_EFFECTS__ */
function getDefault(schema, dataset, config$1) {
return typeof schema.default === "function" ? schema.default(dataset, config$1) : schema.default;
}
/* @__NO_SIDE_EFFECTS__ */
function array(item, message$1) {
return {
kind: "schema",
type: "array",
reference: array,
expects: "Array",
async: false,
item,
message: message$1,
get "~standard"() {
return /* @__PURE__ */ _getStandardProps(this);
},
"~run"(dataset, config$1) {
const input = dataset.value;
if (Array.isArray(input)) {
dataset.typed = true;
dataset.value = [];
for (let key = 0; key < input.length; key++) {
const value$1 = input[key];
const itemDataset = this.item["~run"]({ value: value$1 }, config$1);
if (itemDataset.issues) {
const pathItem = {
type: "array",
origin: "value",
input,
key,
value: value$1,
};
for (const issue of itemDataset.issues) {
if (issue.path) issue.path.unshift(pathItem);
else issue.path = [pathItem];
dataset.issues?.push(issue);
}
if (!dataset.issues) dataset.issues = itemDataset.issues;
if (config$1.abortEarly) {
dataset.typed = false;
break;
}
}
if (!itemDataset.typed) dataset.typed = false;
dataset.value.push(itemDataset.value);
}
} else _addIssue(this, "type", dataset, config$1);
return dataset;
},
};
}
/* @__NO_SIDE_EFFECTS__ */
function date(message$1) {
return {
kind: "schema",
type: "date",
reference: date,
expects: "Date",
async: false,
message: message$1,
get "~standard"() {
return /* @__PURE__ */ _getStandardProps(this);
},
"~run"(dataset, config$1) {
if (dataset.value instanceof Date)
if (!isNaN(dataset.value)) dataset.typed = true;
else _addIssue(this, "type", dataset, config$1, { received: '"Invalid Date"' });
else _addIssue(this, "type", dataset, config$1);
return dataset;
},
};
}
/* @__NO_SIDE_EFFECTS__ */
function nullable(wrapped, default_) {
return {
kind: "schema",
type: "nullable",
reference: nullable,
expects: `(${wrapped.expects} | null)`,
async: false,
wrapped,
default: default_,
get "~standard"() {
return /* @__PURE__ */ _getStandardProps(this);
},
"~run"(dataset, config$1) {
if (dataset.value === null) {
if (this.default !== void 0)
dataset.value = /* @__PURE__ */ getDefault(this, dataset, config$1);
if (dataset.value === null) {
dataset.typed = true;
return dataset;
}
}
return this.wrapped["~run"](dataset, config$1);
},
};
}
/* @__NO_SIDE_EFFECTS__ */
function number(message$1) {
return {
kind: "schema",
type: "number",
reference: number,
expects: "number",
async: false,
message: message$1,
get "~standard"() {
return /* @__PURE__ */ _getStandardProps(this);
},
"~run"(dataset, config$1) {
if (typeof dataset.value === "number" && !isNaN(dataset.value)) dataset.typed = true;
else _addIssue(this, "type", dataset, config$1);
return dataset;
},
};
}
/* @__NO_SIDE_EFFECTS__ */
function object(entries$1, message$1) {
return {
kind: "schema",
type: "object",
reference: object,
expects: "Object",
async: false,
entries: entries$1,
message: message$1,
get "~standard"() {
return /* @__PURE__ */ _getStandardProps(this);
},
"~run"(dataset, config$1) {
const input = dataset.value;
if (input && typeof input === "object") {
dataset.typed = true;
dataset.value = {};
for (const key in this.entries) {
const valueSchema = this.entries[key];
if (
key in input ||
((valueSchema.type === "exact_optional" ||
valueSchema.type === "optional" ||
valueSchema.type === "nullish") &&
valueSchema.default !== void 0)
) {
const value$1 = key in input ? input[key] : /* @__PURE__ */ getDefault(valueSchema);
const valueDataset = valueSchema["~run"]({ value: value$1 }, config$1);
if (valueDataset.issues) {
const pathItem = {
type: "object",
origin: "value",
input,
key,
value: value$1,
};
for (const issue of valueDataset.issues) {
if (issue.path) issue.path.unshift(pathItem);
else issue.path = [pathItem];
dataset.issues?.push(issue);
}
if (!dataset.issues) dataset.issues = valueDataset.issues;
if (config$1.abortEarly) {
dataset.typed = false;
break;
}
}
if (!valueDataset.typed) dataset.typed = false;
dataset.value[key] = valueDataset.value;
} else if (valueSchema.fallback !== void 0)
dataset.value[key] = /* @__PURE__ */ getFallback(valueSchema);
else if (
valueSchema.type !== "exact_optional" &&
valueSchema.type !== "optional" &&
valueSchema.type !== "nullish"
) {
_addIssue(this, "key", dataset, config$1, {
input: void 0,
expected: `"${key}"`,
path: [
{
type: "object",
origin: "key",
input,
key,
value: input[key],
},
],
});
if (config$1.abortEarly) break;
}
}
} else _addIssue(this, "type", dataset, config$1);
return dataset;
},
};
}
/* @__NO_SIDE_EFFECTS__ */
function picklist(options, message$1) {
return {
kind: "schema",
type: "picklist",
reference: picklist,
expects: /* @__PURE__ */ _joinExpects(options.map(_stringify), "|"),
async: false,
options,
message: message$1,
get "~standard"() {
return /* @__PURE__ */ _getStandardProps(this);
},
"~run"(dataset, config$1) {
if (this.options.includes(dataset.value)) dataset.typed = true;
else _addIssue(this, "type", dataset, config$1);
return dataset;
},
};
}
/* @__NO_SIDE_EFFECTS__ */
function string(message$1) {
return {
kind: "schema",
type: "string",
reference: string,
expects: "string",
async: false,
message: message$1,
get "~standard"() {
return /* @__PURE__ */ _getStandardProps(this);
},
"~run"(dataset, config$1) {
if (typeof dataset.value === "string") dataset.typed = true;
else _addIssue(this, "type", dataset, config$1);
return dataset;
},
};
}
/**
* Parses an unknown input based on a schema.
*
* @param schema The schema to be used.
* @param input The input to be parsed.
* @param config The parse configuration.
*
* @returns The parsed input.
*/
function parse(schema, input, config$1) {
const dataset = schema["~run"]({ value: input }, /* @__PURE__ */ getGlobalConfig(config$1));
if (dataset.issues) throw new ValiError(dataset.issues);
return dataset.value;
}
/* @__NO_SIDE_EFFECTS__ */
function pipe(...pipe$1) {
return {
...pipe$1[0],
pipe: pipe$1,
get "~standard"() {
return /* @__PURE__ */ _getStandardProps(this);
},
"~run"(dataset, config$1) {
for (const item of pipe$1)
if (item.kind !== "metadata") {
if (dataset.issues && (item.kind === "schema" || item.kind === "transformation")) {
dataset.typed = false;
break;
}
if (!dataset.issues || (!config$1.abortEarly && !config$1.abortPipeEarly))
dataset = item["~run"](dataset, config$1);
}
return dataset;
},
};
}
//#endregion
//#region ../schemas/libraries/valibot/download.ts
const imageSchema = /* @__PURE__ */ object({
id: /* @__PURE__ */ number(),
created: /* @__PURE__ */ date(),
title: /* @__PURE__ */ pipe(
/* @__PURE__ */ string(),
/* @__PURE__ */ minLength(1),
/* @__PURE__ */ maxLength(100),
),
type: /* @__PURE__ */ picklist(["jpg", "png"]),
size: /* @__PURE__ */ number(),
url: /* @__PURE__ */ pipe(/* @__PURE__ */ string(), /* @__PURE__ */ url()),
});
parse(
/* @__PURE__ */ object({
id: /* @__PURE__ */ number(),
created: /* @__PURE__ */ date(),
title: /* @__PURE__ */ pipe(
/* @__PURE__ */ string(),
/* @__PURE__ */ minLength(1),
/* @__PURE__ */ maxLength(100),
),
brand: /* @__PURE__ */ pipe(
/* @__PURE__ */ string(),
/* @__PURE__ */ minLength(1),
/* @__PURE__ */ maxLength(30),
),
description: /* @__PURE__ */ pipe(
/* @__PURE__ */ string(),
/* @__PURE__ */ minLength(1),
/* @__PURE__ */ maxLength(500),
),
price: /* @__PURE__ */ pipe(
/* @__PURE__ */ number(),
/* @__PURE__ */ minValue(1),
/* @__PURE__ */ maxValue(1e4),
),
discount: /* @__PURE__ */ nullable(
/* @__PURE__ */ pipe(
/* @__PURE__ */ number(),
/* @__PURE__ */ minValue(1),
/* @__PURE__ */ maxValue(100),
),
),
quantity: /* @__PURE__ */ pipe(
/* @__PURE__ */ number(),
/* @__PURE__ */ minValue(1),
/* @__PURE__ */ maxValue(10),
),
tags: /* @__PURE__ */ array(
/* @__PURE__ */ pipe(
/* @__PURE__ */ string(),
/* @__PURE__ */ minLength(1),
/* @__PURE__ */ maxLength(30),
),
),
images: /* @__PURE__ */ array(imageSchema),
ratings: /* @__PURE__ */ array(
/* @__PURE__ */ object({
id: /* @__PURE__ */ number(),
stars: /* @__PURE__ */ pipe(
/* @__PURE__ */ number(),
/* @__PURE__ */ minValue(1),
/* @__PURE__ */ maxValue(5),
),
title: /* @__PURE__ */ pipe(
/* @__PURE__ */ string(),
/* @__PURE__ */ minLength(1),
/* @__PURE__ */ maxLength(100),
),
text: /* @__PURE__ */ pipe(
/* @__PURE__ */ string(),
/* @__PURE__ */ minLength(1),
/* @__PURE__ */ maxLength(1e3),
),
images: /* @__PURE__ */ array(imageSchema),
}),
),
}),
{},
);
//#endregion