Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
refactor(schemer): move to expo/expo (#4795)
Browse files Browse the repository at this point in the history
* refactor(schemer): move to `expo/expo`

* refactor(schemer): drop from ci
  • Loading branch information
byCedric authored Dec 11, 2023
1 parent ad5c0fc commit f922410
Show file tree
Hide file tree
Showing 23 changed files with 2 additions and 2,556 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ jobs:
expo-codemod,
pkcs12,
pwa,
schemer,
# webpack-config,
xdl,
]
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/test_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ jobs:
expo-codemod,
pkcs12,
pwa,
schemer,
webpack-config,
xdl,
]
Expand Down
21 changes: 0 additions & 21 deletions packages/schemer/LICENSE

This file was deleted.

111 changes: 2 additions & 109 deletions packages/schemer/README.md
Original file line number Diff line number Diff line change
@@ -1,110 +1,3 @@
<!-- Title -->
<h1 align="center">
👋 Welcome to <br><code>@expo/schemer</code>
</h1>
# `@expo/schemer`

<p align="center">A Schema validation library for Expo.</p>

<p align="center">
<img src="https://flat.badgen.net/packagephobia/install/@expo/schemer">

<a href="https://www.npmjs.com/package/@expo/schemer">
<img src="https://flat.badgen.net/npm/dw/@expo/schemer" target="_blank" />
</a>
</p>

<!-- Body -->

Details can be found here:
* https://paper.dropbox.com/doc/Expo-Schema-Validation-Library-mQU07rRejSnEe4Vf5dkcS

## Usage

### Usage with XDL

```javascript
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

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

### Validating a property

```javascript
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:

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

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

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 `ValidationError`s.

#### .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 `ValidationError`s.

#### .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 `ValidationError`s.

#### .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 `ValidationError`s.

#### .errors

Contains an array of ValidationErrors

#### new ValidationError({errorCode, fieldPath, message, data, meta}) -> Object
This package has [moved to the `expo/expo` repo](https://github.com/expo/expo/tree/main/packages/%40expo/schemer).
21 changes: 0 additions & 21 deletions packages/schemer/__tests__/__snapshots__/network-test.ts.snap

This file was deleted.

196 changes: 0 additions & 196 deletions packages/schemer/__tests__/__snapshots__/test.ts.snap

This file was deleted.

10 changes: 0 additions & 10 deletions packages/schemer/__tests__/files/app.json

This file was deleted.

9 changes: 0 additions & 9 deletions packages/schemer/__tests__/files/bad.json

This file was deleted.

Loading

0 comments on commit f922410

Please sign in to comment.