https://github.com/nexys-system/validation
Simple, flexible and typesafe validation helpers
https://github.com/nexys-system/validation
nexys package typescript validation zerodependencies
Last synced: about 2 months ago
JSON representation
Simple, flexible and typesafe validation helpers
- Host: GitHub
- URL: https://github.com/nexys-system/validation
- Owner: nexys-system
- License: mit
- Created: 2021-04-15T08:41:14.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-10-12T09:40:21.000Z (8 months ago)
- Last Synced: 2025-03-23T22:42:20.681Z (2 months ago)
- Topics: nexys, package, typescript, validation, zerodependencies
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@nexys/validation
- Size: 170 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Validation
[](https://github.com/nexys-system/validation/actions/workflows/test.yml)
[](https://github.com/nexys-system/validation/actions/workflows/publish.yml)
[](https://www.npmjs.com/package/@nexys/validation)
[](https://www.npmjs.com/package/@nexys/validation)
[](https://bundlephobia.com/result?p=@nexys/validation)
[](https://prettier.io/)Simple, flexible and typesafe validation helpers
## Get started
`yarn add @nexys/validation`
```
import Validation, {Type, Utils} from '@nexys/validation';
```## Examples
see [tests](https://github.com/nexys-system/validation/blob/master/src/main.test.ts)
* [simple](https://github.com/nexys-system/validation/blob/master/src/main.test.ts)
* [objects](https://github.com/nexys-system/validation/blob/master/src/object.test.ts)
* [optional nested object](https://github.com/nexys-system/validation/blob/master/src/object.test.ts#L17)
* [arrays](https://github.com/nexys-system/validation/blob/master/src/array.test.ts)## Koa example
```
import Router from 'koa-router';
import bodyParser from 'koa-body';
import Validation, { Utils as VU } from '@nexys/validation';const router = new Router();
router.post(
'/update',
bodyParser(),
Validation.isShapeMiddleware({
uuid: { extraCheck: VU.checkUuid },
name: {}
}),
async ctx => {
// now that the body has been validated this can be safely typed/cast to the expected type.
// Note that the type should match the validation shape
const { uuid, name }: { uuid: Uuid; name: string } = ctx.request.body;
ctx.body = await myFunc(uuid, name);
}
);export default router.routes();
```