|
| 1 | +# Schemas |
| 2 | + |
| 3 | +The library can **generate schema definitions** for all converters that support it and can **also regenerate** |
| 4 | +'materialize' **structures** from those **schema definitions**. The schema definition format is JSON-based and a |
| 5 | +subset of **JSON Schema** with some custom framework-specific extensions, primarily used for styling in the Flutter |
| 6 | +integration. |
| 7 | + |
| 8 | +## API Examples |
| 9 | + |
| 10 | +```{ .dart title="Export a Schema" } |
| 11 | +// This config fully expands all references for use in other tools |
| 12 | +final schemaConfig = SchemaConfig(useReferences: false); |
| 13 | +
|
| 14 | +final schema = dogs.describe<User>(config: schemaConfig); |
| 15 | +final exported = schema.toJson(); |
| 16 | +``` |
| 17 | + |
| 18 | +```{ .dart title="Define a schema using the dsl" } |
| 19 | +final schema = object({ |
| 20 | + "name": string(), |
| 21 | + "age": integer().min(0), |
| 22 | +}); |
| 23 | +``` |
| 24 | + |
| 25 | +```{ .dart title="Load a converter from a JSON schema" } |
| 26 | +final schema = parseSchema([...]); |
| 27 | +final converter = dogs.materialize(schema); |
| 28 | +// Use the proxy methods of the converter |
| 29 | +``` |
| 30 | + |
| 31 | +```{ .dart title="Import a json schema into the global dogs instance" } |
| 32 | +final schema = parseSchema([...]); |
| 33 | +final typeReference = dogs.importSchema(schema); |
| 34 | +// Use the returned type reference like a type tree |
| 35 | +``` |
| 36 | + |
| 37 | +## Supported JSON Schema Properties |
| 38 | + |
| 39 | +- `type: string | number | integer | boolean | null` |
| 40 | +- `enum: [string]` |
| 41 | +- `default: any` |
| 42 | + |
| 43 | +List/Array specific: |
| 44 | + |
| 45 | +- `minItems: int` |
| 46 | +- `maxItems: int` |
| 47 | +- `uniqueItems: bool` |
| 48 | + |
| 49 | +Object specific: |
| 50 | +`required: [String]` (Only written, for reading only unions with `null` make fields optional) |
| 51 | + |
| 52 | +Number specific: |
| 53 | + |
| 54 | +- `minimum: number` |
| 55 | +- `maximum: number` |
| 56 | +- `exclusiveMinimum: number` |
| 57 | +- `exclusiveMaximum: number` |
| 58 | + |
| 59 | +String specific: |
| 60 | + |
| 61 | +- `minLength: int` |
| 62 | +- `maxLength: int` |
| 63 | +- `pattern: string` |
0 commit comments