Beispiel #1
0
        public void Form_record_is_null_should_throw()
        {
            var         context = new RecordValidationContext(TestData.FormDefinition.Key, null);
            Func <Task> func    = async() => await validator.ValidateAsync(context);

            func.Should().Throw <ArgumentNullException>();
        }
        protected async virtual Task ValidateIntFieldValueAsync(RecordValidationContext context)
        {
            var records = context.FormRecord.Values.Where(_ => _ is IntFieldValue).Select(_ => _ as IntFieldValue);

            foreach (var record in records)
            {
                var fieldDefinition = record.FieldDefinition as IntField;

                if (fieldDefinition.IsRequired && !record.Value.HasValue)
                {
                    context.AddRecordError(record, "required validation failure");
                }

                if (fieldDefinition.IsUnique && record.Value.HasValue)
                {
                    var fieldRecordValidationContext = new UniqueValueValidationContext(context.FormId, record);
                    await _fieldRecordUniqueValidator.ValidateAsync(fieldRecordValidationContext);

                    if (!fieldRecordValidationContext.IsValid)
                    {
                        context.AddRecordError(record, fieldRecordValidationContext.ErrorMessage);
                    }
                }

                if (record.Value.IsOutOfRange(fieldDefinition.Min, fieldDefinition.Max))
                {
                    context.AddRecordError(record, "range validation failure");
                }
            }
        }
        protected async virtual Task ValidateTextboxFieldValueAsync(RecordValidationContext context)
        {
            var records = context.FormRecord.Values.Where(_ => _ is TextboxFieldValue).Select(_ => _ as TextboxFieldValue);

            foreach (var record in records)
            {
                var fieldDefinition = record.FieldDefinition as TextboxField;

                if (fieldDefinition.IsRequired && record.Value.IsMissing())
                {
                    context.AddRecordError(record, "required validation failure");
                }

                if (fieldDefinition.IsUnique && record.Value.IsPresent())
                {
                    var fieldRecordValidationContext = new UniqueValueValidationContext(context.FormId, record);
                    await _fieldRecordUniqueValidator.ValidateAsync(fieldRecordValidationContext);

                    if (!fieldRecordValidationContext.IsValid)
                    {
                        context.AddRecordError(record, fieldRecordValidationContext.ErrorMessage);
                    }
                }

                if (record.Value.IsTooLong(fieldDefinition.MaxLength))
                {
                    context.AddRecordError(record, "max length validation failure");
                }
            }
        }
Beispiel #4
0
        public void Context_is_null_should_throw()
        {
            RecordValidationContext context = null;
            Func <Task>             func    = async() => await validator.ValidateAsync(context);

            func.Should().Throw <ArgumentNullException>();
        }
Beispiel #5
0
        public async Task Validate_should_success()
        {
            var context = new RecordValidationContext(TestData.FormDefinition.Key, TestData.FormRecord);
            await validator.ValidateAsync(context);

            context.IsValid.Should().BeTrue();
        }
Beispiel #6
0
        public void Any_field_definition_is_null_should_throw()
        {
            var formRecord = TestData.FormRecord;

            formRecord.Values.First().FieldDefinition = null;
            var         context = new RecordValidationContext(TestData.FormDefinition.Key, formRecord);
            Func <Task> func    = async() => await validator.ValidateAsync(context);

            func.Should().Throw <ArgumentNullException>();
        }
Beispiel #7
0
        public void Field_values_is_empty_should_throw()
        {
            var formRecord = TestData.FormRecord;

            formRecord.Values = new List <FieldValue>();
            var         context = new RecordValidationContext(TestData.FormDefinition.Key, formRecord);
            Func <Task> func    = async() => await validator.ValidateAsync(context);

            func.Should().Throw <ArgumentNullException>();
        }
Beispiel #8
0
        public void Form_definition_id_is_missiing_should_throw()
        {
            var formRecord = TestData.FormRecord;

            formRecord.FormKey = null;
            var         context = new RecordValidationContext(TestData.FormDefinition.Key, formRecord);
            Func <Task> func    = async() => await validator.ValidateAsync(context);

            func.Should().Throw <ArgumentNullException>();
        }
Beispiel #9
0
        public async Task Uploader_field_value_is_required_and_empty_should_be_invalid()
        {
            var formRecord = TestData.FormRecord;

            formRecord.Values.First(_ => _ is UploaderFieldValue).FieldDefinition.IsRequired = true;

            (formRecord.Values.First(_ => _ is UploaderFieldValue) as UploaderFieldValue).Value = new List <File>();

            var context = new RecordValidationContext(TestData.FormDefinition.Key, formRecord);
            await validator.ValidateAsync(context);

            context.IsValid.Should().BeFalse();
        }
