https://github.com/nonameprovided/typeguard-composer
Performant and composable type guards for runtime data validation.
https://github.com/nonameprovided/typeguard-composer
Last synced: over 1 year ago
JSON representation
Performant and composable type guards for runtime data validation.
- Host: GitHub
- URL: https://github.com/nonameprovided/typeguard-composer
- Owner: NoNameProvided
- License: mit
- Created: 2017-09-11T16:12:53.000Z (almost 9 years ago)
- Default Branch: develop
- Last Pushed: 2023-01-06T01:31:41.000Z (over 3 years ago)
- Last Synced: 2025-03-14T05:47:59.219Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 970 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# typeguard-composer
![CI][github-actions-badge] [![codecov][codecov-badge]][codecov-page]
Performant and composable type guards for runtime data validation.
```ts
import { validate } from 'typeguard-composer';
import { isString, isNumber } from 'typeguard-composer/validators';
import { AddressSchema } from './my-address.schema';
/**
* Schemas are simple object where every value is an object or
* array containing type guard functions or a single type guard function.
*/
const PersonSchema = {
name: isString,
age: isNumber,
addresses: [AddressSchema],
};
validate(PersonSchema, { name: 'Test Elek', age: 18, addresses: [] });
```
## Installation
```bash
npm install typeguard-composer
```
## TODO
- add two different error for schema and value errors
- add support for logging/returning invalid values
- add support for throwing error on extraneous values
- return object instead of simple true/false with reasoning (eg: "'MyValue' was expected to match isMyValidator.")
- create repo for complex types (dates, id formats, phone numbers, addresses, etc)
## Usage
_To be written... include info about defining schemas and how there validation works..._
## API
The public API is extremely small, it consists only one function to validate values against schemas and a validator for every primitive types which can be used to build custom schemas.
### `validate` function
Tries to validate the received value against the received schema. If the value is valid it returns true and false otherwise.
**Signature:**
```ts
validate(schema: TypeSchema, value: any, options: ValidatorOptions): boolean
```
---
### Validators
- validators are simple functions what implement the type guard pattern described the [Typescript documentation][ts-typeguards]
- this library includes validators for the basic primitive types only
- you can and should define your own validators when using this library
- validators should be fairly simple in complexity and check a single value
> _Note: You can check out the [NoNameProvided/typeguard-composer-validators][tcv-repo] repository to see a list of available pre-written complex schemas and validator functions._
List of included base validators:
- `isBoolean`
- `isNumber`
- `isString`
- `isSymbol`
- `isNull`
- `isUndefined`
- `isObject`
- `isArray`
- `isFunction`
- `isAny` - always returns true
- `isValue` - return true when the provided value is not `null` or `undefined`
## License
MIT Licensed
[codecov-badge]: https://codecov.io/gh/NoNameProvided/typeguard-composer/branch/master/graph/badge.svg
[codecov-page]: https://codecov.io/gh/NoNameProvided/typeguard-composer
[github-actions-badge]: https://github.com/NoNameProvided/typeguard-composer/workflows/Continuous%20Integration/badge.svg?branch=master&event=push
[tcv-repo]: https://github.com/NoNameProvided/typeguard-composer-validators
[ts-typeguards]: https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types