搜索插件、代码仓与 APP 分类

插件详情v5.2.2

json_schema

Dart 中的 JSON Schema 实现

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
22.4万
喜欢数
45
Pub 分数
115

安装插件

flutter pub add json_schema

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Dart 实现的 JSON Schema,可基于模式定义对 JSON 数据进行验证、解析和生成。它通过定义和强制执行结构、类型和约束,提供了一种确保数据完整性的可靠方法。该包支持静态和动态模式评估,适用于需要在多个平台上进行严格数据验证的应用程序。它与 Flutter 及其他 Dart 项目无缝集成。

适合场景

  • 验证 API 响应
  • 在表单中强制执行数据结构
  • 从模式模板生成 JSON
  • 与后端数据契约集成
  • 测试 JSON 负载
  • 动态表单生成

主要特点

  • 带有详细错误信息的模式验证
  • 对复杂嵌套架构的支持
  • JSON 与 Dart 之间的序列化和反序列化
  • 可扩展的验证规则
  • 跨平台兼容性
  • 内置架构解析

适合对象

  • 需要数据验证的 API 客户端
  • 带有动态模式的表单构建器
  • 后端集成工具
  • 数据驱动的 UI
  • 测试框架
  • 配置管理系统

使用建议

  • 需要理解 JSON Schema 规范
  • 深度嵌套模式的性能影响
  • 对自定义验证器的内置支持有限,需要扩展
  • 可能需要为大型模式集进行额外设置
  • 不适用于实时架构编辑

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

JSON Schema

A platform agnostic (web, flutter or vm) Dart library for validating JSON instances against JSON Schemas (multi-version support with latest of Draft 7).

Getting Started

  1. Ensure you have latest stable Dart installed.
  2. make pubget
  3. make format
  4. make analyze
  5. make test-with-serve-remotes
Alternative Test Running Strategy (To run specific tests, or pass other args to dart test):
  1. make serve-remotes (in a separate terminal tab - HTTP fixtures need to be served for tests to pass)
  2. make test or dart test -n <YOUR TEST>
  3. SIGINT your make serve-remotes tab or run make stop-serve-remotes

Note: For convenience, make stop-serve-remotes will be run as a prerequisite before make test-with-serve-remotes and make serve-remotes

Updating Test Fixtures
  1. make gen-fixtures Generates Dart source files that contain the JSON-Schema-Test-Suite tests and fixtures for use in cross-platform testing that doesn't require dart:io to read the files on disk.
  2. Commit the results of generation, if any.

Note: CI runs make gen-fixtures --check to ensure these are up-to-date on each

Simple Example Usage

The simplest way to create a schema is to pass JSON data directly to JsonSchema.create with a JSON String, or decoded JSON via Dart Map or bool.

After creating any schema, JSON instances can be validated by calling .validate(instance) on that schema. By default, instances are expected to be pre-parsed JSON as native dart primitives (Map, List, String, bool, num, int). You can also optionally parse at validation time by passing in a string and setting parseJson: schema.validate('{ "name": "any JSON object"}', parseJson: true).

Note: Creating JsonSchemas synchronously implies access to all $refs within the root schema. If you don't have access to all this data at the time of the construction, see "Asynchronous Creation" examples below.

import 'package:json_schema/json_schema.dart';

main() {
  /// Define schema in a Dart [Map] or use a JSON [String].
  final mustBeIntegerSchemaMap = {"type": "integer"};

  // Create some examples to validate against the schema.
  final n = 3;
  final decimals = 3.14;
  final str = 'hi';

  // Construct the schema from the schema map or JSON string.
  final schema = JsonSchema.create(mustBeIntegerSchemaMap);

  print('$n => ${schema.validate(n)}'); // true
  print('$decimals => ${schema.validate(decimals)}'); // false
  print('$str => ${schema.validate(str)}'); // false
}

Or see json_schema/example/readme/synchronous_creation/self_contained.dart

And run dart run ./example/readme/synchronous_creation/self_contained.dart

Advanced Usage

Synchronous Creation, Local Ref Cache

If you want to create JsonSchemas synchronously, and you have $refs that cannot be resolved within the root schema, but you have a cache of those $ref'd schemas locally, you can write a RefProvider to get them during schema evaluation.

See json_schema/example/readme/synchronous_creation/local_ref_cache.dart

Or run dart run ./example/readme/synchronous_creation/local_ref_cache.dart

Asynchronous Creation, Remote HTTP Refs

If you have schemas that have nested $refs that are HTTP URIs that are publicly accessible, you can use Future<JsonSchema> JsonSchema.createAsync and the references will be fetched as needed during evaluation. You can also use JsonSchema.createFromUrl if you want to fetch the root schema remotely as well (see next example).

See json_schema/example/readme/asynchronous_creation/remote_http_refs.dart

Or run dart run ./example/readme/asynchronous_creation/remote_http_refs.dart

Asynchronous Creation, From URL or File

You can also create a schema directly from a publicly accessible URL or File.

URLs

See json_schema/example/readme/asynchronous_creation/from_url.dart

Or run dart run ./example/readme/asynchronous_creation/from_url.dart

Files

See json_schema/example/readme/asynchronous_creation/from_file.dart

Or run dart run ./example/readme/asynchronous_creation/from_file.dart

Asynchronous Creation, with custom remote $refs:

If you have nested $refs that are either non-HTTP URIs or non-publicly-accessible HTTP $refs, you can supply an async RefProvider to createAsync, and perform any custom logic you need.

See json_schema/example/readme/asynchronous_creation/remote_ref_cache.dart

Or run dart run ./example/readme/asynchronous_creation/remote_ref_cache.dart

How To Use Schema Information

Schema information can be used for validation; but it can also be a valuable source of information about the structure of data. The JsonSchema class fully parses the schema first, which itself must be valid on all paths within the schema. Accessors are provided for all specified keywords of the JSON Schema specification associated with a schema, so tools can use it to create rich views of the data, like forms or diagrams.

所属分类

依赖

依赖清单 5 项

源码仓库

使用此插件的项目