UNPKG

3.58 kBMarkdownView Raw
1<!-- Title -->
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<p align="center">
9 <img src="https://flat.badgen.net/packagephobia/install/@expo/schemer">
10
11 <a href="https://www.npmjs.com/package/@expo/schemer">
12 <img src="https://flat.badgen.net/npm/dw/@expo/schemer" target="_blank" />
13 </a>
14</p>
15
16<!-- Body -->
17
18Details can be found here:
19* https://paper.dropbox.com/doc/Expo-Schema-Validation-Library-mQU07rRejSnEe4Vf5dkcS
20
21## Usage
22
23### Usage with XDL
24
25```javascript
26import { ExpSchema } from '@expo/xdl';
27import { getConfig } from '@expo/config';
28import Schemer from '@expo/schemer';
29const { exp } = getConfig(projectRoot);
30const schema = await getSchemaAsync(exp.sdkVersion);
31const validator = new Schemer(schema);
32validator.validateName('Wilson Zhao');
33validator.validateAssets(exp);
34```
35
36### Schema-only validation
37
38```javascript
39const validator = new Schemer(require('schema.json'));
40try {
41 await validator.validateSchemaAsync(require('data.json'));
42} catch (e) {
43 console.error(e);
44}
45```
46
47### Validating a property
48
49```javascript
50const validator = new Schemer(require('schema.json'));
51await validator.validateName('Wilson Zhao');
52```
53
54## Description
55
56Schemer takes in a custom JSON Schema and uses it to validate various data.
57
58Under the hood, it uses Ajv (https://github.com/epoberezkin/ajv) as the Javascript engine for basic schema validation.
59However, 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:
60
61```javascript
62{
63 meta:
64 {
65 asset,
66 contentType, //mime type
67 dimensions: {width, height},
68 square,
69
70 // For custom error messages and docs
71 regexHuman,
72 autogenerated,
73 notHuman
74 }
75}
76```
77
78All 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`.
79If they exist, the errors are thrown at the end of each public-facing function.
80
81All public-facing functions are async functions because asset validation has to be async (accessing the file-system or making a web request).
82
83## API
84
85#### new Schemer(Object JSON Schema, Object options) -> Object
86
87#### .validateSchemaAsync(Object data) -> Promise
88
89Returns a promise that resolve to `true` if the data is conforms to the schema. Otherwise, it rejects and throws an array of `ValidationError`s.
90
91#### .validateAssetsAsync(Object data) -> Promise
92
93Returns 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.
94Otherwise, it rejects and throws an array of `ValidationError`s.
95
96#### .validateAll(Object data) -> Promise
97
98Runs both `.validateSchemaAsync` and `.validateAssetsAsync`.
99Returns a promise that resolve to `true` if the data passes both functions. Otherwise, it rejects and throws an array of `ValidationError`s.
100
101#### .validateProperty(String fieldPath, Object data) -> Promise
102
103Extracts the subSchema for the given field path and validates the data against it. Also checks for the meta tag.
104Returns a promise that resolve to `true` if the data conforms to the subschema. Otherwise, it rejects and throws an array of `ValidationError`s.
105
106#### .errors
107
108Contains an array of ValidationErrors
109
110#### new ValidationError({errorCode, fieldPath, message, data, meta}) -> Object