ack 1.2.0 copy "ack: ^1.2.0" to clipboard
ack: ^1.2.0 copied to clipboard

Schema validation and bidirectional codecs for Dart and Flutter

example/example.dart

// Validates a map against a schema, then exports the same schema as JSON
// Schema Draft-7. Run it with `dart run example/example.dart`.
import 'dart:convert';

import 'package:ack/ack.dart';

void main() {
  final userSchema = Ack.object({
    'name': Ack.string().minLength(2).maxLength(50),
    'email': Ack.string().email(),
    'age': Ack.integer().min(0).max(120).optional(),
  });

  final result = userSchema.safeParse({
    'name': 'Ada Lovelace',
    'email': 'ada@example.com',
    'age': 36,
  });

  if (result.isOk) {
    print('Valid user: ${result.getOrThrow()}');
  } else {
    print('Validation failed: ${result.getError()}');
  }

  // A missing required field fails, and the error names the field.
  final incomplete = userSchema.safeParse({'name': 'Ada'});
  print('Missing email: ${incomplete.getError()}');

  // The same schema exports as JSON Schema Draft-7.
  print(const JsonEncoder.withIndent('  ').convert(userSchema.toJsonSchema()));
}
57
likes
160
points
1.33k
downloads

Documentation

API reference

Publisher

verified publisherleoafarias.com

Weekly Downloads

Schema validation and bidirectional codecs for Dart and Flutter

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#validation #schema #json-schema #codec #serialization

License

BSD-3-Clause (license)

Dependencies

collection, meta

More

Packages that depend on ack