toJsonDecoded function

IValidator toJsonDecoded(
  1. IValidator child, {
  2. String? message,
})

Coerces a JSON string into its decoded form (Map/List). Leaves existing Map/List untouched.

Implementation

IValidator toJsonDecoded(IValidator child, {String? message}) {
  return Validator((value) {
    final mapped = switch (value) {
      final Map m => m,
      final List l => l,
      final String s => _tryJsonDecode(s),
      _ => null,
    };

    if (mapped is! Map && mapped is! List) {
      return Expectation(
        message: message ?? 'a JSON decodable value (Map/List)',
        value: value,
      ).toInvalidResult();
    }

    return child.validate(mapped);
  });
}