@samveys/validator

Schema bertipe yang mengikuti Standard Schema v1

Versi
1.0.35 Nov 2026
Pasang
pnpm add @samveys/validator
Dokumentasi
README paket ini

Daftar ini dibaca dari versi 1.0.2; jalankan pnpm sync untuk memperbaruinya.

Import
import { isOptional, toJsonSchema, schema, ValidationError } from '@samveys/validator'

Fungsi (2)

isOptional Fungsi

True when the schema accepts `undefined` (optional or defaulted): the property is not required.

function isOptional(schema: StandardSchemaV1): boolean;

Diekspor dari packages/validator/src/json-schema.ts

toJsonSchema Fungsi

JSON Schema (draft 2020-12, as used by OpenAPI 3.1) for a `@samveys/validator` schema, describing its INPUT. Custom `refine()` checks and `transform()`s cannot be expressed and are left out; unknown Standard Schema vendors become `{}` (any value).

function toJsonSchema(schema: StandardSchemaV1): JsonSchema;

Diekspor dari packages/validator/src/json-schema.ts

Nilai (1)

schema Nilai

Every factory of the locked `SchemaBuilder` contract; all return immutable schemas.

const schema: SchemaBuilder;

Diekspor dari packages/validator/src/builders.ts

Class (1)

ValidationError Class

Thrown by `parse`; carries every issue found, not only the first.

class ValidationError extends Error {
    readonly issues: readonly ValidationIssueOut[];
    constructor(issues: readonly SchemaIssue[]);
}

Diekspor dari packages/validator/src/schema.ts

Interface (7)

ArraySchema Interface

Tidak ada komentar dokumentasi.

interface ArraySchema<Item extends StandardSchemaV1> extends Schema<InferInput<Item>[], Infer<Item>[]> {
    min(length: number, message?: string): ArraySchema<Item>;
    max(length: number, message?: string): ArraySchema<Item>;
}

Diekspor dari packages/validator/src/contracts.ts

NumberSchema Interface

Tidak ada komentar dokumentasi.

interface NumberSchema extends Schema<number, number> {
    min(value: number, message?: string): NumberSchema;
    max(value: number, message?: string): NumberSchema;
    int(message?: string): NumberSchema;
    positive(message?: string): NumberSchema;
}

Diekspor dari packages/validator/src/contracts.ts

ObjectSchema Interface

Tidak ada komentar dokumentasi.

