602 lines
15 KiB
JavaScript
602 lines
15 KiB
JavaScript
var __defProp = Object.defineProperty;
|
|
var __defNormalProp = (obj, key, value2) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value: value2 }) : obj[key] = value2;
|
|
var __publicField = (obj, key, value2) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value2);
|
|
var store;
|
|
function getGlobalConfig(config2) {
|
|
return {
|
|
lang: config2?.lang ?? store?.lang,
|
|
message: config2?.message,
|
|
abortEarly: config2?.abortEarly ?? store?.abortEarly,
|
|
abortPipeEarly: config2?.abortPipeEarly ?? store?.abortPipeEarly
|
|
};
|
|
}
|
|
var store2;
|
|
function getGlobalMessage(lang) {
|
|
return store2?.get(lang);
|
|
}
|
|
var store3;
|
|
function getSchemaMessage(lang) {
|
|
return store3?.get(lang);
|
|
}
|
|
var store4;
|
|
function getSpecificMessage(reference, lang) {
|
|
return store4?.get(reference)?.get(lang);
|
|
}
|
|
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;
|
|
}
|
|
function _addIssue(context, label, dataset, config2, other) {
|
|
const input = other && "input" in other ? other.input : dataset.value;
|
|
const expected = other?.expected ?? context.expects ?? null;
|
|
const received = other?.received ?? _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: config2.lang,
|
|
abortEarly: config2.abortEarly,
|
|
abortPipeEarly: config2.abortPipeEarly
|
|
};
|
|
const isSchema = context.kind === "schema";
|
|
const message = other?.message ?? context.message ?? getSpecificMessage(context.reference, issue.lang) ?? (isSchema ? getSchemaMessage(issue.lang) : null) ?? config2.message ?? getGlobalMessage(issue.lang);
|
|
if (message) {
|
|
issue.message = typeof message === "function" ? (
|
|
// @ts-expect-error
|
|
message(issue)
|
|
) : message;
|
|
}
|
|
if (isSchema) {
|
|
dataset.typed = false;
|
|
}
|
|
if (dataset.issues) {
|
|
dataset.issues.push(issue);
|
|
} else {
|
|
dataset.issues = [issue];
|
|
}
|
|
}
|
|
function _joinExpects(values, separator) {
|
|
const list = [...new Set(values)];
|
|
if (list.length > 1) {
|
|
return `(${list.join(` ${separator} `)})`;
|
|
}
|
|
return list[0] ?? "never";
|
|
}
|
|
var ValiError = class extends Error {
|
|
/**
|
|
* Creates a Valibot error with useful information.
|
|
*
|
|
* @param issues The error issues.
|
|
*/
|
|
constructor(issues) {
|
|
super(issues[0].message);
|
|
/**
|
|
* The error issues.
|
|
*/
|
|
__publicField(this, "issues");
|
|
this.name = "ValiError";
|
|
this.issues = issues;
|
|
}
|
|
};
|
|
function integer(message) {
|
|
return {
|
|
kind: "validation",
|
|
type: "integer",
|
|
reference: integer,
|
|
async: false,
|
|
expects: null,
|
|
requirement: Number.isInteger,
|
|
message,
|
|
"~validate"(dataset, config2) {
|
|
if (dataset.typed && !this.requirement(dataset.value)) {
|
|
_addIssue(this, "integer", dataset, config2);
|
|
}
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
function maxValue(requirement, message) {
|
|
return {
|
|
kind: "validation",
|
|
type: "max_value",
|
|
reference: maxValue,
|
|
async: false,
|
|
expects: `<=${requirement instanceof Date ? requirement.toJSON() : _stringify(requirement)}`,
|
|
requirement,
|
|
message,
|
|
"~validate"(dataset, config2) {
|
|
if (dataset.typed && !(dataset.value <= this.requirement)) {
|
|
_addIssue(this, "value", dataset, config2, {
|
|
received: dataset.value instanceof Date ? dataset.value.toJSON() : _stringify(dataset.value)
|
|
});
|
|
}
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
function minValue(requirement, message) {
|
|
return {
|
|
kind: "validation",
|
|
type: "min_value",
|
|
reference: minValue,
|
|
async: false,
|
|
expects: `>=${requirement instanceof Date ? requirement.toJSON() : _stringify(requirement)}`,
|
|
requirement,
|
|
message,
|
|
"~validate"(dataset, config2) {
|
|
if (dataset.typed && !(dataset.value >= this.requirement)) {
|
|
_addIssue(this, "value", dataset, config2, {
|
|
received: dataset.value instanceof Date ? dataset.value.toJSON() : _stringify(dataset.value)
|
|
});
|
|
}
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
function url(message) {
|
|
return {
|
|
kind: "validation",
|
|
type: "url",
|
|
reference: url,
|
|
async: false,
|
|
expects: null,
|
|
requirement(input) {
|
|
try {
|
|
new URL(input);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
},
|
|
message,
|
|
"~validate"(dataset, config2) {
|
|
if (dataset.typed && !this.requirement(dataset.value)) {
|
|
_addIssue(this, "URL", dataset, config2);
|
|
}
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
function value(requirement, message) {
|
|
return {
|
|
kind: "validation",
|
|
type: "value",
|
|
reference: value,
|
|
async: false,
|
|
expects: requirement instanceof Date ? requirement.toJSON() : _stringify(requirement),
|
|
requirement,
|
|
message,
|
|
"~validate"(dataset, config2) {
|
|
if (dataset.typed && !(this.requirement <= dataset.value && this.requirement >= dataset.value)) {
|
|
_addIssue(this, "value", dataset, config2, {
|
|
received: dataset.value instanceof Date ? dataset.value.toJSON() : _stringify(dataset.value)
|
|
});
|
|
}
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
function getDefault(schema, dataset, config2) {
|
|
return typeof schema.default === "function" ? (
|
|
// @ts-expect-error
|
|
schema.default(dataset, config2)
|
|
) : (
|
|
// @ts-expect-error
|
|
schema.default
|
|
);
|
|
}
|
|
function is(schema, input) {
|
|
return !schema["~validate"]({ value: input }, { abortEarly: true }).issues;
|
|
}
|
|
function array(item, message) {
|
|
return {
|
|
kind: "schema",
|
|
type: "array",
|
|
reference: array,
|
|
expects: "Array",
|
|
async: false,
|
|
item,
|
|
message,
|
|
"~standard": 1,
|
|
"~vendor": "valibot",
|
|
"~validate"(dataset, config2 = getGlobalConfig()) {
|
|
const input = dataset.value;
|
|
if (Array.isArray(input)) {
|
|
dataset.typed = true;
|
|
dataset.value = [];
|
|
for (let key = 0; key < input.length; key++) {
|
|
const value2 = input[key];
|
|
const itemDataset = this.item["~validate"]({ value: value2 }, config2);
|
|
if (itemDataset.issues) {
|
|
const pathItem = {
|
|
type: "array",
|
|
origin: "value",
|
|
input,
|
|
key,
|
|
value: value2
|
|
};
|
|
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 (config2.abortEarly) {
|
|
dataset.typed = false;
|
|
break;
|
|
}
|
|
}
|
|
if (!itemDataset.typed) {
|
|
dataset.typed = false;
|
|
}
|
|
dataset.value.push(itemDataset.value);
|
|
}
|
|
} else {
|
|
_addIssue(this, "type", dataset, config2);
|
|
}
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
function boolean(message) {
|
|
return {
|
|
kind: "schema",
|
|
type: "boolean",
|
|
reference: boolean,
|
|
expects: "boolean",
|
|
async: false,
|
|
message,
|
|
"~standard": 1,
|
|
"~vendor": "valibot",
|
|
"~validate"(dataset, config2 = getGlobalConfig()) {
|
|
if (typeof dataset.value === "boolean") {
|
|
dataset.typed = true;
|
|
} else {
|
|
_addIssue(this, "type", dataset, config2);
|
|
}
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
function enum_(enum__, message) {
|
|
const options = Object.entries(enum__).filter(([key]) => isNaN(+key)).map(([, value2]) => value2);
|
|
return {
|
|
kind: "schema",
|
|
type: "enum",
|
|
reference: enum_,
|
|
expects: _joinExpects(options.map(_stringify), "|"),
|
|
async: false,
|
|
enum: enum__,
|
|
options,
|
|
message,
|
|
"~standard": 1,
|
|
"~vendor": "valibot",
|
|
"~validate"(dataset, config2 = getGlobalConfig()) {
|
|
if (this.options.includes(dataset.value)) {
|
|
dataset.typed = true;
|
|
} else {
|
|
_addIssue(this, "type", dataset, config2);
|
|
}
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
function null_(message) {
|
|
return {
|
|
kind: "schema",
|
|
type: "null",
|
|
reference: null_,
|
|
expects: "null",
|
|
async: false,
|
|
message,
|
|
"~standard": 1,
|
|
"~vendor": "valibot",
|
|
"~validate"(dataset, config2 = getGlobalConfig()) {
|
|
if (dataset.value === null) {
|
|
dataset.typed = true;
|
|
} else {
|
|
_addIssue(this, "type", dataset, config2);
|
|
}
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
function number(message) {
|
|
return {
|
|
kind: "schema",
|
|
type: "number",
|
|
reference: number,
|
|
expects: "number",
|
|
async: false,
|
|
message,
|
|
"~standard": 1,
|
|
"~vendor": "valibot",
|
|
"~validate"(dataset, config2 = getGlobalConfig()) {
|
|
if (typeof dataset.value === "number" && !isNaN(dataset.value)) {
|
|
dataset.typed = true;
|
|
} else {
|
|
_addIssue(this, "type", dataset, config2);
|
|
}
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
function object(entries, message) {
|
|
return {
|
|
kind: "schema",
|
|
type: "object",
|
|
reference: object,
|
|
expects: "Object",
|
|
async: false,
|
|
entries,
|
|
message,
|
|
"~standard": 1,
|
|
"~vendor": "valibot",
|
|
"~validate"(dataset, config2 = getGlobalConfig()) {
|
|
const input = dataset.value;
|
|
if (input && typeof input === "object") {
|
|
dataset.typed = true;
|
|
dataset.value = {};
|
|
for (const key in this.entries) {
|
|
const value2 = input[key];
|
|
const valueDataset = this.entries[key]["~validate"](
|
|
{ value: value2 },
|
|
config2
|
|
);
|
|
if (valueDataset.issues) {
|
|
const pathItem = {
|
|
type: "object",
|
|
origin: "value",
|
|
input,
|
|
key,
|
|
value: value2
|
|
};
|
|
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 (config2.abortEarly) {
|
|
dataset.typed = false;
|
|
break;
|
|
}
|
|
}
|
|
if (!valueDataset.typed) {
|
|
dataset.typed = false;
|
|
}
|
|
if (valueDataset.value !== void 0 || key in input) {
|
|
dataset.value[key] = valueDataset.value;
|
|
}
|
|
}
|
|
} else {
|
|
_addIssue(this, "type", dataset, config2);
|
|
}
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
function optional(wrapped, default_) {
|
|
return {
|
|
kind: "schema",
|
|
type: "optional",
|
|
reference: optional,
|
|
expects: `(${wrapped.expects} | undefined)`,
|
|
async: false,
|
|
wrapped,
|
|
default: default_,
|
|
"~standard": 1,
|
|
"~vendor": "valibot",
|
|
"~validate"(dataset, config2 = getGlobalConfig()) {
|
|
if (dataset.value === void 0) {
|
|
if (this.default !== void 0) {
|
|
dataset.value = getDefault(this, dataset, config2);
|
|
}
|
|
if (dataset.value === void 0) {
|
|
dataset.typed = true;
|
|
return dataset;
|
|
}
|
|
}
|
|
return this.wrapped["~validate"](dataset, config2);
|
|
}
|
|
};
|
|
}
|
|
function string(message) {
|
|
return {
|
|
kind: "schema",
|
|
type: "string",
|
|
reference: string,
|
|
expects: "string",
|
|
async: false,
|
|
message,
|
|
"~standard": 1,
|
|
"~vendor": "valibot",
|
|
"~validate"(dataset, config2 = getGlobalConfig()) {
|
|
if (typeof dataset.value === "string") {
|
|
dataset.typed = true;
|
|
} else {
|
|
_addIssue(this, "type", dataset, config2);
|
|
}
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
function _subIssues(datasets) {
|
|
let issues;
|
|
if (datasets) {
|
|
for (const dataset of datasets) {
|
|
if (issues) {
|
|
issues.push(...dataset.issues);
|
|
} else {
|
|
issues = dataset.issues;
|
|
}
|
|
}
|
|
}
|
|
return issues;
|
|
}
|
|
function union(options, message) {
|
|
return {
|
|
kind: "schema",
|
|
type: "union",
|
|
reference: union,
|
|
expects: _joinExpects(
|
|
options.map((option) => option.expects),
|
|
"|"
|
|
),
|
|
async: false,
|
|
options,
|
|
message,
|
|
"~standard": 1,
|
|
"~vendor": "valibot",
|
|
"~validate"(dataset, config2 = getGlobalConfig()) {
|
|
let validDataset;
|
|
let typedDatasets;
|
|
let untypedDatasets;
|
|
for (const schema of this.options) {
|
|
const optionDataset = schema["~validate"](
|
|
{ value: dataset.value },
|
|
config2
|
|
);
|
|
if (optionDataset.typed) {
|
|
if (optionDataset.issues) {
|
|
if (typedDatasets) {
|
|
typedDatasets.push(optionDataset);
|
|
} else {
|
|
typedDatasets = [optionDataset];
|
|
}
|
|
} else {
|
|
validDataset = optionDataset;
|
|
break;
|
|
}
|
|
} else {
|
|
if (untypedDatasets) {
|
|
untypedDatasets.push(optionDataset);
|
|
} else {
|
|
untypedDatasets = [optionDataset];
|
|
}
|
|
}
|
|
}
|
|
if (validDataset) {
|
|
return validDataset;
|
|
}
|
|
if (typedDatasets) {
|
|
if (typedDatasets.length === 1) {
|
|
return typedDatasets[0];
|
|
}
|
|
_addIssue(this, "type", dataset, config2, {
|
|
issues: _subIssues(typedDatasets)
|
|
});
|
|
dataset.typed = true;
|
|
} else if (untypedDatasets?.length === 1) {
|
|
return untypedDatasets[0];
|
|
} else {
|
|
_addIssue(this, "type", dataset, config2, {
|
|
issues: _subIssues(untypedDatasets)
|
|
});
|
|
}
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
function unknown() {
|
|
return {
|
|
kind: "schema",
|
|
type: "unknown",
|
|
reference: unknown,
|
|
expects: "unknown",
|
|
async: false,
|
|
"~standard": 1,
|
|
"~vendor": "valibot",
|
|
"~validate"(dataset) {
|
|
dataset.typed = true;
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
function omit(schema, keys) {
|
|
const entries = {
|
|
...schema.entries
|
|
};
|
|
for (const key of keys) {
|
|
delete entries[key];
|
|
}
|
|
return { ...schema, entries };
|
|
}
|
|
function parse(schema, input, config2) {
|
|
const dataset = schema["~validate"](
|
|
{ value: input },
|
|
getGlobalConfig(config2)
|
|
);
|
|
if (dataset.issues) {
|
|
throw new ValiError(dataset.issues);
|
|
}
|
|
return dataset.value;
|
|
}
|
|
function pipe(...pipe2) {
|
|
return {
|
|
...pipe2[0],
|
|
pipe: pipe2,
|
|
"~validate"(dataset, config2 = getGlobalConfig()) {
|
|
for (const item of pipe2) {
|
|
if (item.kind !== "metadata") {
|
|
if (dataset.issues && (item.kind === "schema" || item.kind === "transformation")) {
|
|
dataset.typed = false;
|
|
break;
|
|
}
|
|
if (!dataset.issues || !config2.abortEarly && !config2.abortPipeEarly) {
|
|
dataset = item["~validate"](dataset, config2);
|
|
}
|
|
}
|
|
}
|
|
return dataset;
|
|
}
|
|
};
|
|
}
|
|
export {
|
|
ValiError as V,
|
|
pipe as a,
|
|
array as b,
|
|
optional as c,
|
|
boolean as d,
|
|
enum_ as e,
|
|
union as f,
|
|
null_ as g,
|
|
url as h,
|
|
integer as i,
|
|
omit as j,
|
|
maxValue as k,
|
|
is as l,
|
|
minValue as m,
|
|
number as n,
|
|
object as o,
|
|
parse as p,
|
|
string as s,
|
|
unknown as u,
|
|
value as v
|
|
};
|
|
//# sourceMappingURL=index.BulDzU6h.js.map
|