UNPKG

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