GitXplorerGitXplorer
N

typeguard-composer

public
0 stars
0 forks
10 issues

Commits

List of commits on branch develop.
Verified
74e7bce4e43303722f9b7833c01f2f40cc2d7a7e

build(deps): bump y18n from 4.0.0 to 4.0.1 (#5)

ddependabot[bot] committed 4 years ago
Unverified
979f263b0e73133450fe4080bd5949d207209884

refactor: format code with Prettier

NNoNameProvided committed 4 years ago
Unverified
d5f5cfc3d41b1de0fdde457230f10910e7703b6c

build: update project tooling

NNoNameProvided committed 4 years ago
Unverified
75202813e3322dcdffae7a1616ec13dc4cdaadea

refactor: format code with Prettier

NNoNameProvided committed 4 years ago
Verified
431bef56f3b83a83fc648422e244632465c3ccde

build(deps): bump lodash from 4.17.15 to 4.17.19 (#3)

ddependabot[bot] committed 4 years ago
Unverified
56aacf1f7565c92f152341d0468ae3cc64efdf7f

docs: add TODO

NNoNameProvided committed 5 years ago

README

The README file for this repository.

typeguard-composer

CI codecov

Performant and composable type guards for runtime data validation.

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

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:

validate(schema: TypeSchema, value: any, options: ValidatorOptions): boolean

Validators

  • validators are simple functions what implement the type guard pattern described the Typescript documentation
  • 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 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