json_guard library

JSON Guard is a library for validating and transforming JSON data in Dart.

It provides a schema-based approach to define how JSON data should be validated, transformed, and constructed into Dart objects. Key features include:

  • Type validation and conversion
  • No code generation required
  • Field presence validation
  • Value constraints (min/max, regex patterns, allowed values)
  • Detailed error reporting with JSON paths
  • Support for nested objects and lists

Classes

Field
Factory for creating fields with various validation rules.
FieldInfo<T, J>
Defines metadata for a field in a schema, including how to extract and convert its value from JSON.
Option<T>
Represents an optional value that may or may not be present.
Schema<T>
Defines a schema for validating and constructing objects from JSON data.
Union<T1, T2>
A union type that can hold a value of either type T1 or type T2.

Typedefs

Converter<T, J> = T Function(J value, JsonPath path, FieldInfo field)
Function type for converting a JSON value of type J to a Dart value of type T.

Exceptions / Errors

ArgumentErrorValidationException
Exception thrown when an ArgumentError is raised during object construction.
FieldMissingException
Exception thrown when a required field is missing from the JSON.
JsonGuardException
Base exception class for all exceptions thrown by the JsonGuard library.
JsonTypeException
Exception thrown when JSON data is not of the expected type.
ValidationException
Base class for all validation exceptions in the JsonGuard library.
ValueValidationException
Exception thrown when a value fails validation.