@vielzeug/spell
v26.11.33
Published
Zero-dependency schema validation with Standard Schema interoperability
Readme
@vielzeug/spell
Zero-dependency schema validation with Standard Schema interoperability
Installation
pnpm add @vielzeug/spell
npm install @vielzeug/spell
yarn add @vielzeug/spellQuick Start
import { s, type Infer } from '@vielzeug/spell';
const UserSchema = s.object({
id: s.coerce.number().int().positive(),
name: s.string().trim().min(1),
email: s.string().trim().email(),
role: s.enum(['admin', 'editor', 'viewer']).default('viewer'),
tags: s.array(s.string()).unique().default([]),
});
type User = Infer<typeof UserSchema>;
const result = UserSchema.safeParse({ id: '42', name: 'Ada', email: '[email protected]' });
if (result.success) {
const user: User = result.data;
console.log(user.id); // 42
// Assertion form: narrows type, throws SpellValidationError on failure
UserSchema.assert(result.data, 'user');
} else {
const { fieldErrors, formErrors } = result.error.flattenFirst();
console.log(fieldErrors, formErrors);
}Documentation
License
MIT © Helmuth Saatkamp: part of the Vielzeug monorepo.
