@bemedev/valibot-extended
v0.1.3
Published
Valibot utilities
Maintainers
Readme
@bemedev/valibot-extended
Extra helpers for Valibot: byFunction, deepPartial, soa
and trueO.
Installation
pnpm add @bemedev/valibot-extended
# or
npm install @bemedev/valibot-extendedvalibot (^1.5.0) ships as a direct dependency, so it does not need to be
installed separately. The package is published as a dual ESM/CJS build with its type
declarations, so it works in both import and require contexts.
Exports
| Export | Sub-path | Description |
| ------------------- | -------------- | ------------------------------------------------------------------------ |
| byFunction | /byFunction | Lazy schema evaluator returning the schema built by a factory |
| deepPartial | /deepPartial | Recursively converts an object, tuple or array schema into a partial one |
| soa | /soa | Union accepting either a single value or an array of values |
| trueO | /trueO | Schema accepting only plain objects |
| DeepPartial | /deepPartial | Recursive deep partial type |
| DeepPartialSchema | /deepPartial | Schema type produced by deepPartial |
Every export is available from the package root and from a dedicated sub-path:
import { byFunction, deepPartial, soa, trueO } from '@bemedev/valibot-extended';
import { deepPartial } from '@bemedev/valibot-extended/deepPartial';byFunction
Defers the creation of a schema, which keeps recursive and mutually referencing schemas easy to declare.
import { byFunction } from '@bemedev/valibot-extended';
import * as v from 'valibot';
const schema = byFunction(() => v.object({ name: v.string(), age: v.number() }));
type User = v.InferOutput<typeof schema>;
// { name: string; age: number }
const parse = v.parser(schema);
parse({ name: 'Gartner', age: 40 }); // => { name: 'Gartner', age: 40 }
parse({ name: 'Gartner', age: 'old' } as any); // => throwsdeepPartial
Turns every object entry optional, recursively. Arrays and tuples recurse into their items while keeping their shape, and any other schema is returned untouched. Objects are rebuilt as strict partial objects, so unknown keys are rejected.
import { deepPartial } from '@bemedev/valibot-extended/deepPartial';
import * as v from 'valibot';
const base = v.object({
name: v.string(),
nested: v.object({ tag: v.string() }),
tags: v.array(v.string()),
});
const schema = deepPartial(base);
const parse = v.parser(schema);
parse({}); // => {}
parse({ name: 'Gartner', nested: { tag: 'old' } }); // => valid
parse({ tags: ['smart', 'strong'] }); // => valid
parse({ toto: 67 } as any); // => throws (unknown key)
type Shape = v.InferOutput<typeof schema>;
// { name?: string; nested?: { tag?: string }; tags?: string[] }Tuples keep their length and element order:
const tuple = deepPartial(v.tuple([base, v.string()]));
// [{ name?: string; ... }, string]DeepPartial and DeepPartialSchema
Both types let you describe a deeply optional shape without re-declaring it, and pin
a schema variable to the exact schema deepPartial produces.
import {
deepPartial,
type DeepPartial,
type DeepPartialSchema,
} from '@bemedev/valibot-extended';
import * as v from 'valibot';
const base = v.object({ name: v.string(), nested: v.object({ tag: v.string() }) });
type Shape = DeepPartial<v.InferOutput<typeof base>>;
// { name?: string; nested?: { tag?: string } }
const schema: DeepPartialSchema<typeof base> = deepPartial(base);soa
Wraps a schema into a union accepting either a single value or an array of values — handy for struct-of-arrays payloads coming from a CSV or an API.
import { soa } from '@bemedev/valibot-extended';
import * as v from 'valibot';
const schema = soa(v.object({ name: v.string(), age: v.number() }));
const parse = v.parser(schema);
const many = [
{ name: 'Gartner', age: 40 },
{ name: 'Bri', age: 20 },
];
parse({ name: 'Gartner', age: 40 }); // => { name: 'Gartner', age: 40 }
parse([]); // => []
parse(many); // => array of objects
parse(123 as any); // => throwstrueO
Validates that the input is a "true" plain object. Arrays, null, primitives, class
instances and any object with a custom prototype are rejected, while objects created
from {} or Object.create(null) are accepted.
import { trueO } from '@bemedev/valibot-extended/trueO';
import * as v from 'valibot';
const parse = v.parser(trueO);
parse({}); // => {}
parse({ name: 'Gartner' }); // => { name: 'Gartner' }
parse({ nested: { tag: 'old' }, tags: ['smart'] }); // => valid
parse(Object.assign(Object.create(null), { name: 'Gartner' })); // => valid
parse([]); // => throws
parse(new Date()); // => throws
parse(Object.create({ toto: 67 })); // => throws
parse(123 as any); // => throws
type Shape = v.InferOutput<typeof trueO>;
// Record<string, any>Development
From packages/valibot:
pnpm run build # bundle the library with rolldown
pnpm run test # run the Vitest suite, coverage enabled by default
pnpm run lint # format with oxfmt, then lint with oxlintLicence
CHANGE_LOG
Read CHANGELOG.md for more details about the changes.
Author
chlbri ([email protected])
