搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案