schemas/libraries/@remix-run/data-schema/download_compiled/index/unminified.js
schemas/libraries/@remix-run/data-schema/download_compiled/index/unminified.js
schemas/libraries/@remix-run/data-schema/download_compiled/index/unminified.js
schemas/libraries/@remix-run/data-schema/download_compiled/index/unminified.js
const builtinSchemas =
new WeakSet();
function createSchema(validator) {
let schema = {
"~standard": {
version: 1,
vendor: "data-schema",
validate(value, options) {
return validator(value, {
path: [],
options,
mutablePath:
isBuiltinSchema(schema),
});
},
},
"~run"(value, context) {
if (context.mutablePath)
return validator(value, {
path: [...context.path],
options: context.options,
});
return validator(value, context);
},
pipe(...checks) {
if (checks.length === 0)
return schema;
return createDerivedSchema(
schema,
function validate(
value,
context,
) {
let result = schema["~run"](
value,
context,
);
if (result.issues)
return result;
for (let check of checks)
if (
!check.check(result.value)
) {
if (!check.code)
return {
issues: [
createIssue(
check.message ??
"Check failed",
[...context.path],
),
],
};
return {
issues: [
createIssueFromContext(
context,
{
code: check.code,
defaultMessage:
check.message ??
"Check failed",
input:
result.value,
values:
check.values,
},
),
],
};
}
return result;
},
);
},
refine(predicate, message) {
return createDerivedSchema(
schema,
function validate(
value,
context,
) {
let result = schema["~run"](
value,
context,
);
if (result.issues)
return result;
if (
!predicate(result.value)
) {
if (message !== void 0)
return {
issues: [
createIssue(message, [
...context.path,
]),
],
};
return {
issues: [
createIssueFromContext(
context,
{
code: "refine.failed",
defaultMessage:
"Refinement failed",
input: result.value,
},
),
],
};
}
return result;
},
);
},
transform(transformer) {
return createDerivedSchema(
schema,
function validate(
value,
context,
) {
let result = schema["~run"](
value,
context,
);
if (result.issues)
return result;
return {
value: transformer(
result.value,
),
};
},
);
},
};
return schema;
}
function isBuiltinSchema(schema) {
return builtinSchemas.has(schema);
}
function createBuiltinSchema(
validator,
) {
let schema = createSchema(validator);
schema["~run"] = validator;
builtinSchemas.add(schema);
return schema;
}
function createDerivedSchema(
source,
validator,
) {
return isBuiltinSchema(source)
? createBuiltinSchema(validator)
: createSchema(validator);
}
function runAtPath(
schema,
value,
context,
key,
) {
if (
context.mutablePath !== true ||
!Array.isArray(context.path)
) {
let childContext = {
path: withPath(context.path, key),
options: context.options,
mutablePath: false,
};
return schema["~run"](
value,
childContext,
);
}
context.path.push(key);
try {
return schema["~run"](
value,
context,
);
} finally {
context.path.pop();
}
}
function shouldAbortEarly(options) {
let libraryAbortEarly =
options?.libraryOptions?.abortEarly;
let abortEarly =
options?.abortEarly ??
libraryAbortEarly;
return Boolean(abortEarly);
}
function withPath(path, key) {
return path.length === 0
? [key]
: [...path, key];
}
function getErrorMap(options) {
let libraryErrorMap =
options?.libraryOptions?.errorMap;
if (
typeof options?.errorMap ===
"function"
)
return options.errorMap;
if (
typeof libraryErrorMap ===
"function"
)
return libraryErrorMap;
}
function getLocale(options) {
let libraryLocale =
options?.libraryOptions?.locale;
if (
typeof options?.locale === "string"
)
return options.locale;
if (typeof libraryLocale === "string")
return libraryLocale;
}
function resolveIssueMessage(
options,
context,
) {
let errorMap = getErrorMap(options);
if (!errorMap)
return context.defaultMessage;
return (
errorMap(context) ??
context.defaultMessage
);
}
function createIssueFromContext(
context,
descriptor,
) {
let path = [
...(descriptor.path ??
context.path),
];
return createIssue(
resolveIssueMessage(
context.options,
{
code: descriptor.code,
defaultMessage:
descriptor.defaultMessage,
path,
values: descriptor.values,
input: descriptor.input,
locale: getLocale(
context.options,
),
},
),
path,
);
}
function createIssue(message, path) {
return !path || path.length === 0
? { message }
: {
message,
path,
};
}
function fail(message, path, options) {
if (!options?.code)
return {
issues: [
createIssue(message, path),
],
};
path = path && [...path];
return {
issues: [
createIssue(
resolveIssueMessage(
options.parseOptions,
{
code: options.code,
defaultMessage: message,
path,
values: options.values,
input: options.input,
locale: getLocale(
options.parseOptions,
),
},
),
path,
),
],
};
}
function array(elementSchema) {
return createBuiltinSchema(
function validate(value, context) {
if (!Array.isArray(value))
return fail(
"Expected array",
context.path,
{
code: "type.array",
input: value,
parseOptions:
context.options,
},
);
let abortEarly = shouldAbortEarly(
context.options,
);
let issues = [];
let outputValues = new Array(
value.length,
);
let index = 0;
for (let item of value) {
let result = runAtPath(
elementSchema,
item,
context,
index,
);
if (result.issues) {
if (abortEarly) return result;
issues.push(...result.issues);
} else
outputValues[index] =
result.value;
index += 1;
}
if (issues.length > 0)
return { issues };
outputValues.length = index;
return { value: outputValues };
},
);
}
function enum_(values) {
return createBuiltinSchema(
function validate(value, context) {
for (let allowed of values)
if (value === allowed)
return { value };
return fail(
"Expected one of: " +
values.map(String).join(", "),
context.path,
{
code: "enum.invalid_value",
input: value,
values: {
values: [...values],
},
parseOptions: context.options,
},
);
},
);
}
function instanceof_(constructor) {
return createBuiltinSchema(
function validate(value, context) {
if (
!(value instanceof constructor)
)
return fail(
"Expected instance of " +
constructor.name,
context.path,
{
code: "instanceof.invalid_type",
input: value,
values: {
constructorName:
constructor.name,
},
parseOptions:
context.options,
},
);
return { value };
},
);
}
function nullable(schema) {
return createBuiltinSchema(
function validate(value, context) {
if (value === null)
return { value: null };
return schema["~run"](
value,
context,
);
},
);
}
function number() {
return createBuiltinSchema(
function validate(value, context) {
if (
typeof value !== "number" ||
!Number.isFinite(value)
)
return fail(
"Expected number",
context.path,
{
code: "type.number",
input: value,
parseOptions:
context.options,
},
);
return { value };
},
);
}
function object(shape, options) {
return createBuiltinSchema(
function validate(value, context) {
if (
typeof value !== "object" ||
value === null ||
Array.isArray(value)
)
return fail(
"Expected object",
context.path,
{
code: "type.object",
input: value,
parseOptions:
context.options,
},
);
let abortEarly = shouldAbortEarly(
context.options,
);
let issues = [];
let outputValues = {};
let input = value;
let unknownKeys =
options?.unknownKeys ?? "strip";
for (let key of Object.keys(
shape,
)) {
let result = runAtPath(
shape[key],
input[key],
context,
key,
);
if (result.issues) {
if (abortEarly) return result;
issues.push(...result.issues);
} else if (
Object.prototype.hasOwnProperty.call(
input,
key,
) ||
result.value !== void 0
)
outputValues[key] =
result.value;
}
if (
unknownKeys === "passthrough" ||
unknownKeys === "error"
)
for (let key in input) {
if (
!Object.prototype.hasOwnProperty.call(
input,
key,
)
)
continue;
if (
Object.prototype.hasOwnProperty.call(
shape,
key,
)
)
continue;
if (
unknownKeys ===
"passthrough"
)
outputValues[key] =
input[key];
else {
let issue =
createIssueFromContext(
context,
{
code: "object.unknown_key",
defaultMessage:
"Unknown key",
input: input[key],
path: withPath(
context.path,
key,
),
values: { key },
},
);
if (abortEarly)
return {
issues: [issue],
};
issues.push(issue);
}
}
if (issues.length > 0)
return { issues };
return { value: outputValues };
},
);
}
function string() {
return createBuiltinSchema(
function validate(value, context) {
if (typeof value !== "string")
return fail(
"Expected string",
context.path,
{
code: "type.string",
input: value,
parseOptions:
context.options,
},
);
return { value };
},
);
}
var ValidationError = class extends Error {
issues;
constructor(
issues,
message = "Validation failed",
) {
super(message);
this.name = "ValidationError";
this.issues = issues;
}
};
function parse(schema, value, options) {
let result = schema[
"~standard"
].validate(value, options);
if (result.issues)
throw new ValidationError(
result.issues,
);
return result.value;
}
function minLength(length) {
return {
check(value) {
return value.length >= length;
},
code: "string.min_length",
values: { min: length },
message:
"Expected at least " +
String(length) +
" characters",
};
}
function maxLength(length) {
return {
check(value) {
return value.length <= length;
},
code: "string.max_length",
values: { max: length },
message:
"Expected at most " +
String(length) +
" characters",
};
}
function url() {
return {
check(value) {
try {
new URL(value);
return true;
} catch {
return false;
}
},
code: "string.url",
message: "Expected valid URL",
};
}
function min(limit) {
return {
check(value) {
return value >= limit;
},
code: "number.min",
values: { min: limit },
message:
"Expected number greater than or equal to " +
String(limit),
};
}
function max(limit) {
return {
check(value) {
return value <= limit;
},
code: "number.max",
values: { max: limit },
message:
"Expected number less than or equal to " +
String(limit),
};
}
const imageSchema = object({
id: number(),
created: instanceof_(Date),
title: string().pipe(
minLength(1),
maxLength(100),
),
type: enum_(["jpg", "png"]),
size: number(),
url: string().pipe(url()),
});
const ratingSchema = object({
id: number(),
stars: number().pipe(min(1), max(5)),
title: string().pipe(
minLength(1),
maxLength(100),
),
text: string().pipe(
minLength(1),
maxLength(1e3),
),
images: array(imageSchema),
});
parse(
object({
id: number(),
created: instanceof_(Date),
title: string().pipe(
minLength(1),
maxLength(100),
),
brand: string().pipe(
minLength(1),
maxLength(30),
),
description: string().pipe(
minLength(1),
maxLength(500),
),
price: number().pipe(
min(1),
max(1e4),
),
discount: nullable(
number().pipe(min(1), max(100)),
),
quantity: number().pipe(
min(0),
max(10),
),
tags: array(
string().pipe(
minLength(1),
maxLength(30),
),
),
images: array(imageSchema),
ratings: array(ratingSchema),
}),
{},
);
const builtinSchemas = new WeakSet();
function createSchema(validator) {
let schema = {
"~standard": {
version: 1,
vendor: "data-schema",
validate(value, options) {
return validator(value, {
path: [],
options,
mutablePath: isBuiltinSchema(schema),
});
},
},
"~run"(value, context) {
if (context.mutablePath)
return validator(value, {
path: [...context.path],
options: context.options,
});
return validator(value, context);
},
pipe(...checks) {
if (checks.length === 0) return schema;
return createDerivedSchema(
schema,
function validate(value, context) {
let result = schema["~run"](value, context);
if (result.issues) return result;
for (let check of checks)
if (!check.check(result.value)) {
if (!check.code)
return {
issues: [
createIssue(
check.message ?? "Check failed",
[...context.path],
),
],
};
return {
issues: [
createIssueFromContext(context, {
code: check.code,
defaultMessage:
check.message ?? "Check failed",
input: result.value,
values: check.values,
}),
],
};
}
return result;
},
);
},
refine(predicate, message) {
return createDerivedSchema(
schema,
function validate(value, context) {
let result = schema["~run"](value, context);
if (result.issues) return result;
if (!predicate(result.value)) {
if (message !== void 0)
return {
issues: [
createIssue(message, [...context.path]),
],
};
return {
issues: [
createIssueFromContext(context, {
code: "refine.failed",
defaultMessage: "Refinement failed",
input: result.value,
}),
],
};
}
return result;
},
);
},
transform(transformer) {
return createDerivedSchema(
schema,
function validate(value, context) {
let result = schema["~run"](value, context);
if (result.issues) return result;
return { value: transformer(result.value) };
},
);
},
};
return schema;
}
function isBuiltinSchema(schema) {
return builtinSchemas.has(schema);
}
function createBuiltinSchema(validator) {
let schema = createSchema(validator);
schema["~run"] = validator;
builtinSchemas.add(schema);
return schema;
}
function createDerivedSchema(source, validator) {
return isBuiltinSchema(source)
? createBuiltinSchema(validator)
: createSchema(validator);
}
function runAtPath(schema, value, context, key) {
if (
context.mutablePath !== true ||
!Array.isArray(context.path)
) {
let childContext = {
path: withPath(context.path, key),
options: context.options,
mutablePath: false,
};
return schema["~run"](value, childContext);
}
context.path.push(key);
try {
return schema["~run"](value, context);
} finally {
context.path.pop();
}
}
function shouldAbortEarly(options) {
let libraryAbortEarly =
options?.libraryOptions?.abortEarly;
let abortEarly = options?.abortEarly ?? libraryAbortEarly;
return Boolean(abortEarly);
}
function withPath(path, key) {
return path.length === 0 ? [key] : [...path, key];
}
function getErrorMap(options) {
let libraryErrorMap = options?.libraryOptions?.errorMap;
if (typeof options?.errorMap === "function")
return options.errorMap;
if (typeof libraryErrorMap === "function")
return libraryErrorMap;
}
function getLocale(options) {
let libraryLocale = options?.libraryOptions?.locale;
if (typeof options?.locale === "string")
return options.locale;
if (typeof libraryLocale === "string")
return libraryLocale;
}
function resolveIssueMessage(options, context) {
let errorMap = getErrorMap(options);
if (!errorMap) return context.defaultMessage;
return errorMap(context) ?? context.defaultMessage;
}
function createIssueFromContext(context, descriptor) {
let path = [...(descriptor.path ?? context.path)];
return createIssue(
resolveIssueMessage(context.options, {
code: descriptor.code,
defaultMessage: descriptor.defaultMessage,
path,
values: descriptor.values,
input: descriptor.input,
locale: getLocale(context.options),
}),
path,
);
}
function createIssue(message, path) {
return !path || path.length === 0
? { message }
: {
message,
path,
};
}
function fail(message, path, options) {
if (!options?.code)
return { issues: [createIssue(message, path)] };
path = path && [...path];
return {
issues: [
createIssue(
resolveIssueMessage(options.parseOptions, {
code: options.code,
defaultMessage: message,
path,
values: options.values,
input: options.input,
locale: getLocale(options.parseOptions),
}),
path,
),
],
};
}
function array(elementSchema) {
return createBuiltinSchema(
function validate(value, context) {
if (!Array.isArray(value))
return fail("Expected array", context.path, {
code: "type.array",
input: value,
parseOptions: context.options,
});
let abortEarly = shouldAbortEarly(context.options);
let issues = [];
let outputValues = new Array(value.length);
let index = 0;
for (let item of value) {
let result = runAtPath(
elementSchema,
item,
context,
index,
);
if (result.issues) {
if (abortEarly) return result;
issues.push(...result.issues);
} else outputValues[index] = result.value;
index += 1;
}
if (issues.length > 0) return { issues };
outputValues.length = index;
return { value: outputValues };
},
);
}
function enum_(values) {
return createBuiltinSchema(
function validate(value, context) {
for (let allowed of values)
if (value === allowed) return { value };
return fail(
"Expected one of: " + values.map(String).join(", "),
context.path,
{
code: "enum.invalid_value",
input: value,
values: { values: [...values] },
parseOptions: context.options,
},
);
},
);
}
function instanceof_(constructor) {
return createBuiltinSchema(
function validate(value, context) {
if (!(value instanceof constructor))
return fail(
"Expected instance of " + constructor.name,
context.path,
{
code: "instanceof.invalid_type",
input: value,
values: { constructorName: constructor.name },
parseOptions: context.options,
},
);
return { value };
},
);
}
function nullable(schema) {
return createBuiltinSchema(
function validate(value, context) {
if (value === null) return { value: null };
return schema["~run"](value, context);
},
);
}
function number() {
return createBuiltinSchema(
function validate(value, context) {
if (
typeof value !== "number" ||
!Number.isFinite(value)
)
return fail("Expected number", context.path, {
code: "type.number",
input: value,
parseOptions: context.options,
});
return { value };
},
);
}
function object(shape, options) {
return createBuiltinSchema(
function validate(value, context) {
if (
typeof value !== "object" ||
value === null ||
Array.isArray(value)
)
return fail("Expected object", context.path, {
code: "type.object",
input: value,
parseOptions: context.options,
});
let abortEarly = shouldAbortEarly(context.options);
let issues = [];
let outputValues = {};
let input = value;
let unknownKeys = options?.unknownKeys ?? "strip";
for (let key of Object.keys(shape)) {
let result = runAtPath(
shape[key],
input[key],
context,
key,
);
if (result.issues) {
if (abortEarly) return result;
issues.push(...result.issues);
} else if (
Object.prototype.hasOwnProperty.call(
input,
key,
) ||
result.value !== void 0
)
outputValues[key] = result.value;
}
if (
unknownKeys === "passthrough" ||
unknownKeys === "error"
)
for (let key in input) {
if (
!Object.prototype.hasOwnProperty.call(
input,
key,
)
)
continue;
if (
Object.prototype.hasOwnProperty.call(shape, key)
)
continue;
if (unknownKeys === "passthrough")
outputValues[key] = input[key];
else {
let issue = createIssueFromContext(context, {
code: "object.unknown_key",
defaultMessage: "Unknown key",
input: input[key],
path: withPath(context.path, key),
values: { key },
});
if (abortEarly) return { issues: [issue] };
issues.push(issue);
}
}
if (issues.length > 0) return { issues };
return { value: outputValues };
},
);
}
function string() {
return createBuiltinSchema(
function validate(value, context) {
if (typeof value !== "string")
return fail("Expected string", context.path, {
code: "type.string",
input: value,
parseOptions: context.options,
});
return { value };
},
);
}
var ValidationError = class extends Error {
issues;
constructor(issues, message = "Validation failed") {
super(message);
this.name = "ValidationError";
this.issues = issues;
}
};
function parse(schema, value, options) {
let result = schema["~standard"].validate(value, options);
if (result.issues)
throw new ValidationError(result.issues);
return result.value;
}
function minLength(length) {
return {
check(value) {
return value.length >= length;
},
code: "string.min_length",
values: { min: length },
message:
"Expected at least " + String(length) + " characters",
};
}
function maxLength(length) {
return {
check(value) {
return value.length <= length;
},
code: "string.max_length",
values: { max: length },
message:
"Expected at most " + String(length) + " characters",
};
}
function url() {
return {
check(value) {
try {
new URL(value);
return true;
} catch {
return false;
}
},
code: "string.url",
message: "Expected valid URL",
};
}
function min(limit) {
return {
check(value) {
return value >= limit;
},
code: "number.min",
values: { min: limit },
message:
"Expected number greater than or equal to " +
String(limit),
};
}
function max(limit) {
return {
check(value) {
return value <= limit;
},
code: "number.max",
values: { max: limit },
message:
"Expected number less than or equal to " +
String(limit),
};
}
const imageSchema = object({
id: number(),
created: instanceof_(Date),
title: string().pipe(minLength(1), maxLength(100)),
type: enum_(["jpg", "png"]),
size: number(),
url: string().pipe(url()),
});
const ratingSchema = object({
id: number(),
stars: number().pipe(min(1), max(5)),
title: string().pipe(minLength(1), maxLength(100)),
text: string().pipe(minLength(1), maxLength(1e3)),
images: array(imageSchema),
});
parse(
object({
id: number(),
created: instanceof_(Date),
title: string().pipe(minLength(1), maxLength(100)),
brand: string().pipe(minLength(1), maxLength(30)),
description: string().pipe(
minLength(1),
maxLength(500),
),
price: number().pipe(min(1), max(1e4)),
discount: nullable(number().pipe(min(1), max(100))),
quantity: number().pipe(min(0), max(10)),
tags: array(string().pipe(minLength(1), maxLength(30))),
images: array(imageSchema),
ratings: array(ratingSchema),
}),
{},
);
const builtinSchemas = new WeakSet();
function createSchema(validator) {
let schema = {
"~standard": {
version: 1,
vendor: "data-schema",
validate(value, options) {
return validator(value, {
path: [],
options,
mutablePath: isBuiltinSchema(schema),
});
},
},
"~run"(value, context) {
if (context.mutablePath)
return validator(value, {
path: [...context.path],
options: context.options,
});
return validator(value, context);
},
pipe(...checks) {
if (checks.length === 0) return schema;
return createDerivedSchema(schema, function validate(value, context) {
let result = schema["~run"](value, context);
if (result.issues) return result;
for (let check of checks)
if (!check.check(result.value)) {
if (!check.code)
return {
issues: [
createIssue(check.message ?? "Check failed", [
...context.path,
]),
],
};
return {
issues: [
createIssueFromContext(context, {
code: check.code,
defaultMessage: check.message ?? "Check failed",
input: result.value,
values: check.values,
}),
],
};
}
return result;
});
},
refine(predicate, message) {
return createDerivedSchema(schema, function validate(value, context) {
let result = schema["~run"](value, context);
if (result.issues) return result;
if (!predicate(result.value)) {
if (message !== void 0)
return { issues: [createIssue(message, [...context.path])] };
return {
issues: [
createIssueFromContext(context, {
code: "refine.failed",
defaultMessage: "Refinement failed",
input: result.value,
}),
],
};
}
return result;
});
},
transform(transformer) {
return createDerivedSchema(schema, function validate(value, context) {
let result = schema["~run"](value, context);
if (result.issues) return result;
return { value: transformer(result.value) };
});
},
};
return schema;
}
function isBuiltinSchema(schema) {
return builtinSchemas.has(schema);
}
function createBuiltinSchema(validator) {
let schema = createSchema(validator);
schema["~run"] = validator;
builtinSchemas.add(schema);
return schema;
}
function createDerivedSchema(source, validator) {
return isBuiltinSchema(source)
? createBuiltinSchema(validator)
: createSchema(validator);
}
function runAtPath(schema, value, context, key) {
if (context.mutablePath !== true || !Array.isArray(context.path)) {
let childContext = {
path: withPath(context.path, key),
options: context.options,
mutablePath: false,
};
return schema["~run"](value, childContext);
}
context.path.push(key);
try {
return schema["~run"](value, context);
} finally {
context.path.pop();
}
}
function shouldAbortEarly(options) {
let libraryAbortEarly = options?.libraryOptions?.abortEarly;
let abortEarly = options?.abortEarly ?? libraryAbortEarly;
return Boolean(abortEarly);
}
function withPath(path, key) {
return path.length === 0 ? [key] : [...path, key];
}
function getErrorMap(options) {
let libraryErrorMap = options?.libraryOptions?.errorMap;
if (typeof options?.errorMap === "function") return options.errorMap;
if (typeof libraryErrorMap === "function") return libraryErrorMap;
}
function getLocale(options) {
let libraryLocale = options?.libraryOptions?.locale;
if (typeof options?.locale === "string") return options.locale;
if (typeof libraryLocale === "string") return libraryLocale;
}
function resolveIssueMessage(options, context) {
let errorMap = getErrorMap(options);
if (!errorMap) return context.defaultMessage;
return errorMap(context) ?? context.defaultMessage;
}
function createIssueFromContext(context, descriptor) {
let path = [...(descriptor.path ?? context.path)];
return createIssue(
resolveIssueMessage(context.options, {
code: descriptor.code,
defaultMessage: descriptor.defaultMessage,
path,
values: descriptor.values,
input: descriptor.input,
locale: getLocale(context.options),
}),
path,
);
}
function createIssue(message, path) {
return !path || path.length === 0
? { message }
: {
message,
path,
};
}
function fail(message, path, options) {
if (!options?.code) return { issues: [createIssue(message, path)] };
path = path && [...path];
return {
issues: [
createIssue(
resolveIssueMessage(options.parseOptions, {
code: options.code,
defaultMessage: message,
path,
values: options.values,
input: options.input,
locale: getLocale(options.parseOptions),
}),
path,
),
],
};
}
function array(elementSchema) {
return createBuiltinSchema(function validate(value, context) {
if (!Array.isArray(value))
return fail("Expected array", context.path, {
code: "type.array",
input: value,
parseOptions: context.options,
});
let abortEarly = shouldAbortEarly(context.options);
let issues = [];
let outputValues = new Array(value.length);
let index = 0;
for (let item of value) {
let result = runAtPath(elementSchema, item, context, index);
if (result.issues) {
if (abortEarly) return result;
issues.push(...result.issues);
} else outputValues[index] = result.value;
index += 1;
}
if (issues.length > 0) return { issues };
outputValues.length = index;
return { value: outputValues };
});
}
function enum_(values) {
return createBuiltinSchema(function validate(value, context) {
for (let allowed of values) if (value === allowed) return { value };
return fail(
"Expected one of: " + values.map(String).join(", "),
context.path,
{
code: "enum.invalid_value",
input: value,
values: { values: [...values] },
parseOptions: context.options,
},
);
});
}
function instanceof_(constructor) {
return createBuiltinSchema(function validate(value, context) {
if (!(value instanceof constructor))
return fail("Expected instance of " + constructor.name, context.path, {
code: "instanceof.invalid_type",
input: value,
values: { constructorName: constructor.name },
parseOptions: context.options,
});
return { value };
});
}
function nullable(schema) {
return createBuiltinSchema(function validate(value, context) {
if (value === null) return { value: null };
return schema["~run"](value, context);
});
}
function number() {
return createBuiltinSchema(function validate(value, context) {
if (typeof value !== "number" || !Number.isFinite(value))
return fail("Expected number", context.path, {
code: "type.number",
input: value,
parseOptions: context.options,
});
return { value };
});
}
function object(shape, options) {
return createBuiltinSchema(function validate(value, context) {
if (typeof value !== "object" || value === null || Array.isArray(value))
return fail("Expected object", context.path, {
code: "type.object",
input: value,
parseOptions: context.options,
});
let abortEarly = shouldAbortEarly(context.options);
let issues = [];
let outputValues = {};
let input = value;
let unknownKeys = options?.unknownKeys ?? "strip";
for (let key of Object.keys(shape)) {
let result = runAtPath(shape[key], input[key], context, key);
if (result.issues) {
if (abortEarly) return result;
issues.push(...result.issues);
} else if (
Object.prototype.hasOwnProperty.call(input, key) ||
result.value !== void 0
)
outputValues[key] = result.value;
}
if (unknownKeys === "passthrough" || unknownKeys === "error")
for (let key in input) {
if (!Object.prototype.hasOwnProperty.call(input, key)) continue;
if (Object.prototype.hasOwnProperty.call(shape, key)) continue;
if (unknownKeys === "passthrough") outputValues[key] = input[key];
else {
let issue = createIssueFromContext(context, {
code: "object.unknown_key",
defaultMessage: "Unknown key",
input: input[key],
path: withPath(context.path, key),
values: { key },
});
if (abortEarly) return { issues: [issue] };
issues.push(issue);
}
}
if (issues.length > 0) return { issues };
return { value: outputValues };
});
}
function string() {
return createBuiltinSchema(function validate(value, context) {
if (typeof value !== "string")
return fail("Expected string", context.path, {
code: "type.string",
input: value,
parseOptions: context.options,
});
return { value };
});
}
var ValidationError = class extends Error {
issues;
constructor(issues, message = "Validation failed") {
super(message);
this.name = "ValidationError";
this.issues = issues;
}
};
function parse(schema, value, options) {
let result = schema["~standard"].validate(value, options);
if (result.issues) throw new ValidationError(result.issues);
return result.value;
}
function minLength(length) {
return {
check(value) {
return value.length >= length;
},
code: "string.min_length",
values: { min: length },
message: "Expected at least " + String(length) + " characters",
};
}
function maxLength(length) {
return {
check(value) {
return value.length <= length;
},
code: "string.max_length",
values: { max: length },
message: "Expected at most " + String(length) + " characters",
};
}
function url() {
return {
check(value) {
try {
new URL(value);
return true;
} catch {
return false;
}
},
code: "string.url",
message: "Expected valid URL",
};
}
function min(limit) {
return {
check(value) {
return value >= limit;
},
code: "number.min",
values: { min: limit },
message: "Expected number greater than or equal to " + String(limit),
};
}
function max(limit) {
return {
check(value) {
return value <= limit;
},
code: "number.max",
values: { max: limit },
message: "Expected number less than or equal to " + String(limit),
};
}
const imageSchema = object({
id: number(),
created: instanceof_(Date),
title: string().pipe(minLength(1), maxLength(100)),
type: enum_(["jpg", "png"]),
size: number(),
url: string().pipe(url()),
});
const ratingSchema = object({
id: number(),
stars: number().pipe(min(1), max(5)),
title: string().pipe(minLength(1), maxLength(100)),
text: string().pipe(minLength(1), maxLength(1e3)),
images: array(imageSchema),
});
parse(
object({
id: number(),
created: instanceof_(Date),
title: string().pipe(minLength(1), maxLength(100)),
brand: string().pipe(minLength(1), maxLength(30)),
description: string().pipe(minLength(1), maxLength(500)),
price: number().pipe(min(1), max(1e4)),
discount: nullable(number().pipe(min(1), max(100))),
quantity: number().pipe(min(0), max(10)),
tags: array(string().pipe(minLength(1), maxLength(30))),
images: array(imageSchema),
ratings: array(ratingSchema),
}),
{},
);
const builtinSchemas = new WeakSet();
function createSchema(validator) {
let schema = {
"~standard": {
version: 1,
vendor: "data-schema",
validate(value, options) {
return validator(value, {
path: [],
options,
mutablePath: isBuiltinSchema(schema),
});
},
},
"~run"(value, context) {
if (context.mutablePath)
return validator(value, {
path: [...context.path],
options: context.options,
});
return validator(value, context);
},
pipe(...checks) {
if (checks.length === 0) return schema;
return createDerivedSchema(schema, function validate(value, context) {
let result = schema["~run"](value, context);
if (result.issues) return result;
for (let check of checks)
if (!check.check(result.value)) {
if (!check.code)
return { issues: [createIssue(check.message ?? "Check failed", [...context.path])] };
return {
issues: [
createIssueFromContext(context, {
code: check.code,
defaultMessage: check.message ?? "Check failed",
input: result.value,
values: check.values,
}),
],
};
}
return result;
});
},
refine(predicate, message) {
return createDerivedSchema(schema, function validate(value, context) {
let result = schema["~run"](value, context);
if (result.issues) return result;
if (!predicate(result.value)) {
if (message !== void 0) return { issues: [createIssue(message, [...context.path])] };
return {
issues: [
createIssueFromContext(context, {
code: "refine.failed",
defaultMessage: "Refinement failed",
input: result.value,
}),
],
};
}
return result;
});
},
transform(transformer) {
return createDerivedSchema(schema, function validate(value, context) {
let result = schema["~run"](value, context);
if (result.issues) return result;
return { value: transformer(result.value) };
});
},
};
return schema;
}
function isBuiltinSchema(schema) {
return builtinSchemas.has(schema);
}
function createBuiltinSchema(validator) {
let schema = createSchema(validator);
schema["~run"] = validator;
builtinSchemas.add(schema);
return schema;
}
function createDerivedSchema(source, validator) {
return isBuiltinSchema(source) ? createBuiltinSchema(validator) : createSchema(validator);
}
function runAtPath(schema, value, context, key) {
if (context.mutablePath !== true || !Array.isArray(context.path)) {
let childContext = {
path: withPath(context.path, key),
options: context.options,
mutablePath: false,
};
return schema["~run"](value, childContext);
}
context.path.push(key);
try {
return schema["~run"](value, context);
} finally {
context.path.pop();
}
}
function shouldAbortEarly(options) {
let libraryAbortEarly = options?.libraryOptions?.abortEarly;
let abortEarly = options?.abortEarly ?? libraryAbortEarly;
return Boolean(abortEarly);
}
function withPath(path, key) {
return path.length === 0 ? [key] : [...path, key];
}
function getErrorMap(options) {
let libraryErrorMap = options?.libraryOptions?.errorMap;
if (typeof options?.errorMap === "function") return options.errorMap;
if (typeof libraryErrorMap === "function") return libraryErrorMap;
}
function getLocale(options) {
let libraryLocale = options?.libraryOptions?.locale;
if (typeof options?.locale === "string") return options.locale;
if (typeof libraryLocale === "string") return libraryLocale;
}
function resolveIssueMessage(options, context) {
let errorMap = getErrorMap(options);
if (!errorMap) return context.defaultMessage;
return errorMap(context) ?? context.defaultMessage;
}
function createIssueFromContext(context, descriptor) {
let path = [...(descriptor.path ?? context.path)];
return createIssue(
resolveIssueMessage(context.options, {
code: descriptor.code,
defaultMessage: descriptor.defaultMessage,
path,
values: descriptor.values,
input: descriptor.input,
locale: getLocale(context.options),
}),
path,
);
}
function createIssue(message, path) {
return !path || path.length === 0
? { message }
: {
message,
path,
};
}
function fail(message, path, options) {
if (!options?.code) return { issues: [createIssue(message, path)] };
path = path && [...path];
return {
issues: [
createIssue(
resolveIssueMessage(options.parseOptions, {
code: options.code,
defaultMessage: message,
path,
values: options.values,
input: options.input,
locale: getLocale(options.parseOptions),
}),
path,
),
],
};
}
function array(elementSchema) {
return createBuiltinSchema(function validate(value, context) {
if (!Array.isArray(value))
return fail("Expected array", context.path, {
code: "type.array",
input: value,
parseOptions: context.options,
});
let abortEarly = shouldAbortEarly(context.options);
let issues = [];
let outputValues = new Array(value.length);
let index = 0;
for (let item of value) {
let result = runAtPath(elementSchema, item, context, index);
if (result.issues) {
if (abortEarly) return result;
issues.push(...result.issues);
} else outputValues[index] = result.value;
index += 1;
}
if (issues.length > 0) return { issues };
outputValues.length = index;
return { value: outputValues };
});
}
function enum_(values) {
return createBuiltinSchema(function validate(value, context) {
for (let allowed of values) if (value === allowed) return { value };
return fail("Expected one of: " + values.map(String).join(", "), context.path, {
code: "enum.invalid_value",
input: value,
values: { values: [...values] },
parseOptions: context.options,
});
});
}
function instanceof_(constructor) {
return createBuiltinSchema(function validate(value, context) {
if (!(value instanceof constructor))
return fail("Expected instance of " + constructor.name, context.path, {
code: "instanceof.invalid_type",
input: value,
values: { constructorName: constructor.name },
parseOptions: context.options,
});
return { value };
});
}
function nullable(schema) {
return createBuiltinSchema(function validate(value, context) {
if (value === null) return { value: null };
return schema["~run"](value, context);
});
}
function number() {
return createBuiltinSchema(function validate(value, context) {
if (typeof value !== "number" || !Number.isFinite(value))
return fail("Expected number", context.path, {
code: "type.number",
input: value,
parseOptions: context.options,
});
return { value };
});
}
function object(shape, options) {
return createBuiltinSchema(function validate(value, context) {
if (typeof value !== "object" || value === null || Array.isArray(value))
return fail("Expected object", context.path, {
code: "type.object",
input: value,
parseOptions: context.options,
});
let abortEarly = shouldAbortEarly(context.options);
let issues = [];
let outputValues = {};
let input = value;
let unknownKeys = options?.unknownKeys ?? "strip";
for (let key of Object.keys(shape)) {
let result = runAtPath(shape[key], input[key], context, key);
if (result.issues) {
if (abortEarly) return result;
issues.push(...result.issues);
} else if (Object.prototype.hasOwnProperty.call(input, key) || result.value !== void 0)
outputValues[key] = result.value;
}
if (unknownKeys === "passthrough" || unknownKeys === "error")
for (let key in input) {
if (!Object.prototype.hasOwnProperty.call(input, key)) continue;
if (Object.prototype.hasOwnProperty.call(shape, key)) continue;
if (unknownKeys === "passthrough") outputValues[key] = input[key];
else {
let issue = createIssueFromContext(context, {
code: "object.unknown_key",
defaultMessage: "Unknown key",
input: input[key],
path: withPath(context.path, key),
values: { key },
});
if (abortEarly) return { issues: [issue] };
issues.push(issue);
}
}
if (issues.length > 0) return { issues };
return { value: outputValues };
});
}
function string() {
return createBuiltinSchema(function validate(value, context) {
if (typeof value !== "string")
return fail("Expected string", context.path, {
code: "type.string",
input: value,
parseOptions: context.options,
});
return { value };
});
}
var ValidationError = class extends Error {
issues;
constructor(issues, message = "Validation failed") {
super(message);
this.name = "ValidationError";
this.issues = issues;
}
};
function parse(schema, value, options) {
let result = schema["~standard"].validate(value, options);
if (result.issues) throw new ValidationError(result.issues);
return result.value;
}
function minLength(length) {
return {
check(value) {
return value.length >= length;
},
code: "string.min_length",
values: { min: length },
message: "Expected at least " + String(length) + " characters",
};
}
function maxLength(length) {
return {
check(value) {
return value.length <= length;
},
code: "string.max_length",
values: { max: length },
message: "Expected at most " + String(length) + " characters",
};
}
function url() {
return {
check(value) {
try {
new URL(value);
return true;
} catch {
return false;
}
},
code: "string.url",
message: "Expected valid URL",
};
}
function min(limit) {
return {
check(value) {
return value >= limit;
},
code: "number.min",
values: { min: limit },
message: "Expected number greater than or equal to " + String(limit),
};
}
function max(limit) {
return {
check(value) {
return value <= limit;
},
code: "number.max",
values: { max: limit },
message: "Expected number less than or equal to " + String(limit),
};
}
const imageSchema = object({
id: number(),
created: instanceof_(Date),
title: string().pipe(minLength(1), maxLength(100)),
type: enum_(["jpg", "png"]),
size: number(),
url: string().pipe(url()),
});
const ratingSchema = object({
id: number(),
stars: number().pipe(min(1), max(5)),
title: string().pipe(minLength(1), maxLength(100)),
text: string().pipe(minLength(1), maxLength(1e3)),
images: array(imageSchema),
});
parse(
object({
id: number(),
created: instanceof_(Date),
title: string().pipe(minLength(1), maxLength(100)),
brand: string().pipe(minLength(1), maxLength(30)),
description: string().pipe(minLength(1), maxLength(500)),
price: number().pipe(min(1), max(1e4)),
discount: nullable(number().pipe(min(1), max(100))),
quantity: number().pipe(min(0), max(10)),
tags: array(string().pipe(minLength(1), maxLength(30))),
images: array(imageSchema),
ratings: array(ratingSchema),
}),
{},
);