Skip to content

Commit

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

Moving the actively used `@expo/schemer` over from `expo/expo-cli`.

Currently used in:
- `expo-doctor`
- Our backend

Counter-part PR: expo/expo-cli#4795

# How

- Copied `@expo/schemer` from `expo/expo-cli`
- Updated `tsconfig.json` to point at this monorepo config
- Integrated `expo-module-scripts`
- Added missing types packages
- Updated tests to work with Jest (+ overall easier to read)
- Resolved issues with snapshots
- Resolved linting issues
- Reconfigured e2e tests
- Dropped lodash

# Test Plan

See if CI passes.

# Checklist

<!--
Please check the appropriate items below if they apply to your diff.
This is required for changes to Expo modules.
-->

- [x] Documentation is up to date to reflect these changes (eg:
https://docs.expo.dev and README.md).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).

---------

Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
  • Loading branch information
byCedric and expo-bot committed Dec 11, 2023
1 parent f66b1de commit 535a81f
Show file tree
Hide file tree
Showing 36 changed files with 4,283 additions and 30 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/packages/@expo/metro-runtime @EvanBacon @bycedric @marklawlor
/packages/@expo/package-manager @EvanBacon @bycedric
/packages/@expo/plist @EvanBacon @bycedric
/packages/@expo/schemer @bycedric
/packages/babel-preset-expo @brentvatne @ide @EvanBacon
/packages/create-expo @EvanBacon @bycedric
/packages/expo-* @alanjhughes
Expand Down
1 change: 1 addition & 0 deletions packages/@expo/schemer/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
2 changes: 2 additions & 0 deletions packages/@expo/schemer/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @generated by expo-module-scripts
module.exports = require('expo-module-scripts/eslintrc.base.js');
13 changes: 13 additions & 0 deletions packages/@expo/schemer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

## Unpublished

### 🛠 Breaking changes

### 🎉 New features

### 🐛 Bug fixes

### 💡 Others

- Move package from `expo/expo-cli` to `expo/expo`. ([#25769](https://github.com/expo/expo/pull/25769) by [@byCedric](https://github.com/byCedric))
21 changes: 21 additions & 0 deletions packages/@expo/schemer/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017-present, 650 Industries

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
103 changes: 103 additions & 0 deletions packages/@expo/schemer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<!-- Title -->
<h1 align="center">
👋 Welcome to <br><code>@expo/schemer</code>
</h1>

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

<!-- Body -->

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

## Usage

### Usage with XDL

```javascript
import { getConfig } from '@expo/config';
import Schemer from '@expo/schemer';

const { exp } = getConfig(projectRoot);
const schema = await getSchemaAsync(exp.sdkVersion);
const validator = new Schemer(require('schema.json'));

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
32 changes: 32 additions & 0 deletions packages/@expo/schemer/build/Error.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions packages/@expo/schemer/build/Error.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/@expo/schemer/build/Error.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/@expo/schemer/build/Util.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions packages/@expo/schemer/build/Util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/@expo/schemer/build/Util.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions packages/@expo/schemer/build/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 535a81f

Please sign in to comment.