ack_json_schema_builder 2.0.0-beta.4
ack_json_schema_builder: ^2.0.0-beta.4 copied to clipboard
JSON Schema Builder converter for the Ack validation library
ack_json_schema_builder #
Adapter between Ack schemas and json_schema_builder schemas, in both directions: .toJsonSchemaBuilder() exports an Ack schema, and .toAckSchema() imports a builder schema.
Installation #
dependencies:
ack: ^2.0.0-beta.4
ack_json_schema_builder: ^2.0.0-beta.4
json_schema_builder: ^0.1.3
This package needs ack: ^2.0.0-beta.4 and json_schema_builder: ^0.1.3. Report compatibility issues.
Usage #
Export an Ack schema, then validate data with json_schema_builder:
import 'package:ack/ack.dart';
import 'package:ack_json_schema_builder/ack_json_schema_builder.dart';
import 'package:json_schema_builder/json_schema_builder.dart';
Future<void> main() async {
const data = {'name': 'Ada', 'email': 'ada@example.com', 'age': 20};
final userSchema = Ack.object({
'name': Ack.string().minLength(2).maxLength(50),
'email': Ack.string().email(),
'age': Ack.integer().min(0).max(120).optional(),
});
final jsonSchema = userSchema.toJsonSchemaBuilder();
final errors = await jsonSchema.validate(data);
if (errors.isEmpty) {
print('Data is valid!');
} else {
print('Validation errors: $errors');
}
}
Import a builder schema into an executable Ack schema:
import 'package:ack/ack.dart';
import 'package:ack_json_schema_builder/ack_json_schema_builder.dart';
import 'package:json_schema_builder/json_schema_builder.dart' as jsb;
final AckSchema<Object, Object> schema = jsb.Schema.object(
properties: {'name': jsb.Schema.string()},
required: ['name'],
).toAckSchema();
final value = schema.parse({'name': 'Ada'});
final exported = schema.toJsonSchemaBuilder();
Imports use the Ack Draft 2020-12 and Draft-07 importer. Supply cross-document references through documents: <Uri, jsb.Schema>{...} and an optional baseUri. The importer makes no network requests. It rejects unsupported features with JsonSchemaImportException, and the diagnostics identify the keyword and location. Pass unicodeNormalizer: nfkc from package:unorm_dart to normalize international hostname labels in format assertions. Pass assertFormats: true to assert built-in string and numeric formats.
For the supported keywords and the reference and diagnostic behavior, see the JSON Schema Integration guide.
Schema mapping #
The adapter renders AckSchema.toJsonSchema() and wraps the map with Schema.fromMap():
AckSchema
-> AckSchemaModel
-> JSON Schema map
-> json_schema_builder Schema.fromMap()
Defaults, const values, extension keywords, transformed-schema metadata, composition and discriminated-union branches follow AckSchema.toSchemaModel().toJsonSchema().
| Ack type | json_schema_builder type | Notes |
|---|---|---|
Ack.string() |
Schema.string() |
minLength and maxLength |
Ack.integer() |
Schema.integer() |
|
Ack.double() |
Schema.number() |
|
Ack.boolean() |
Schema.boolean() |
|
Ack.object({...}) |
Schema.object() |
|
Ack.list(...) |
Schema.list() |
|
Ack.enumString([...]) |
Schema.string() with enumValues |
|
Ack.anyOf([...]) |
Schema.combined(anyOf: ...) |
|
Ack.any() |
Schema.combined(anyOf: ...) |
Expands to a union of all types |
| Ack constraint | json_schema_builder | Notes |
|---|---|---|
.minLength() / .maxLength() |
minLength / maxLength |
Strings |
.minItems() / .maxItems() |
minItems / maxItems |
Arrays |
.min() / .max() |
minimum / maximum |
Numeric bounds |
.email() / .uuid() / .uri() |
format |
Format hints |
.optional() |
Excluded from required |
Optional fields |
.describe() |
description |
Descriptions |
.withDefault() |
default |
JSON-compatible defaults |
Ack.literal(...) |
const |
Literal values |
.unique() |
uniqueItems |
Array uniqueness |
Limitations #
- Conversion succeeds and omits custom refinements from the generated JSON Schema. Validate data with the original Ack schema when those rules matter.
- A downstream validator can ignore a JSON Schema keyword. Validate with Ack after parsing.
- Native Ack schemas export as Draft-7. Imported Draft 2020-12 resources are preserved by default. To lower an imported schema to Draft-07
definitions, callschema.toJsonSchemaBuilderDraft7(). Ack.date()uses the local value fromDateTime(year, month, day). On a day with a time zone transition, that value can be later than midnight. See Codecs. UseDateTime.utc(...)withAck.datetime(), which requires UTC.
final schema = Ack.string().refine((s) => s.startsWith('ACK_'));
final jsonSchema = schema.toJsonSchemaBuilder();
final result = schema.safeParse(data);
final schema = Ack.date().min(DateTime(2026));
final jsonSchema = schema.toJsonSchemaBuilder(); // Includes format: date.
Learn more #
- Ecosystem Integrations
- Contributing
- Part of the Ack monorepo. See LICENSE.