Skip to content

Export compatibility ​

Every native export helper uses the same strict audit. A contract is emitted only when the schema's supported behavior can be represented faithfully for JSON values. Unsupported behavior throws rather than disappearing from the contract.

Export helpers ​

APIResultDefault mode
toJsonSchema(mode: ...)JSON Schema 2020-12 mapInput
exportJsonSchema(mode: ...)Same JSON Schema mapExplicit mode required
toPrettyJsonSchema(indent: 2, mode: ...)Formatted JSON stringInput
toOpenApiSchema(mode: ...)OpenAPI 3.1 Schema ObjectExplicit mode required
exportOpenApiSchema(mode: ...)Same OpenAPI Schema ObjectExplicit mode required

Supported behavior ​

Every child, constraint, default, and annotation must pass the audit.

Native behaviorExport
Unconstrained strings, numbers, booleans, scalar literalsSupported when scalar equality is representable
Objects, lists, tuples, nullable values, string-key recordsSupported when their children and checks are supported
Optional fields, defaults, PATCH, unknown-key policiesSupported; input/output modes can differ
Numeric bounds, scalar equality/enumeration, boolean checksSupported with representable finite constraints
Empty/nonempty string constraintsSupported
List lengths, scalar membership, uniquenessSupported within the audited equality subset
Object property countsSupported
Plain unions, scalar templates, stable recursive objectsSupported subset; recursion uses local references
Field-described instancesDeclared JSON representation; getters do not run
General string lengths, patterns, and formatsRejected
Dart integer() / doubleType() restrictionsRejected
Coercion, transforms, pipes, custom/async checks, guarded unions, field rulesRejected
Native dates and opaque domain schemasRejected

Why a check can be rejected ​

Native string lengths count UTF-16 code units; JSON Schema lengths count Unicode code points. Dart int and double are distinct runtime representations; JSON numeric types cannot enforce that distinction. Arbitrary callbacks cannot be translated into declarative JSON constraints.

Use an imported JSON contract when JSON semantics are the requirement. Keep additional application checks explicit when a published contract describes only a boundary subset.

Input versus output ​

Input mode describes accepted JSON, including missing/null values handled by valid defaults. Output mode describes successful parsed values, including the effects of stripping and defaults. Neither describes invalid recovery. An external validator may validate input without applying Acanthis's transformations to it.

Handle an export failure ​

dart
final schema = object({'email': string().email()});
try {
  schema.toJsonSchema();
} on AcanthisSchemaExportException catch (error) {
  print(error.path); // Points to the unsupported part of the schema.
  print(error.reason);
}

Export does not execute arbitrary validation, conversion, getter, or constructor callbacks. Supported metadata is serialized to fresh JSON containers; cycles and unsafe annotation values fail explicitly.

Built by Avesbox · Made for Dart & Flutter