@produck/es-abstract-member-zod
v0.2.5
Published
Zod-schema parsers for declaring the members of an abstract constructor.
Readme
@produck/es-abstract-member-zod
Declare the members of an abstract constructor with zod schemas.
Installation
npm install @produck/es-abstract-member-zodzod (^4.0.0) is a peer dependency and has to be installed as well.
Usage
import Abstract from '@produck/es-abstract-token';
import { Zod } from '@produck/es-abstract-member-zod';
import * as zod from 'zod';
const AbstractSample = Abstract(
class Sample {},
...[
Abstract({
config: Zod(
zod.object({
name: zod.string(),
port: zod.int(),
}),
),
}),
],
);
class SubSample extends AbstractSample {
config = { name: 'sample', port: 8080 };
}API
Zod(schema) / zodParserFactory(schema)
Returns a parser validating a member against the given zod schema.
- Throws a
TypeErrorwhenschemais not a zod type. - The parser runs
schema.parse(value)and returnsvalueunchanged, so a member is typed as the input type of the schema, not its output type. For a schema using.transform()orz.coerce, that is the value the member actually holds.
License
MIT
Author
Produck
