shelving
Version:
Toolkit for using data in JavaScript.
35 lines (34 loc) • 2.04 kB
TypeScript
import type { Data, Database, PartialData } from "../util/data.js";
import type { Identifier, Item } from "../util/item.js";
import { type Key } from "../util/object.js";
import type { NullableSchema } from "./NullableSchema.js";
import type { SchemaOptions, Schemas } from "./Schema.js";
import { Schema } from "./Schema.js";
/** Allowed options for `PropsSchema` (a schema that has props). */
export interface DataSchemaOptions<T extends Data> extends SchemaOptions {
readonly props: Schemas<T>;
readonly value?: Partial<T> | undefined;
}
/** Validate a data object. */
export declare class DataSchema<T extends Data> extends Schema<unknown> {
readonly value: T;
readonly props: Schemas<T>;
constructor({ one, title, props, value: partialValue, ...options }: DataSchemaOptions<T>);
validate(unsafeValue?: unknown): T;
/** Make a new `DataSchema` that only uses a defined subset of the current props. */
pick<K extends Key<T>>(...keys: K[]): DataSchema<Pick<T, K>>;
/** Make a new `DataSchema` that omits one or more of the current props. */
omit<K extends Key<T>>(...keys: K[]): DataSchema<Omit<T, K>>;
}
/** Set of named data schemas. */
export type DataSchemas<T extends Database> = {
[K in keyof T]: DataSchema<T[K]>;
};
/** Create a `DataSchema` for a set of properties. */
export declare const DATA: <T extends Data>(props: Schemas<T>) => DataSchema<T>;
/** Valid data object with specifed properties, or `null` */
export declare const NULLABLE_DATA: <T extends Data>(props: Schemas<T>) => NullableSchema<T>;
/** Create a `DataSchema` that validates partially, i.e. properties can be their value, or `undefined` */
export declare function PARTIAL<T extends Data>(source: Schemas<T> | DataSchema<T>): DataSchema<PartialData<T>>;
/** Create a `DataSchema` that validates a data item, i.e. it has a string or number `.id` identifier property. */
export declare function ITEM<I extends Identifier, T extends Data>(id: Schema<I>, schemas: Schemas<T> | DataSchema<T>): DataSchema<Item<I, T>>;