validate method

JarResult validate(
  1. T? value, [
  2. Map<String, dynamic>? allValues
])

Implementation

JarResult validate(T? value, [Map<String, dynamic>? allValues]) {
  _allValues = allValues;
  var transformedValue = value;

  for (final transformer in transformers) {
    transformedValue = transformer(transformedValue);
  }

  if (_dependsOn != null && allValues != null) {
    final dependentValue = allValues[_dependsOn];
    final conditions = _conditions[_dependsOn];

    if (conditions != null && conditions.containsKey(dependentValue)) {
      final conditionFn = conditions[dependentValue];
      if (conditionFn != null) {
        final conditionSchema = conditionFn(self);
        final validatorsToApply =
            List<JarValidator<T>>.from(conditionSchema.validators);

        for (final validator in validatorsToApply) {
          final error = validator(transformedValue);
          if (error != null) return JarResult.error(error);
        }
      }
    }
  }

  for (final validator in validators) {
    final error = validator(transformedValue);
    if (error != null) return JarResult.error(error);
  }

  return JarResult.success();
}