You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Validate Objects Against TypeScript Interfaces #
5
+
# Strongly-Typed Validators for TypeScript
6
+
*(Formerly `validate-interface`)*
6
7
7
-
Builds strongly-typed validators that can prove to the TypeScript compiler that a given object conforms to a TypeScript interface.
8
+
Build strongly-typed validators that TypeScript can understand, so that TypeScript can validate that your validator is correct.
8
9
9
10
## Installation ##
10
11
11
-
`$ npm install --save validate-interface`
12
+
`$ npm install --save typed-validation`
12
13
13
14
## Basic Usage ##
14
15
16
+
**Example:** check that a value of type `any` (perhaps from an untrusted source, such as a file) is an object that conforms to an interface called `Employee`:
This library provides a number of strongly-typed assertions which can be combined to validate the type of each property.
66
+
Validators are built by combining simple assertions using function composition and higher-order functions. For example, the `isString()` assertion returns a function which accepts a single argument of type `any` and returns a `string`. If the argument is a string, it returns the string. If the argument is not a string, it throws an error. This module provides a number of assertions, described below.
61
67
62
-
An assertion may take another assertion as its last argument; if assertion check passes, it calls the next assertion. For example, `isString(minLength(1, maxLength(10)))` first checks if the value is a string, then checks if its length is at least 1, and then checks that its length is no more than 10. If `isString` fails, `minLength` isn't run. Chaining assertions in this way allows for complex validation.
68
+
An assertion may take another assertion as its last argument; if assertion check passes, it calls the next assertion. For example, `isString(minLength(1, maxLength(10)))` first checks if the value is a string, then checks if its length is at least 1, and then checks that its length is no more than 10. If `isString` fails, `minLength` isn't run. Chaining assertions in this way allows for complex validation of types and values.
63
69
64
70
Some assertions require other assertions to come before it. For example, `minLength` can't be used by itself because it needs another assertion to check that the value has the `length` property - so something like `isString(minLength(1))` or `isArray(minLength(1))`.
0 commit comments