From 1ffca12ddce00827ef86afc76c6b84c2671a10dc Mon Sep 17 00:00:00 2001 From: Francesco Vallone Date: Mon, 18 Mar 2024 23:12:03 +0100 Subject: [PATCH] refactor: change dependency paramters name --- bin/acanthis.dart | 6 +++--- lib/src/types/map.dart | 36 ++++++++++++++++++------------------ test/types/map_test.dart | 12 ++++++------ 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/bin/acanthis.dart b/bin/acanthis.dart index d847efd..8920ed5 100644 --- a/bin/acanthis.dart +++ b/bin/acanthis.dart @@ -17,9 +17,9 @@ void main(List arguments) { }) .passthrough() .addFieldDependency( - dependency: 'name', - dependFrom: 'attributes.age', - condition: (age, name) { + dependent: 'name', + dependendsOn: 'attributes.age', + dependency: (age, name) { return name.length < age; }); diff --git a/lib/src/types/map.dart b/lib/src/types/map.dart index ebbbbcb..f3201a8 100644 --- a/lib/src/types/map.dart +++ b/lib/src/types/map.dart @@ -38,12 +38,12 @@ class AcanthisMap extends AcanthisType> { parsed[obj.key] = _fields[obj.key]!.parse(obj.value).value; } for (var dependency in _dependencies) { - final dependFrom = _keyQuery(dependency.dependFrom, value); - final dependTo = _keyQuery(dependency.dependency, value); + final dependFrom = _keyQuery(dependency.dependendsOn, value); + final dependTo = _keyQuery(dependency.dependent, value); if (dependFrom != null && dependTo != null) { - if (!dependency.condition(dependFrom, dependTo)) { + if (!dependency.dependency(dependFrom, dependTo)) { throw ValidationError( - 'Dependency not met: ${dependency.dependFrom}->${dependency.dependency}'); + 'Dependency not met: ${dependency.dependendsOn}->${dependency.dependent}'); } } else { throw ValidationError( @@ -82,15 +82,15 @@ class AcanthisMap extends AcanthisType> { } final result = super.tryParse(parsed); for (var dependency in _dependencies) { - final dependFrom = _keyQuery(dependency.dependFrom, value); - final dependTo = _keyQuery(dependency.dependency, value); + final dependFrom = _keyQuery(dependency.dependendsOn, value); + final dependTo = _keyQuery(dependency.dependent, value); if (dependFrom != null && dependTo != null) { - if (!dependency.condition(dependFrom, dependTo)) { - errors[dependency.dependency] = {'dependency': 'Dependency not met'}; + if (!dependency.dependency(dependFrom, dependTo)) { + errors[dependency.dependent] = {'dependency': 'Dependency not met'}; } } else { - errors[dependency.dependency] = { - 'dependency[${dependency.dependFrom}->${dependency.dependency}]': + errors[dependency.dependent] = { + 'dependency[${dependency.dependendsOn}->${dependency.dependent}]': 'The dependency or dependFrom field does not exist in the map' }; } @@ -138,11 +138,11 @@ class AcanthisMap extends AcanthisType> { /// Add a field dependency to the map to validate the map based on the [condition] /// [dependency] is the field that depends on [dependFrom] AcanthisMap addFieldDependency({ - required String dependency, - required String dependFrom, - required bool Function(dynamic, dynamic) condition, + required String dependent, + required String dependendsOn, + required bool Function(dynamic, dynamic) dependency, }) { - _dependencies.add(_Dependency(dependency, dependFrom, condition)); + _dependencies.add(_Dependency(dependent, dependendsOn, dependency)); return this; } @@ -218,9 +218,9 @@ AcanthisMap object(Map fields) => AcanthisMap( ); class _Dependency { - final String dependency; - final String dependFrom; - final bool Function(dynamic, dynamic) condition; + final String dependent; + final String dependendsOn; + final bool Function(dynamic, dynamic) dependency; - _Dependency(this.dependency, this.dependFrom, this.condition); + _Dependency(this.dependent, this.dependendsOn, this.dependency); } diff --git a/test/types/map_test.dart b/test/types/map_test.dart index 44d86b5..249ca36 100644 --- a/test/types/map_test.dart +++ b/test/types/map_test.dart @@ -322,9 +322,9 @@ void main() { }) .passthrough() .addFieldDependency( - dependency: 'name', - dependFrom: 'attributes.age', - condition: (age, name) { + dependent: 'name', + dependendsOn: 'attributes.age', + dependency: (age, name) { return name.length > age; }); @@ -385,9 +385,9 @@ void main() { }) .passthrough() .addFieldDependency( - dependency: 'name', - dependFrom: 'attributes.age', - condition: (age, name) { + dependent: 'name', + dependendsOn: 'attributes.age', + dependency: (age, name) { return name.length < age; });