@expo/schemer

中文文档
2.4.3发布于 2026/10/31MITTypeScriptCommonJS构建来源已验证近 30 天下载 12万

Centralized scheme validation library for Expo

原始使用文档
文档目录6 节
  1. Usage
  2. Usage with XDL
  3. Schema-only validation
  4. Validating a property
  5. Description
  6. API

👋 Welcome to
@expo/schemer

A Schema validation library for Expo.

Details can be found here:

Usage

Usage with 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(require('schema.json'));

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,
    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 resolves to true if the data conforms to the schema. Otherwise, it rejects and throws an array of ValidationErrors.

.validateAssetsAsync(Object data) -> Promise

Returns a promise that resolves to true if the data 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 resolves 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 resolves 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

版本与发布记录

@expo/schemer 共发布 260 个版本,首次发布于 2017/07/14;过去 12 个月发布了 69 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.4.3latest2026/10/3113 天前
2.4.22026/10/1728 天前
2.4.12026/09/291 个月前
2.4.02026/09/112 个月前
3.0.0-canary-20260909-ea7a89acanary2026/09/092 个月前
3.0.0-canary-20260902-26df09e2026/09/022 个月前

其他发布通道:alpha → 1.3.27-alpha.0,sdk-50 → 1.5.0,sdk-51 → 1.5.2,canary-sdk-55 → 2.1.5-canary-20260429-a5e59cf,canary-sdk-56 → 2.2.2-canary-20260701-9100865,canary → 3.0.0-canary-20260909-ea7a89a。

关于 @expo/schemer 的常见问题

@expo/schemer 是什么?

Centralized scheme validation library for Expo

如何安装 @expo/schemer?

在项目目录运行 npm install @expo/schemer,或使用 pnpm add / yarn add / bun add。

@expo/schemer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@expo/schemer 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@expo/schemer 可以商用吗?

@expo/schemer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@expo/schemer 的下载量有多少?

根据 npm 官方下载统计,@expo/schemer 在 2026/10/14 – 2026/11/12 这 30 天内被下载 119,944 次,日均约 3,998 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@expo/schemer 还在维护吗?

最新版本 2.4.3 发布于 2026/10/31(13 天前);至今共发布 260 个版本,其中过去 12 个月发布了 69 个;首次发布于 2017/07/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。