import type { StandardSchemaV1 } from "./standard-schema.ts"; import { type StructContext } from "./struct-context.ts"; import { _StructError } from "./struct-error.ts"; export type Schema = Record; export type StructExec = (input: unknown, context: StructContext) => T; export type Infer = T extends Structure ? U : never; export type InferObject = { [K in keyof T]: Infer; }; export declare function _additionalProperties(fields: Record, input: Record): string[]; /** * @group Structure * * **Structure** is a composable primative for processing values to make sure they are what you expect them to be, optionally coercing the value into something else. It's also strongly-typed so values that are validated have the correct TypeScript type too. * * The Structure class also supports [StandardSchema v1](https://standardschema.dev) so you can use it anywhere that supports that standard. */ export declare class Structure { static Error: typeof _StructError; readonly "~standard": StandardSchemaV1.Props; schema: Schema; _process: StructExec; constructor(schema: Schema, process: StructExec); /** * Execute the structure by passing it a value and getting back the result if it is successful, otherwise a {@link Structure.Error} is thrown */ process(input?: unknown, context?: StructContext): T; /** * Get a JSON schema from the structure, where equivalent fields are available. */ getFullSchema(): Schema; /** @deprecated use {@link getFullSchema} */ getSchema(): Schema; /** * Define a string-based value with an optional `fallback`. * * ```js * Structure.string() * Structure.string("Geoff Testington") * ``` */ static string(fallback?: string | undefined): Structure; /** * Define a number-based value with an optional `fallback`, * it will also try to parse floating-point values from strings. * * ```js * Structure.number() * Structure.number("Geoff Testington") * ``` */ static number(fallback?: number | undefined): Structure; /** * Define a boolean value with an optional `fallback`. * * ```js * Structure.boolean() * Structure.boolean(false) * ``` */ static boolean(fallback?: boolean | undefined): Structure; /** * Define a URL value with an optional `fallback`, * that will be coerced into a `URL`. * * ```js * Structure.url() * Structure.url("http://example.com") * Structure.url(new URL("http://example.com")) * ``` */ static url(fallback?: string | URL | undefined): Structure; /** * Define a group of structures under an object. * Each field needs to matched their respective Structures and no additionaly fields are allowed. * * ```js * Structure.object({ * name: Structure.string(), * age: Structure.number(), * }) * ``` */ static object>(fields: { [K in keyof T]: Structure; }): Structure; /** * Define a list of values that each match the same structure. * * ```js * // An array of strings * Structure.array( * Structure.string() * ) * * // An array of objects * Structure.array( * Structure.object({ * name: Structure.string(), * age: Structure.number() * }) * ) * ``` */ static array(struct: Structure): Structure>; /** * Define a specific value that must be exactly equal. * * * ```js * Structure.literal("click_event") * Structure.literal(42) * Structure.literal(true) * ``` */ static literal(value: T): Structure; /** * Define a Structure that must match one of a set of Structures * * ```js * Structure.union([ * Structure.object({ * type: Structure.literal("click"), * element: Structure.string() * }), * Structure.object({ * type: Structure.literal("login"), * }), * ]) * ``` */ static union[]>(types: T): Structure>; /** * @unstable * * Attempts to create a Structure from a parsed [JSON Schema](https://json-schema.org/specification) value. * This is implemented on a as-needed bases, currently it supports: * - "const" → `Structure.literal` * - type=string → `Structure.string` * - type=number → `Structure.number` * - type=boolean → `Structure.boolean` * - type=array → "items" are recursively parsed and put into a `Structure.array` * - type=object → "properties" are recursively parsed and put into a `Structure.object` * - anyOf → `Structure.union` * * ```js * Structure.fromJSONSchema({ type: "string" }) * Structure.fromJSONSchema({ type: "number" }) * Structure.fromJSONSchema({ type: "boolean" }) * Structure.fromJSONSchema({ * type: "object", * properties: { * name: { type:"string" }, * age: { type: "number" } * }, * required: ["name"] * }) * Structure.fromJSONSchema({ type: "array", items: { type: "string" } }) * Structure.fromJSONSchema({ * anyOf: [ * { type: "string" }, * { type: "number" } * ] * }); * ``` * * notes * - array "prefixItems" are not supported, maybe they could be mapped to tuples? */ static fromJSONSchema(schema: any): Structure; /** * @unstable * * Creates a Structure for arrays where each index has a different validation * * ```js * Structure.tuple([Structure.string(), Structure.number()]) * ``` */ static tuple[]>(types: T): Structure>; /** * @unstable * * Creates a Structure for objects that map a key to a common type of value * * ```js * Structure.record(Structure.string(), Structure.number()) * Structure.record( * Structure.string(), * Structure.object({ name: Structure.string() }) * ) * Structure.record( * Structure.enum(["name", "address", "emailAddress"]), * Structure.string() * ) * ``` */ static record(keyStruct: Structure, valueStruct: Structure): Structure>; /** * Define a Structure to validate the value is `null` * * ```js * Structure.null() * ``` */ static null(): Structure; /** * Define a Structure that lets any value through * * ```js * Structure.any() * ``` */ static any(): Structure; /** * Create a Structure that validates an object where some or none of the fields match their respective Structures. * Only fields specified may be set, nothing additional. * * ```js * Structure.partial({ * name: Structure.string(), * age: Structure.number() * }) * ``` */ static partial>(fields: { [K in keyof T]: Structure; }): Structure<{ [K in keyof T]?: T[K]; }>; /** * Creates a Structure that validates dates or values that can be turned into dates * through the [Date constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date). * * ```js * Structure.date() * ``` */ static date(): Structure; /** * Creates a Structure that validates a value is either another structure or a null value * * ```js * Structure.nullable(Structure.string()) * ``` */ static nullable(input: Structure): Structure; /** * Creates a Structure that validates a value is one of a set of literals * * ```js * Structure.enum(['a string', 42, false]) * ``` */ static enum(values: T): Structure; /** * Creates a Structure that validates a value is the `undefined` value * * ```js * Structure.undefined() * ``` */ static undefined(): Structure; /** * Creates a Structure that validates another structure or is not defined * * ```js * Structure.optional(Structure.string()) * ``` */ static optional(input: Structure): Structure; } //# sourceMappingURL=structure.d.ts.map