Loading...
Loading...
Instructions and guidelines for using the Schemantic library for type-safe data classes and schemas in Dart.
npx skill4agent add genkit-ai/genkit-dart schemanticgenkit-dartschemantic@Schematic()$abstract class $Userdart run build_runner build.g.dartimport 'package:schemantic/schemantic.dart';
part 'my_file.g.dart'; // Must match the filename
()
abstract class $MyObj {
String get name;
$MySubObj get subObj;
}
()
abstract class $MySubObj {
String get foo;
}MyObj$MyObj.fromJson// Creating an instance
final obj = MyObj(name: 'test', subObj: MySubObj(foo: 'bar'));
// Serializing to JSON
print(obj.toJson());
// Parsing from JSON
final parsed = MyObj.fromJson({'name': 'test', 'subObj': {'foo': 'bar'}});$schemaSchemanticType<T>// Access JSON schema
final schema = MyObj.$schema.jsonSchema;
print(schema.toJson());
// Validate arbitrary JSON at runtime
final validationErrors = await schema.validate({'invalid': 'data'});final ageSchema = intSchema(description: 'Age in years', minimum: 0);
final nameSchema = stringSchema(minLength: 2);
final nothingSchema = voidSchema();
final anySchema = dynamicSchema();
final userSchema = mapSchema(stringSchema(), intSchema()); // Map<String, int>
final tagsSchema = listSchema(stringSchema()); // List<String>@AnyOf()
abstract class $Poly {
([int, String, $MyObj])
Object? get id;
}PolyIdfinal poly1 = Poly(id: PolyId.int(123));
final poly2 = Poly(id: PolyId.string('abc'));()
abstract class $User {
(
name: 'years_old', // Change JSON key
description: 'Age of the user',
minimum: 0,
defaultValue: 18,
)
int? get age;
(
minLength: 2,
enumValues: ['user', 'admin'],
)
String get role;
}useRefs: true()
abstract class $Node {
String get id;
List<$Node>? get children;
}Node.$schema.jsonSchema(useRefs: true)$ref