interface ObjectSchema<S extends ObjectShape> extends Schema<ObjectInput<S>, ObjectOutput<S>> {
    readonly shape: S;
    /** Default: unknown keys are removed from the output (used by response schemas). */
    strip(): ObjectSchema<S>;
    /** Unknown keys are an issue. */
    strict(): ObjectSchema<S>;
    /** Unknown keys are copied, except `__proto__`, `constructor`, `prototype`. */
    passthrough(): ObjectSchema<S>;
    extend<E extends ObjectShape>(shape: E): ObjectSchema<Omit<S, keyof E> & E>;
    pick<K extends keyof S & string>(...keys: readonly K[]): ObjectSchema<Pick<S, K>>;
  …
Tampilkan seluruh deklarasi (15 baris)
interface ObjectSchema<S extends ObjectShape> extends Schema<ObjectInput<S>, ObjectOutput<S>> {
    readonly shape: S;
    /** Default: unknown keys are removed from the output (used by response schemas). */
    strip(): ObjectSchema<S>;
    /** Unknown keys are an issue. */
    strict(): ObjectSchema<S>;
    /** Unknown keys are copied, except `__proto__`, `constructor`, `prototype`. */
    passthrough(): ObjectSchema<S>;
    extend<E extends ObjectShape>(shape: E): ObjectSchema<Omit<S, keyof E> & E>;
    pick<K extends keyof S & string>(...keys: readonly K[]): ObjectSchema<Pick<S, K>>;
    omit<K extends keyof S & string>(...keys: readonly K[]): ObjectSchema<Omit<S, K>>;
    partial(): ObjectSchema<{
        [K in keyof S]: Schema<InferInput<S[K]> | undefined, Infer<S[K]> | undefined>;
    }>;
}

Diekspor dari packages/validator/src/contracts.ts

Schema Interface

A schema producing `Output` from `Input`. Immutable: every modifier returns a new schema.

interface Schema<Input = unknown, Output = Input> extends StandardSchemaV1<Input, Output> {
    readonly kind: string;
    /** Accepts `undefined`; the key may be absent in objects. */
    optional(): Schema<Input | undefined, Output | undefined>;
    /** Accepts `null`. */
    nullable(): Schema<Input | null, Output | null>;
    /** Uses `value` when the input is `undefined`; the key may be absent in objects. */
    default(value: Exclude<Output, undefined> | (() => Exclude<Output, undefined>)): Schema<Input | undefined, Exclude<Output, undefined>>;

Diekspor dari packages/validator/src/contracts.ts

SchemaBuilder Interface

Tidak ada komentar dokumentasi.

interface SchemaBuilder {
    string(message?: string): StringSchema;
    number(message?: string): NumberSchema;
    /** Accepts numbers and numeric strings (query and params); output is a number. */
    coerceNumber(message?: string): NumberSchema;
    boolean(message?: string): Schema<boolean, boolean>;
    /** Accepts booleans and `'true' | 'false' | '1' | '0'`. */
    coerceBoolean(message?: string): Schema<boolean | string, boolean>;
    literal<const V extends string | number | boolean | null>(value: V, message?: string): Schema<V, V>;
    enum<const V extends readonly [string, ...string[]]>(values: V, message?: string): Schema<V[number], V[number]>;
  …
Tampilkan seluruh deklarasi (17 baris)
interface SchemaBuilder {
    string(message?: string): StringSchema;
    number(message?: string): NumberSchema;
    /** Accepts numbers and numeric strings (query and params); output is a number. */
    coerceNumber(message?: string): NumberSchema;
    boolean(message?: string): Schema<boolean, boolean>;
    /** Accepts booleans and `'true' | 'false' | '1' | '0'`. */
    coerceBoolean(message?: string): Schema<boolean | string, boolean>;
    literal<const V extends string | number | boolean | null>(value: V, message?: string): Schema<V, V>;
    enum<const V extends readonly [string, ...string[]]>(values: V, message?: string): Schema<V[number], V[number]>;
    array<Item extends StandardSchemaV1>(item: Item, message?: string): ArraySchema<Item>;
    object<S extends ObjectShape>(shape: S, message?: string): ObjectSchema<S>;
    /** First matching member wins. */
    union<const M extends readonly [StandardSchemaV1, StandardSchemaV1, ...StandardSchemaV1[]]>(members: M): Schema<InferInput<M[number]>, Infer<M[number]>>;
    record<V extends StandardSchemaV1>(value: V): Schema<Record<string, InferInput<V>>, Record<string, Infer<V>>>;
    unknown(): Schema<unknown, unknown>;
}

Diekspor dari packages/validator/src/contracts.ts

StringSchema Interface

Tidak ada komentar dokumentasi.

interface StringSchema<Output extends string = string> extends Schema<string, Output> {
    min(length: number, message?: string): StringSchema<Output>;
    max(length: number, message?: string): StringSchema<Output>;
    length(length: number, message?: string): StringSchema<Output>;
    /** Practical address check: one `@`, no spaces, a dot in the domain, ≤ 254 chars. */
    email(message?: string): StringSchema<Output>;
    /** Absolute `http:` or `https:` URL. */
    url(message?: string): StringSchema<Output>;
    uuid(message?: string): StringSchema<Output>;
    /** Regex is allowed here (input validation), never in route patterns. */
  …
Tampilkan seluruh deklarasi (15 baris)
interface StringSchema<Output extends string = string> extends Schema<string, Output> {
    min(length: number, message?: string): StringSchema<Output>;
    max(length: number, message?: string): StringSchema<Output>;
    length(length: number, message?: string): StringSchema<Output>;
    /** Practical address check: one `@`, no spaces, a dot in the domain, ≤ 254 chars. */
    email(message?: string): StringSchema<Output>;
    /** Absolute `http:` or `https:` URL. */
    url(message?: string): StringSchema<Output>;
    uuid(message?: string): StringSchema<Output>;
    /** Regex is allowed here (input validation), never in route patterns. */
    regex(pattern: RegExp, message?: string): StringSchema<Output>;
    /** Strips leading and trailing whitespace before checks. */
    trim(): StringSchema<Output>;
    toLowerCase(): StringSchema<Output>;
}

Diekspor dari packages/validator/src/contracts.ts

ValidationIssueOut Interface

Tidak ada komentar dokumentasi.

interface ValidationIssueOut {
    readonly message: string;
    /** Property keys from the root. */
    readonly path: readonly PropertyKey[];
}

Diekspor dari packages/validator/src/contracts.ts

Tipe (6)

Infer Tipe

Tidak ada komentar dokumentasi.

type Infer<S extends StandardSchemaV1> = StandardSchemaV1.InferOutput<S>;

Diekspor dari packages/validator/src/contracts.ts

InferInput Tipe

Tidak ada komentar dokumentasi.

type InferInput<S extends StandardSchemaV1> = StandardSchemaV1.InferInput<S>;

Diekspor dari packages/validator/src/contracts.ts

ObjectInput Tipe

Tidak ada komentar dokumentasi.

type ObjectInput<S extends ObjectShape> = Simplify<{
    [K in Exclude<keyof S, OptionalInputKeys<S>>]: InferInput<S[K]>;
} & {
    [K in OptionalInputKeys<S>]?: InferInput<S[K]>;
}>;

Diekspor dari packages/validator/src/contracts.ts

ObjectOutput Tipe

Tidak ada komentar dokumentasi.

type ObjectOutput<S extends ObjectShape> = Simplify<{
    [K in Exclude<keyof S, OptionalOutputKeys<S>>]: Infer<S[K]>;
} & {
    [K in OptionalOutputKeys<S>]?: Infer<S[K]>;
}>;

Diekspor dari packages/validator/src/contracts.ts

ObjectShape Tipe

Tidak ada komentar dokumentasi.

type ObjectShape = Readonly<Record<string, StandardSchemaV1>>;

Diekspor dari packages/validator/src/contracts.ts

UnknownKeys Tipe

Tidak ada komentar dokumentasi.

type UnknownKeys = 'strip' | 'strict' | 'passthrough';

Diekspor dari packages/validator/src/contracts.ts