1 |
|
2 | <h1 align="center">
|
3 | 👋 Welcome to <br><code>@expo/schemer</code>
|
4 | </h1>
|
5 |
|
6 | <p align="center">A Schema validation library for Expo.</p>
|
7 |
|
8 |
|
9 |
|
10 | Details can be found here:
|
11 | * https://paper.dropbox.com/doc/Expo-Schema-Validation-Library-mQU07rRejSnEe4Vf5dkcS
|
12 |
|
13 | ## Usage
|
14 |
|
15 | ### Usage with XDL
|
16 |
|
17 | ```javascript
|
18 | import { getConfig } from '@expo/config';
|
19 | import Schemer from '@expo/schemer';
|
20 |
|
21 | const { exp } = getConfig(projectRoot);
|
22 | const schema = await getSchemaAsync(exp.sdkVersion);
|
23 | const validator = new Schemer(require('schema.json'));
|
24 |
|
25 | validator.validateName('Wilson Zhao');
|
26 | validator.validateAssets(exp);
|
27 | ```
|
28 |
|
29 | ### Schema-only validation
|
30 |
|
31 | ```javascript
|
32 | const validator = new Schemer(require('schema.json'));
|
33 | try {
|
34 | await validator.validateSchemaAsync(require('data.json'));
|
35 | } catch (e) {
|
36 | console.error(e);
|
37 | }
|
38 | ```
|
39 |
|
40 | ### Validating a property
|
41 |
|
42 | ```javascript
|
43 | const validator = new Schemer(require('schema.json'));
|
44 | await validator.validateName('Wilson Zhao');
|
45 | ```
|
46 |
|
47 | ## Description
|
48 |
|
49 | Schemer takes in a custom JSON Schema and uses it to validate various data.
|
50 |
|
51 | Under the hood, it uses Ajv (https://github.com/epoberezkin/ajv) as the Javascript engine for basic schema validation.
|
52 | However, each subschema also contains a custom meta tag, which can be parsed for further "manual" validation. As of now, Schemer supports manual validation for assets:
|
53 |
|
54 | ```javascript
|
55 | {
|
56 | meta:
|
57 | {
|
58 | asset,
|
59 | contentType, //mime type
|
60 | dimensions: {width, height},
|
61 | square,
|
62 |
|
63 | // For custom error messages and docs
|
64 | regexHuman,
|
65 | autogenerated,
|
66 | notHuman
|
67 | }
|
68 | }
|
69 | ```
|
70 |
|
71 | All errors can be accessed in `this.errors`, which has a getter function that combines Ajv JSON Schema errors with custom meta/asset validation errors into a unified array of `ValidationErrors`.
|
72 | If they exist, the errors are thrown at the end of each public-facing function.
|
73 |
|
74 | All public-facing functions are async functions because asset validation has to be async (accessing the file-system or making a web request).
|
75 |
|
76 | ## API
|
77 |
|
78 | #### new Schemer(Object JSON Schema, Object options) -> Object
|
79 |
|
80 | #### .validateSchemaAsync(Object data) -> Promise
|
81 |
|
82 | Returns a promise that resolve to `true` if the data is conforms to the schema. Otherwise, it rejects and throws an array of `ValidationError`s.
|
83 |
|
84 | #### .validateAssetsAsync(Object data) -> Promise
|
85 |
|
86 | Returns a promise that resolve to `true` if the data is conforms to the additional validation steps found in each meta tag. For example, it will download an asset and read the header of the file to see if it is a certain content type.
|
87 | Otherwise, it rejects and throws an array of `ValidationError`s.
|
88 |
|
89 | #### .validateAll(Object data) -> Promise
|
90 |
|
91 | Runs both `.validateSchemaAsync` and `.validateAssetsAsync`.
|
92 | Returns a promise that resolve to `true` if the data passes both functions. Otherwise, it rejects and throws an array of `ValidationError`s.
|
93 |
|
94 | #### .validateProperty(String fieldPath, Object data) -> Promise
|
95 |
|
96 | Extracts the subSchema for the given field path and validates the data against it. Also checks for the meta tag.
|
97 | Returns a promise that resolve to `true` if the data conforms to the subschema. Otherwise, it rejects and throws an array of `ValidationError`s.
|
98 |
|
99 | #### .errors
|
100 |
|
101 | Contains an array of ValidationErrors
|
102 |
|
103 | #### new ValidationError({errorCode, fieldPath, message, data, meta}) -> Object
|