Skip to content

Commit da58ec7

Browse files
committed
Merge branch 'release/0.6.0'
2 parents 81a09c5 + 7bb4d67 commit da58ec7

File tree

7 files changed

+309
-251
lines changed

7 files changed

+309
-251
lines changed

README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
[![npm](https://img.shields.io/npm/v/validate-interface.svg)](https://www.npmjs.com/package/validate-interface)
2-
[![npm](https://img.shields.io/npm/dt/validate-interface.svg)](https://www.npmjs.com/package/validate-interface)
3-
[![npm](https://img.shields.io/npm/l/validate-interface.svg)](https://www.npmjs.com/package/validate-interface)
1+
[![npm](https://img.shields.io/npm/v/typed-validation.svg)](https://www.npmjs.com/package/typed-validation)
2+
[![npm](https://img.shields.io/npm/dt/typed-validation.svg)](https://www.npmjs.com/package/typed-validation)
3+
[![npm](https://img.shields.io/npm/l/typed-validation.svg)](https://www.npmjs.com/package/typed-validation)
44

5-
# Validate Objects Against TypeScript Interfaces #
5+
# Strongly-Typed Validators for TypeScript
6+
*(Formerly `validate-interface`)*
67

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.
89

910
## Installation ##
1011

11-
`$ npm install --save validate-interface`
12+
`$ npm install --save typed-validation`
1213

1314
## Basic Usage ##
1415

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`:
17+
1518
```ts
16-
// 1) Define an interface
19+
// 1) Define the interface
1720
interface Employee {
1821
name: string;
1922
roleCode: number;
2023
completedTraining: boolean | undefined;
2124
addressPostcode: string;
2225
}
2326

24-
// 2) Define a schema
27+
// 2) Define the validator
2528
const employeeValidator: Validator<Employee> = {
2629
name: isString(minLength(1)),
2730
roleCode: isNumber(min(1, max(10))),
@@ -30,16 +33,19 @@ const employeeValidator: Validator<Employee> = {
3033
};
3134

3235
// 3) Validate
33-
let bob: Employee = validate({
36+
37+
const unsafeObject: any = {
3438
name: 'Bob Smith',
3539
roleCode: 7,
3640
completedTraining: true,
3741
addressPostcode: 'AB1 2CD'
38-
}, employeeValidator);
42+
};
43+
44+
const bob: Employee = validate(unsafeObject, employeeValidator);
3945

4046
// Catch and log error messages
4147
try {
42-
let wrong: Employee = validate({
48+
const wrong: Employee = validate({
4349
name: 'Name',
4450
roleCode: 4,
4551
completedTraining: 'false',
@@ -57,9 +63,9 @@ try {
5763
```
5864

5965
## Documentation ##
60-
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.
6167

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.
6369

6470
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))`.
6571

0 commit comments

Comments
 (0)