public KzFieldsFormValidationResult ValidateForm(IDictionary <int, object> values) { var fields = values.Map(x => this.GetFieldValue(x.Key, x.Value) .Map(value => new KzField(x.Key, value)) .MapFail(err => new KzFieldValidationError(x.Key, List(err))) ) .Apply(toList); var validFields = fields.SelectMany(f => f.SuccessAsEnumerable()); var failedFields = fields.SelectMany(f => f.FailAsEnumerable()); var fieldSet = new KzFieldSet( toHashSet(validFields)); var failedValidationRules = this.ValidationRules .Map(x => ( x.FieldNumber, errors: toList(x.Rules.SelectMany(rule => rule.Validate(fieldSet, x.FieldNumber))) )) .Filter(x => x.errors.Any()) .Map(x => new KzFieldValidationError(x.FieldNumber, x.errors)); return(new KzFieldsFormValidationResult( fieldSet, new KzFieldFormValidationError(toList(failedFields) + failedValidationRules))); }
public Lst <KzFieldError> Validate(KzFieldSet fieldSet, int fieldNumber) { if (this.required) { return(fieldSet.GetValue(fieldNumber) .Match(some => Lst <KzFieldError> .Empty, () => Prelude.List(KzFieldError.Required))); } if (this.lessThan != null) { var comparedField = fieldSet.GetValue(this.lessThan.Value); var currentField = fieldSet.GetValue(fieldNumber); var comparedValue = comparedField .Map(x => x.GetDecimalValue()) .IfNone(0); return(toList(currentField .Map(value => value.GetDecimalValue()) .Match( value => Math.Abs(value) >= Math.Abs(comparedValue) && value != 0m ? Some(KzFieldError.MustBeLessThanAnotherField(this.lessThan.Value)) : None, () => comparedField.Match( Some: _ => Some(KzFieldError.Required), None: () => None )))); } if (this.custom != null) { return(this.custom(fieldSet, fieldNumber)); } throw new InvalidOperationException(); }
public KzFieldsFormValidationResult(KzFieldSet validFieldSet, KzFieldFormValidationError validationError) { this.ValidFieldSet = validFieldSet ?? throw new ArgumentNullException(nameof(validFieldSet)); this.ValidationError = validationError ?? throw new ArgumentNullException(nameof(validationError)); }