fillAttributes method

Map<String, Object?> fillAttributes(
  1. Map<String, Object?> payload, {
  2. bool strict = true,
  3. ValueCodecRegistry? registry,
})
inherited

Fills attributes from payload, respecting fillable/guarded metadata.

Returns a map containing the values that were accepted. When strict is true, guarded properties produce a MassAssignmentException.

Implementation

Map<String, Object?> fillAttributes(
  Map<String, Object?> payload, {
  bool strict = true,
  ValueCodecRegistry? registry,
}) {
  final inspector = _attributeInspector();
  final codecs = registry ?? ValueCodecRegistry.instance;
  final filled = <String, Object?>{};
  final discarded = <String>[];
  for (final entry in payload.entries) {
    if (_isUngarded || inspector.isFillable(entry.key)) {
      final decoded = _decodeAttribute(
        column: entry.key,
        value: entry.value,
        registry: codecs,
        definition: _definition,
        inspector: inspector,
      );
      setAttribute(entry.key, decoded);
      filled[entry.key] = decoded;
    } else {
      discarded.add(entry.key);
    }
  }
  if (strict && discarded.isNotEmpty) {
    throw MassAssignmentException(
      'Mass assignment rejected for attributes: ${discarded.join(', ')}',
    );
  }
  return filled;
}