UNPKG

3.32 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<!-- Body -->
9
10Details 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
18import { getConfig } from '@expo/config';
19import Schemer from '@expo/schemer';
20
21const { exp } = getConfig(projectRoot);
22const schema = await getSchemaAsync(exp.sdkVersion);
23const validator = new Schemer(require('schema.json'));
24
25validator.validateName('Wilson Zhao');
26validator.validateAssets(exp);
27```
28
29### Schema-only validation
30
31```javascript
32const validator = new Schemer(require('schema.json'));
33try {
34 await validator.validateSchemaAsync(require('data.json'));
35} catch (e) {
36 console.error(e);
37}
38```
39
40### Validating a property
41
42```javascript
43const validator = new Schemer(require('schema.json'));
44await validator.validateName('Wilson Zhao');
45```
46
47## Description
48
49Schemer takes in a custom JSON Schema and uses it to validate various data.
50
51Under the hood, it uses Ajv (https://github.com/epoberezkin/ajv) as the Javascript engine for basic schema validation.
52However, 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
71All 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`.
72If they exist, the errors are thrown at the end of each public-facing function.
73
74All 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
82Returns 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
86Returns 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.
87Otherwise, it rejects and throws an array of `ValidationError`s.
88
89#### .validateAll(Object data) -> Promise
90
91Runs both `.validateSchemaAsync` and `.validateAssetsAsync`.
92Returns 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
96Extracts the subSchema for the given field path and validates the data against it. Also checks for the meta tag.
97Returns 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
101Contains an array of ValidationErrors
102
103#### new ValidationError({errorCode, fieldPath, message, data, meta}) -> Object