UNPKG

xsschema

Version:

extra-small, Standard Schema-based alternative to typeschema.

249 lines (222 loc) 9.13 kB
/** The Standard Schema interface. */ interface StandardSchemaV1<Input = unknown, Output = Input> { /** The Standard Schema properties. */ readonly "~standard": StandardSchemaV1.Props<Input, Output>; } declare namespace StandardSchemaV1 { /** The Standard Schema properties interface. */ export interface Props<Input = unknown, Output = Input> { /** The version number of the standard. */ readonly version: 1; /** The vendor name of the schema library. */ readonly vendor: string; /** Validates unknown input values. */ readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>; /** Inferred types associated with the schema. */ readonly types?: Types<Input, Output> | undefined; } /** The result interface of the validate function. */ export type Result<Output> = SuccessResult<Output> | FailureResult; /** The result interface if validation succeeds. */ export interface SuccessResult<Output> { /** The typed output value. */ readonly value: Output; /** The non-existent issues. */ readonly issues?: undefined; } /** The result interface if validation fails. */ export interface FailureResult { /** The issues of failed validation. */ readonly issues: ReadonlyArray<Issue>; } /** The issue interface of the failure output. */ export interface Issue { /** The error message of the issue. */ readonly message: string; /** The path of the issue, if any. */ readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined; } /** The path segment interface of the issue. */ export interface PathSegment { /** The key representing a path segment. */ readonly key: PropertyKey; } /** The Standard Schema types interface. */ export interface Types<Input = unknown, Output = Input> { /** The input type of the schema. */ readonly input: Input; /** The output type of the schema. */ readonly output: Output; } /** Infers the input type of a Standard Schema. */ export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"]; /** Infers the output type of a Standard Schema. */ export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"]; export { }; } // ================================================================================================== // JSON Schema Draft 07 // ================================================================================================== // https://tools.ietf.org/html/draft-handrews-json-schema-validation-01 // -------------------------------------------------------------------------------------------------- /** * Primitive type * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1 */ type JSONSchema7TypeName = | "string" // | "number" | "integer" | "boolean" | "object" | "array" | "null"; /** * Primitive type * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1 */ type JSONSchema7Type = | string // | number | boolean | JSONSchema7Object | JSONSchema7Array | null; // Workaround for infinite type recursion interface JSONSchema7Object { [key: string]: JSONSchema7Type; } // Workaround for infinite type recursion // https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540 interface JSONSchema7Array extends Array<JSONSchema7Type> {} /** * Meta schema * * Recommended values: * - 'http://json-schema.org/schema#' * - 'http://json-schema.org/hyper-schema#' * - 'http://json-schema.org/draft-07/schema#' * - 'http://json-schema.org/draft-07/hyper-schema#' * * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5 */ type JSONSchema7Version = string; /** * JSON Schema v7 * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01 */ type JSONSchema7Definition = JSONSchema7 | boolean; interface JSONSchema7 { $id?: string | undefined; $ref?: string | undefined; $schema?: JSONSchema7Version | undefined; $comment?: string | undefined; /** * @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4 * @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A */ $defs?: { [key: string]: JSONSchema7Definition; } | undefined; /** * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1 */ type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined; enum?: JSONSchema7Type[] | undefined; const?: JSONSchema7Type | undefined; /** * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2 */ multipleOf?: number | undefined; maximum?: number | undefined; exclusiveMaximum?: number | undefined; minimum?: number | undefined; exclusiveMinimum?: number | undefined; /** * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3 */ maxLength?: number | undefined; minLength?: number | undefined; pattern?: string | undefined; /** * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4 */ items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined; additionalItems?: JSONSchema7Definition | undefined; maxItems?: number | undefined; minItems?: number | undefined; uniqueItems?: boolean | undefined; contains?: JSONSchema7Definition | undefined; /** * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5 */ maxProperties?: number | undefined; minProperties?: number | undefined; required?: string[] | undefined; properties?: { [key: string]: JSONSchema7Definition; } | undefined; patternProperties?: { [key: string]: JSONSchema7Definition; } | undefined; additionalProperties?: JSONSchema7Definition | undefined; dependencies?: { [key: string]: JSONSchema7Definition | string[]; } | undefined; propertyNames?: JSONSchema7Definition | undefined; /** * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6 */ if?: JSONSchema7Definition | undefined; then?: JSONSchema7Definition | undefined; else?: JSONSchema7Definition | undefined; /** * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7 */ allOf?: JSONSchema7Definition[] | undefined; anyOf?: JSONSchema7Definition[] | undefined; oneOf?: JSONSchema7Definition[] | undefined; not?: JSONSchema7Definition | undefined; /** * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7 */ format?: string | undefined; /** * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8 */ contentMediaType?: string | undefined; contentEncoding?: string | undefined; /** * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9 */ definitions?: { [key: string]: JSONSchema7Definition; } | undefined; /** * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10 */ title?: string | undefined; description?: string | undefined; default?: JSONSchema7Type | undefined; readOnly?: boolean | undefined; writeOnly?: boolean | undefined; examples?: JSONSchema7Type | undefined; } type Infer<T extends StandardSchemaV1> = StandardSchemaV1.InferOutput<T>; type InferIn<T extends StandardSchemaV1> = StandardSchemaV1.InferInput<T>; declare const jsonSchema: (schema: JSONSchema7) => JSONSchema7; declare const strictJsonSchema: (schema: JSONSchema7) => JSONSchema7; /** * Converts a Standard Schema to a JSON schema. * * @note This method is `async` because it has to `await import` the schema vendor's dependencies. */ declare const toJsonSchema: (schema: StandardSchemaV1) => Promise<JSONSchema7>; type ToJsonSchemaFn = (schema: unknown) => JSONSchema7 | Promise<JSONSchema7>; /** @experimental */ declare const initToJsonSchemaSyncVendor: (vendor: string) => Promise<Map<string, ToJsonSchemaFn>>; /** @experimental */ declare const toJsonSchemaSync: (schema: StandardSchemaV1) => JSONSchema7; /** @see {@link https://github.com/standard-schema/standard-schema#how-do-i-accept-standard-schemas-in-my-library} */ declare const validate: <T extends StandardSchemaV1>(schema: T, input: StandardSchemaV1.InferInput<T>) => Promise<StandardSchemaV1.InferOutput<T>>; export { type Infer, type InferIn, type JSONSchema7 as JsonSchema, StandardSchemaV1 as Schema, initToJsonSchemaSyncVendor, jsonSchema, strictJsonSchema, toJsonSchema, toJsonSchemaSync, validate };