From 7f73235871cc159984598600bdb6e81d1fd9a62b Mon Sep 17 00:00:00 2001 From: Chima Precious Date: Thu, 9 May 2024 21:08:52 +0000 Subject: [PATCH] add type mismatch check in binding --- lib/src/builder/utils.dart | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/src/builder/utils.dart b/lib/src/builder/utils.dart index 30c46c21..8d9a4cc0 100644 --- a/lib/src/builder/utils.dart +++ b/lib/src/builder/utils.dart @@ -252,18 +252,20 @@ final class ParsedEntityClass { /// Check the field we're binding onto. If provided, validate that if exists /// if not, use the related class primary key - Symbol? fieldToBind = field.reader.peek('on')?.symbolValue; - - if (fieldToBind != null) { - final fields = parsedRelatedClass.allFields.map((e) => Symbol(e.name)); - if (!fields.contains(fieldToBind)) { - throw InvalidGenerationSource( - 'Field $fieldToBind used in Binding does not exist on ${parsedRelatedClass.className} Entity', - element: field.field, - ); - } - } else { - fieldToBind = Symbol(parsedRelatedClass.primaryKey.field.name); + final fieldToBind = field.reader.peek('on')?.symbolValue ?? Symbol(parsedRelatedClass.primaryKey.field.name); + final referencedField = parsedRelatedClass.allFields.firstWhereOrNull((e) => Symbol(e.name) == fieldToBind); + if (referencedField == null) { + throw InvalidGenerationSource( + 'Field $fieldToBind used in Binding does not exist on ${parsedRelatedClass.className} Entity', + element: field.field, + ); + } + + if (referencedField.type != field.field.type) { + throw InvalidGenerationSource( + 'Type-mismatch between fields $className.${field.field.name}(${field.field.type}) and ${parsedRelatedClass.className}.${referencedField.name}(${referencedField.type})', + element: field.field, + ); } bindings[Symbol(field.field.name)] = (entity: parsedRelatedClass, field: fieldToBind, reader: field.reader);