DataSchema.pick()method
Make a new DataSchema that only uses a defined subset of the current props.
pick(...keys: K[]): DataSchema<Pick<T, K>>
new DataSchema<T>({ one = "item", title = "Item", props, value: partialValue, ...options }: DataSchemaOptions<T>)| Param | Type | |
|---|---|---|
options | DataSchemaOptions<T> | Options for DataSchema. required |
.props | Schemas<T> | A named schema for each property the data object must have. required readonly |
.value | Partial<T> | Partial default value merged over the per-prop defaults. readonly |
| Return | |
|---|---|
DataSchema<T> | Schema that validates a data object against a fixed set of named property schemas. |
| Property | Type | |
|---|---|---|
.props | Schemas<T> | A set of named schemas in { name: schema } format. required readonly |
Schema that validates a data object against a fixed set of named property schemas.
props.undefined outputs removed (via validateData()).Validates a plain object whose properties each have their own schema. The term Data in Shelving refers to a plain object with known named properties; a DataSchema is the validator for one.
When several properties fail, the errors are joined by \n with each field name prepended — e.g. "name: Required\nprice: Minimum 0". This file also exports the DATA, ITEM, and PARTIAL sugar factories that build DataSchema instances.
DATA — validate an objectimport { DATA, BOOLEAN, StringSchema, NumberSchema } from "shelving/schema";
const PRODUCT = DATA({
name: new StringSchema({ title: "Name", min: 1, max: 100 }),
price: new NumberSchema({ title: "Price", min: 0 }),
available: BOOLEAN,
});
PRODUCT.validate({ name: "Widget", price: 9.99, available: true });
// { name: "Widget", price: 9.99, available: true }
PRODUCT.validate({ name: "", price: -1, available: true });
// throws "name: Required\nprice: Minimum 0"Use .pick() and .omit() to derive subset schemas without redefining props:
const PatchProduct = PRODUCT.omit("available");
const NameOnly = PRODUCT.pick("name");ITEM — add a typed id fieldITEM wraps a DataSchema to add a typed id field, matching the Item type in shelving/util/item.
import { ITEM, STRING, INTEGER, NUMBER } from "shelving/schema";
const PRODUCT_ITEM = ITEM(INTEGER, {
name: STRING,
price: NUMBER,
}); // Validates: { id: number, name: string, price: number }PARTIAL — make every field optionalPARTIAL wraps a DataSchema so every field becomes optional — useful for PATCH-style update payloads.
import { PARTIAL } from "shelving/schema";
const PARTIAL_PRODUCT = PARTIAL(PRODUCT);
PARTIAL_PRODUCT.validate({ price: 4.99 }); // { price: 4.99 }const schema = new DataSchema({ props: { name: STRING, age: NUMBER } });
schema.validate({ name: "Dave", age: 40 }); // { name: "Dave", age: 40 }Make a new DataSchema that only uses a defined subset of the current props.
pick(...keys: K[]): DataSchema<Pick<T, K>>
Make a new DataSchema that omits one or more of the current props.
omit(...keys: K[]): DataSchema<Omit<T, K>>