validate method

List<SchemaViolation> validate(
  1. Map<String, dynamic> map
)

Checks a raw map against the declaration: missing non-nullable keys and undecodable values. For tests and debug tooling.

Implementation

List<SchemaViolation> validate(Map<String, dynamic> map) {
  final violations = <SchemaViolation>[];
  for (final field in fields) {
    final present = map.containsKey(field.name);
    if (!present || map[field.name] == null) {
      if (!field.isNullable) {
        violations.add((field: field.name, message: present ? 'null for non-nullable ${field.valueType}' : 'missing key'));
      }
      continue;
    }
    try {
      field.decode(map[field.name]);
    } on Object catch (error) {
      violations.add((field: field.name, message: error.toString()));
    }
  }
  return violations;
}