validateMap method

ValidationError? validateMap(
  1. Map<String, Object?> values
)

Implementation

ValidationError? validateMap(Map<String, Object?> values) {
  for (final column in this) {
    var value = values[column.name];

    final dataType = switch (column) {
      PrimaryKeyColumn _ => DataType.integer,
      JsonColumn _ => DataType.json,
      ReferenceColumn col => col.type,
      TextColumn _ => DataType.text,
      GenericColumn column => column.dataType,
      PrimaryKeyCuidColumn _ => DataType.text,
      PrimaryKeyUuidColumn _ => DataType.text,
    };

    final nullable = switch (column) {
      JsonColumn column => column.nullable,
      GenericColumn column => column.nullable,
      ReferenceColumn column => column.nullable,
      TextColumn column => column.nullable,
      PrimaryKeyColumn _ => true,
      PrimaryKeyCuidColumn _ => true,
      PrimaryKeyUuidColumn _ => true,
    };

    if (value == null && !nullable) return NullableError(keyName: column.name, column: column);

    switch (dataType) {
      case DataType.bytes:
        if (value is! Uint8List?) return InvalidTypeError(column.name, expected: Uint8List, actual: value.runtimeType);
        break;
      case DataType.boolean:
        if (value is! bool?) return InvalidTypeError(column.name, expected: bool, actual: value.runtimeType);
        break;
      case DataType.real:
        if (value is! double?) return InvalidTypeError(column.name, expected: double, actual: value.runtimeType);
        break;
      case DataType.integer:
        if (value is! int?) return InvalidTypeError(column.name, expected: int, actual: value.runtimeType);
        break;
      case DataType.text:
        if (value is! String?) return InvalidTypeError(column.name, expected: String, actual: value.runtimeType);
        break;
      case DataType.json:
        if (value is! Map?) return InvalidTypeError(column.name, expected: Map, actual: value.runtimeType);
        break;
      case DataType.dateTime:
        // if (value is! DateTime?) return InvalidTypeError(column.name, expected: DateTime, actual: value.runtimeType);
        if (value is! int?) return InvalidTypeError(column.name, expected: int, actual: value.runtimeType);
        break;
      case DataType.date:
        if (value is! DateTime?) return InvalidTypeError(column.name, expected: DateTime, actual: value.runtimeType);
        break;
    }
  }

  return null;
}