Skip to main content

zema_standard_schema 0.2.0 copy "zema_standard_schema: ^0.2.0" to clipboard
zema_standard_schema: ^0.2.0 copied to clipboard

Standard Schema adapter for Zema. Exposes any Zema schema through the standard_schema contract so tools can validate without depending on Zema.

example/main.dart

// ignore_for_file: avoid_print

import 'package:standard_schema/standard_schema.dart';
import 'package:zema/zema.dart';
import 'package:zema_standard_schema/zema_standard_schema.dart';

/// A consumer that only knows about Standard Schema, not about Zema.
///
/// CLI frameworks, form libraries or config loaders can accept any
/// `StandardSchemaV1` and stay independent of the validation library.
void runCommand(StandardSchemaV1<Object?, Object?> schema, Object? input) {
  final result = schema.standard.validate(input);

  if (result is StandardFailure) {
    for (final issue in result.issues) {
      print('${issue.path.join('.')}: ${issue.message}');
    }
    return;
  }

  print('OK: $input');
}

void main() {
  final userSchema = z.object({
    'name': z.string().min(2),
    'email': z.string().email(),
    'age': z.integer().gte(0),
  });

  // Expose the Zema schema through the Standard Schema contract.
  final standard = userSchema.asStandard;

  runCommand(standard, {'name': 'Ada', 'email': 'ada@example.com', 'age': 36});
  runCommand(standard, {'name': 'A', 'email': 'nope', 'age': -1});
}
1
likes
160
points
163
downloads

Documentation

API reference

Publisher

verified publishermeragix.dev

Weekly Downloads

Standard Schema adapter for Zema. Exposes any Zema schema through the standard_schema contract so tools can validate without depending on Zema.

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#validation #schema #standard-schema #interoperability

License

MIT (license)

Dependencies

standard_schema, zema

More

Packages that depend on zema_standard_schema