@expo/schemer
Centralized scheme validation library for Expo
Last updated a day ago by brentvatne .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @expo/schemer 
SYNC missed versions from official npm registry.

Schemer

A Schema validation library for Expo, to be used both Details can be found here: https://paper.dropbox.com/doc/Expo-Schema-Validation-Library-mQU07rRejSnEe4Vf5dkcS

Usage

Usage with XDL

import { ExpSchema } from '@expo/xdl';
import { getConfig } from '@expo/config';
import Schemer from '@expo/schemer';
const { exp } = getConfig(projectRoot);
const schema = await getSchemaAsync(exp.sdkVersion);
const validator = new Schemer(schema);
validator.validateName('Wilson Zhao');
validator.validateAssets(exp);

Schema-only validation

const validator = new Schemer(require('schema.json'));
try {
  await validator.validateSchemaAsync(require('data.json'));
} catch (e) {
  console.error(e);
}

Validating a property

const validator = new Schemer(require('schema.json'));
await validator.validateName('Wilson Zhao');

Description

Schemer takes in a custom JSON Schema and uses it to validate various data.

Under the hood, it uses Ajv (https://github.com/epoberezkin/ajv) as the Javascript engine for basic schema validation. 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:

{
  meta:
  {
    asset,
    contentType, //mime type
    dimensions: {width, height},
    square,

    // For custom error messages and docs
    regexHuman,
    autogenerated
  }
}

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. If they exist, the errors are thrown at the end of each public-facing function.

All public-facing functions are async functions because asset validation has to be async (accessing the file-system or making a web request).

API

new Schemer(Object JSON Schema, Object options) -> Object

.validateSchemaAsync(Object data) -> Promise

Returns a promise that resolve to true if the data is conforms to the schema. Otherwise, it rejects and throws an array of ValidationErrors.

.validateAssetsAsync(Object data) -> Promise

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. Otherwise, it rejects and throws an array of ValidationErrors.

.validateAll(Object data) -> Promise

Runs both .validateSchemaAsync and .validateAssetsAsync. Returns a promise that resolve to true if the data passes both functions. Otherwise, it rejects and throws an array of ValidationErrors.

.validateProperty(String fieldPath, Object data) -> Promise

Extracts the subSchema for the given field path and validates the data against it. Also checks for the meta tag. Returns a promise that resolve to true if the data conforms to the subschema. Otherwise, it rejects and throws an array of ValidationErrors.

.errors

Contains an array of ValidationErrors

new ValidationError({errorCode, fieldPath, message, data, meta}) -> Object

Current Tags

  • 1.2.8-alpha.0                                ...           alpha (7 months ago)
  • 1.3.8                                ...           latest (a day ago)

59 Versions

  • 1.3.8                                ...           a day ago
  • 1.3.7                                ...           18 days ago
  • 1.3.6                                ...           a month ago
  • 1.3.5                                ...           a month ago
  • 1.3.4                                ...           3 months ago
  • 1.3.3                                ...           3 months ago
  • 1.3.2                                ...           4 months ago
  • 1.3.1                                ...           4 months ago
  • 1.3.0                                ...           5 months ago
  • 1.2.9                                ...           6 months ago
  • 1.2.8                                ...           7 months ago
  • 1.2.8-alpha.0                                ...           7 months ago
  • 1.2.7                                ...           8 months ago
  • 1.2.6                                ...           9 months ago
  • 1.2.5                                ...           a year ago
  • 1.2.4                                ...           a year ago
  • 1.2.3                                ...           a year ago
  • 1.2.2                                ...           a year ago
  • 1.2.1                                ...           a year ago
  • 1.2.0                                ...           a year ago
  • 1.1.1                                ...           2 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.44                                ...           3 years ago
  • 1.0.43                                ...           3 years ago
  • 1.0.40                                ...           3 years ago
  • 1.0.37                                ...           3 years ago
  • 1.0.36                                ...           3 years ago
  • 1.0.35                                ...           3 years ago
  • 1.0.34                                ...           3 years ago
  • 1.0.33                                ...           3 years ago
  • 1.0.32                                ...           3 years ago
  • 1.0.31                                ...           3 years ago
  • 1.0.30                                ...           3 years ago
  • 1.0.29                                ...           3 years ago
  • 1.0.28                                ...           3 years ago
  • 1.0.27                                ...           3 years ago
  • 1.0.25                                ...           3 years ago
  • 1.0.24                                ...           3 years ago
  • 1.0.23                                ...           3 years ago
  • 1.0.22                                ...           3 years ago
  • 1.0.20                                ...           3 years ago
  • 1.0.19                                ...           3 years ago
  • 1.0.18                                ...           3 years ago
  • 1.0.17                                ...           3 years ago
  • 1.0.16                                ...           3 years ago
  • 1.0.15                                ...           3 years ago
  • 1.0.14                                ...           3 years ago
  • 1.0.13                                ...           3 years ago
  • 1.0.12                                ...           3 years ago
  • 1.0.11                                ...           3 years ago
  • 1.0.10                                ...           3 years ago
  • 1.0.7                                ...           3 years ago
  • 1.0.6                                ...           3 years ago
  • 1.0.5                                ...           3 years ago
  • 1.0.4                                ...           3 years ago
  • 1.0.3                                ...           3 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Downloads
Today 7
This Week 359
This Month 950
Last Day 286
Last Week 936
Last Month 4,599
Dependencies (6)
Dev Dependencies (3)
Dependents (3)

Copyright 2014 - 2016 © taobao.org |