Skip to main content

schema_engine 1.0.0 copy "schema_engine: ^1.0.0" to clipboard
schema_engine: ^1.0.0 copied to clipboard

A portable JSON Schema compiler, validator, parser, and schema builder for Dart.

example/schema_engine_example.dart

import 'package:schema_engine/schema_engine.dart' as schema;

/// Builds a schema and validates JSON with each diagnostic mode.
void main() {
  final document = schema.object({
    'name': schema.string().min(1),
    'age': schema.integer().nonnegative().optional(),
  }).toJsonSchema();
  print(document.toJsonString(indent: '  '));

  final validator = document.compile();
  final instance = schema.decodeJson('{"name":"Example","age":30}');
  print('Valid instance: ${validator.isValid(instance)}');

  final invalid = schema.decodeJson('{"name":"","age":-1}');
  final firstError = validator.validate(invalid);
  print('First error: ${firstError?.message}');
  for (final error in validator.errors(invalid)) {
    print('${error.instancePath}: ${error.message}');
  }
}
0
likes
160
points
233
downloads

Documentation

API reference

Publisher

verified publishermikelpastor.com

Weekly Downloads

A portable JSON Schema compiler, validator, parser, and schema builder for Dart.

Repository (GitHub)
View/report issues

Topics

#json #json-schema #validation #code-generation

License

MIT (license)

More

Packages that depend on schema_engine