Beispiel #10
0
        public async Task Slider_field_value_greater_than_max_should_be_invalid()
        {
            var formRecord = TestData.FormRecord;

            (formRecord.Values.First(_ => _ is IntFieldValue).FieldDefinition as IntField).Max = 1;

            (formRecord.Values.First(_ => _ is IntFieldValue) as IntFieldValue).Value = 2;

            var context = new RecordValidationContext(TestData.FormDefinition.Key, formRecord);
            await validator.ValidateAsync(context);

            context.IsValid.Should().BeFalse();
        }
Beispiel #11
0
        public async Task Colorpicker_field_value_is_required_and_null_should_be_invalid()
        {
            var formRecord = TestData.FormRecord;

            formRecord.Values.First(_ => _ is ColorPickerFieldValue).FieldDefinition.IsRequired = true;

            (formRecord.Values.First(_ => _ is ColorPickerFieldValue) as ColorPickerFieldValue).Value = null;

            var context = new RecordValidationContext(TestData.FormDefinition.Key, formRecord);
            await validator.ValidateAsync(context);

            context.IsValid.Should().BeFalse();
        }
Beispiel #12
0
        public async Task Decimal_field_value_less_than_min_should_be_invalid()
        {
            var formRecord = TestData.FormRecord;

            (formRecord.Values.First(_ => _ is DecimalFieldValue).FieldDefinition as DecimalField).Min = 1.0M;

            (formRecord.Values.First(_ => _ is DecimalFieldValue) as DecimalFieldValue).Value = 0.9M;

            var context = new RecordValidationContext(TestData.FormDefinition.Key, formRecord);
            await validator.ValidateAsync(context);

            context.IsValid.Should().BeFalse();
        }
        public async Task ValidateAsync(RecordValidationContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var formRecord = context.FormRecord;

            if (formRecord == null)
            {
                throw new ArgumentNullException(nameof(formRecord));
            }

            if (formRecord.FormKey.IsMissing())
            {
                throw new ArgumentNullException(nameof(formRecord.FormKey));
            }

            if (formRecord.Values.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(formRecord.Values));
            }

            var fieldValues = formRecord.Values;

            if (fieldValues.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(context.FormRecord.Values));
            }

            if (fieldValues.Any(_ => _.FieldDefinition == null))
            {
                throw new ArgumentNullException("field definition");
            }

            if (fieldValues.Any(_ => _.FieldDefinition.FieldName.IsMissing()))
            {
                throw new ArgumentNullException("field name");
            }

            await ValidateFieldValuesAsync(context);

            if (!context.IsValid)
            {
                return;
            }
        }
        protected virtual Task ValidateUploadFieldValueAsync(RecordValidationContext context)
        {
            var records = context.FormRecord.Values.Where(_ => _ is UploaderFieldValue).Select(_ => _ as UploaderFieldValue);

            foreach (var record in records)
            {
                var fieldDefinition = record.FieldDefinition as UploaderField;

                if (fieldDefinition.IsRequired && record.Value.IsNullOrEmpty())
                {
                    context.AddRecordError(record, "required validation failure");
                }
            }

            return(Task.CompletedTask);
        }
        protected virtual Task ValidateSliderFieldValueAsync(RecordValidationContext context)
        {
            var records = context.FormRecord.Values.Where(_ => _ is SliderFieldValue).Select(_ => _ as SliderFieldValue);

            foreach (var record in records)
            {
                var fieldDefinition = record.FieldDefinition as SliderField;

                if (fieldDefinition.IsRequired && !record.Value.HasValue)
                {
                    context.AddRecordError(record, "required validation failure");
                }

                if (record.Value.IsOutOfRange(fieldDefinition.Min, fieldDefinition.Max))
                {
                    context.AddRecordError(record, "range validation failure");
                }
            }

            return(Task.CompletedTask);
        }
        protected async virtual Task ValidateFieldValuesAsync(RecordValidationContext context)
        {
            await ValidateCheckboxFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateDateFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateDateTimeFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateDecimalFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateIntFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateMultiSelectFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateRadioFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateRichTextFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateSelectFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateTextAreaFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateTextboxFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateTimeFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateCascaderFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateColorPickerFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateSliderFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateSwitchFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }

            await ValidateUploadFieldValueAsync(context);

            if (!context.IsValid)
            {
                return;
            }
        }