# @codenhub/validation

> Validation for TypeScript where each validator is a small standalone function, so a package or app ships only the checks it uses. Works for a single value such as an email or a port, and for whole objects such as forms. The package is experimental.

A validator takes any input and returns `{ ok: true, value }` or `{ ok: false, error: { issues } }`; it never throws for invalid input. Create one by calling a factory, then call it with the value: `email()(input)`, `number({ int: true, min: 0 })(input)`. Ready-made validators cover strings, numbers, bigints, booleans, dates, functions, literals and enums, and string formats such as email, url, uuid, ip and datetime. Collections (`array`, `tuple`, `record`, `set`, `map`), `union`, `discriminatedUnion`, `intersection`, `nullable`, `withDefault`, `transform` and `lazy` combine them. Build objects with `object`, add rules with `refine`, chain with `pipe`, read the type with `Infer`, and write your own by returning `pass(value)` or `fail(...)` from any function. Text input is converted with the `coerce*` validators, and `standard` exposes a validator as a Standard Schema. Issues carry a code, a path and params, never the input; `formatIssue` and `flatten` turn them into text from a message map you pass, `englishMessages` for the built-in English, or your own to reword or localize. A validator with an asynchronous rule is an `AsyncValidator` and must be awaited.

## Documentation

- [Documentation overview](docs/index.md): The idea, results, objects, combining validators, and sync versus async.
- [Validator reference](docs/validators.md): Every validator and combinator with its options and the issue it reports.
- [Custom validators](docs/custom-validators.md): Writing validators, `refine`, asynchronous rules and testing.
- [Coercion](docs/coercion.md): `coerceString`, `coerceNumber`, `coerceBoolean`, `coerceBigint` and `coerceDate` for text input such as environment variables and form fields.
- [Standard Schema](docs/standard-schema.md): `standard(validator, messages)` for libraries that accept a Standard Schema.
- [Issues and messages](docs/errors.md): The shape of an issue, built-in codes and params, message text, localization and form errors.
- [Changelog](docs/changelog/index.md): Release notes and the migration from 0.0.1.
