processValid method

List<ConstrainViolation> processValid({
  1. required List<Valid> valid,
  2. required Object? fieldValue,
  3. required String fieldName,
})

Implementation

List<ConstrainViolation> processValid({
  required List<Valid> valid,
  required Object? fieldValue,
  required String fieldName,
}) {
  List<ConstrainViolation> violations = [];
  if (valid.isNotEmpty) {
    for (var element in valid) {
      for (var singleValidation in element.validations) {
        ConstraintValidatorContext cvc = ConstraintValidatorContext(
          parent: element,
        );
        bool valid = singleValidation(fieldValue, cvc);
        if (!valid) {
          if (cvc.hasAnyViolations()) {
            violations.addAll(
              cvc.templateViolations
                  .map(
                    (message) => ConstrainViolation(
                      value: fieldValue,
                      fieldName: fieldName,
                      message: message,
                    ),
                  )
                  .toList(),
            );
            if (cvc.concatParentName) {
              violations.addAll(
                cvc.constrainViolations
                    .map(
                      (e) => ConstrainViolation(
                        value: e.value,
                        fieldName: '$fieldName.${e.fieldName}',
                        message: e.message,
                      ),
                    )
                    .toList(),
              );
            } else {
              violations.addAll(cvc.constrainViolations);
            }
          } else if (element is ValidMessage) {
            violations.add(
              ConstrainViolation(
                value: fieldValue,
                fieldName: fieldName,
                message: (element as ValidMessage).defaultMessage,
              ),
            );
          } else {
            violations.add(
              ConstrainViolation(
                value: fieldValue,
                fieldName: fieldName,
                message: 'Error validating field',
              ),
            );
          }
        }
      }
    }
  }
  return violations;
}