parseErrors static method
Parses all validation errors
Implementation
static List<ValidationError> parseErrors(String message) {
try {
final decoded = json.decode(message);
final Map<String, dynamic>? errors =
decoded["errors"] as Map<String, dynamic>?;
if (errors == null) {
return const [];
}
final mapped = [
...errors.entries.expand(
(entry) {
final errorPath =
JsonPath.parsed(entry.key, relativeTo: inputSchemaPath);
return <ValidationError>[
if (entry.value is! Iterable)
ValidationError(
path: errorPath,
message: "${entry.value}",
keyword: "error.unknown",
code: "unknown",
arguments: [],
),
if (entry.value is Iterable)
...entry.value.map(
(final error) {
return error is Map<String, dynamic>
? ValidationError(
path: errorPath,
message: error["message"]?.toString(),
keyword: error["keyword"]?.toString(),
code: error["code"]?.toString(),
arguments: error["arguments"] as List?,
)
: ValidationError.ofString(
JsonPath.of(error), "$error");
},
),
];
},
),
];
return mapped;
} catch (e, stack) {
_log.info("Unable to parse validation errors: $e", e, stack);
return const [];
}
}