|
1 | | -# Simple JavaScript Object & JSON Validator |
| 1 | +# Simple JavaScript/TypeScript Object & JSON Validator |
2 | 2 |
|
3 | | -This package is a lightweight schema validator that is useful for asserting keys and types inside an object or a JSON response, often from HTTP requests. It allows you to reject requests with incomplete or malformed payloads before passing the data along to other parts of the system. It is also helpful for providing users with feedback about what is malformed. |
| 3 | +This package is a lightweight schema validator that is useful for asserting keys and types inside an object or a JSON response, often from HTTP requests. It allows you to reject requests with incomplete or malformed payloads before passing the data along to other parts of the system. It is also helpful for providing users with feedback about what is malformed. |
| 4 | + |
| 5 | +This package includes TypeScript type definitions out of the box. |
4 | 6 |
|
5 | 7 | This package pairs especially well with MongoDB because, after validation, you can confidently insert the JSON objects into the database. |
6 | 8 |
|
| 9 | +## Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install @lms5400/easy-validation |
| 13 | +``` |
| 14 | + |
7 | 15 | ## Simple Example |
8 | 16 |
|
9 | 17 | ```js |
@@ -140,19 +148,19 @@ import { types, conditions, validateData } from 'easy-validation'; |
140 | 148 | const schema1 = { |
141 | 149 | name: types.isString, |
142 | 150 | props: { |
143 | | - x: types.isNumber, |
144 | | - y: types.isNumber, |
145 | | - z: types.isNumber, |
| 151 | + x: types.isNumeric, |
| 152 | + y: types.isNumeric, |
| 153 | + z: types.isNumeric, |
146 | 154 | } |
147 | 155 | }; |
148 | 156 |
|
149 | 157 | const schema2 = { |
150 | 158 | name: types.isString, |
151 | 159 | props: types.isObject.and( |
152 | 160 | conditions.ofShape({ |
153 | | - x: types.isNumber, |
154 | | - y: types.isNumber, |
155 | | - z: types.isNumber, |
| 161 | + x: types.isNumeric, |
| 162 | + y: types.isNumeric, |
| 163 | + z: types.isNumeric, |
156 | 164 | }), |
157 | 165 | conditions.required // This makes a difference! |
158 | 166 | ) |
@@ -201,7 +209,7 @@ const result = await validateData(schema, sampleData); |
201 | 209 | This is a more complete sample of what the API might look like in practice. |
202 | 210 |
|
203 | 211 | ```js |
204 | | -const {types, conditions, validateData} = require('./src/index'); |
| 212 | +const {types, conditions, validateData} = require('easy-validation'); |
205 | 213 |
|
206 | 214 | const color = { |
207 | 215 | red: types.isInteger.and(conditions.range(0,255), conditions.required), |
@@ -268,7 +276,7 @@ result: |
268 | 276 | Use `isCustom` to pass asynchronous functions to value validation. This is useful for performing a database query to validate an ID asynchronously. Alternatively you may also create any custom condition you wish and add it to the `and()` parameters for a given type. Conditions are asynchronously by nature too. |
269 | 277 |
|
270 | 278 | ```js |
271 | | - const {types, conditions, validateData} = require('./src/index'); |
| 279 | + const {types, conditions, validateData} = require('easy-validation'); |
272 | 280 |
|
273 | 281 | async function validateKey(value) { |
274 | 282 | try { |
|
0 commit comments