コード例 #1
0
        public IEnumerable <ValidationError> Visit(StringFieldProperties properties)
        {
            if (!properties.Editor.IsEnumValue())
            {
                yield return(new ValidationError("Editor is not a valid value.",
                                                 nameof(properties.Editor)));
            }

            if ((properties.Editor == StringFieldEditor.Radio || properties.Editor == StringFieldEditor.Dropdown) && (properties.AllowedValues == null || properties.AllowedValues.Count == 0))
            {
                yield return(new ValidationError("Radio buttons or dropdown list need allowed values.",
                                                 nameof(properties.AllowedValues)));
            }

            if (properties.Pattern != null && !properties.Pattern.IsValidRegex())
            {
                yield return(new ValidationError("Pattern is not a valid expression.",
                                                 nameof(properties.Pattern)));
            }

            if (properties.MaxLength.HasValue && properties.MinLength.HasValue && properties.MinLength.Value >= properties.MaxLength.Value)
            {
                yield return(new ValidationError("Max length must be greater than min length.",
                                                 nameof(properties.MinLength),
                                                 nameof(properties.MaxLength)));
            }

            if (properties.AllowedValues != null && properties.AllowedValues.Count > 0 && (properties.MinLength.HasValue || properties.MaxLength.HasValue))
            {
                yield return(new ValidationError("Either allowed values or min and max length can be defined.",
                                                 nameof(properties.AllowedValues),
                                                 nameof(properties.MinLength),
                                                 nameof(properties.MaxLength)));
            }
        }
コード例 #2
0
        public FieldPropertiesDto Visit(StringFieldProperties properties)
        {
            var result = SimpleMapper.Map(properties, new StringFieldPropertiesDto());

            result.AllowedValues = properties.AllowedValues?.ToArray();

            return(result);
        }
コード例 #3
0
        public void Should_not_add_error_if_inline_editing_is_allowed_for_editor(StringFieldEditor editor)
        {
            var sut = new StringFieldProperties {
                InlineEditable = true, Editor = editor, AllowedValues = ImmutableList.Create("Value")
            };

            var errors = FieldPropertiesValidator.Validate(sut).ToList();

            Assert.Empty(errors);
        }
コード例 #4
0
        public void Should_not_add_error_if_min_words_equal_to_max_words()
        {
            var sut = new StringFieldProperties {
                MinWords = 2, MaxWords = 2
            };

            var errors = FieldPropertiesValidator.Validate(sut).ToList();

            Assert.Empty(errors);
        }
コード例 #5
0
        private static FieldPropertiesDto Convert(StringFieldProperties source)
        {
            var result = SimpleMapper.Map(source, new StringFieldPropertiesDto());

            if (source.AllowedValues != null)
            {
                result.AllowedValues = source.AllowedValues.ToArray();
            }

            return(result);
        }
コード例 #6
0
        public IEnumerable <ValidationError> Visit(StringFieldProperties properties)
        {
            if (!properties.Editor.IsEnumValue())
            {
                yield return(new ValidationError(Not.Valid(nameof(properties.Editor)),
                                                 nameof(properties.Editor)));
            }

            if (!properties.ContentType.IsEnumValue())
            {
                yield return(new ValidationError(Not.Valid(nameof(properties.ContentType)),
                                                 nameof(properties.ContentType)));
            }

            if ((properties.Editor == StringFieldEditor.Radio || properties.Editor == StringFieldEditor.Dropdown) && properties.AllowedValues?.Any() != true)
            {
                yield return(new ValidationError(T.Get("schemas.stringEditorsNeedAllowedValuesError"),
                                                 nameof(properties.AllowedValues)));
            }

            if (properties.Pattern != null && !properties.Pattern.IsValidRegex())
            {
                yield return(new ValidationError(Not.Valid(nameof(properties.Pattern)),
                                                 nameof(properties.Pattern)));
            }

            if (IsMaxGreaterThanMin(properties.MaxLength, properties.MinLength))
            {
                yield return(new ValidationError(Not.GreaterEqualsThan(nameof(properties.MaxLength), nameof(properties.MinLength)),
                                                 nameof(properties.MinLength),
                                                 nameof(properties.MaxLength)));
            }

            if (IsMaxGreaterThanMin(properties.MaxWords, properties.MinWords))
            {
                yield return(new ValidationError(Not.GreaterEqualsThan(nameof(properties.MaxWords), nameof(properties.MinWords)),
                                                 nameof(properties.MinWords),
                                                 nameof(properties.MaxWords)));
            }

            if (IsMaxGreaterThanMin(properties.MaxCharacters, properties.MinCharacters))
            {
                yield return(new ValidationError(Not.GreaterEqualsThan(nameof(properties.MaxCharacters), nameof(properties.MinCharacters)),
                                                 nameof(properties.MinCharacters),
                                                 nameof(properties.MaxCharacters)));
            }

            if (properties.InlineEditable && properties.Editor != StringFieldEditor.Dropdown && properties.Editor != StringFieldEditor.Input && properties.Editor != StringFieldEditor.Slug)
            {
                yield return(new ValidationError(T.Get("schemas.string.inlineEditorError"),
                                                 nameof(properties.InlineEditable),
                                                 nameof(properties.Editor)));
            }
        }
コード例 #7
0
        public void Should_add_error_if_radio_button_has_no_allowed_values()
        {
            var sut = new StringFieldProperties {
                Editor = StringFieldEditor.Radio
            };

            var errors = FieldPropertiesValidator.Validate(sut).ToList();

            errors.Should().BeEquivalentTo(
                new List <ValidationError>
            {
                new ValidationError("Radio buttons or dropdown list need allowed values.", "AllowedValues")
            });
        }
コード例 #8
0
        public void Should_add_error_if_editor_is_not_valid()
        {
            var sut = new StringFieldProperties {
                Editor = (StringFieldEditor)123
            };

            var errors = FieldPropertiesValidator.Validate(sut).ToList();

            errors.Should().BeEquivalentTo(
                new List <ValidationError>
            {
                new ValidationError("Editor is not a valid value.", "Editor")
            });
        }
コード例 #9
0
        public void Should_add_error_if_inline_editing_is_not_allowed_for_editor(StringFieldEditor editor)
        {
            var sut = new StringFieldProperties {
                InlineEditable = true, Editor = editor, AllowedValues = ImmutableList.Create("Value")
            };

            var errors = FieldPropertiesValidator.Validate(sut).ToList();

            errors.Should().BeEquivalentTo(
                new List <ValidationError>
            {
                new ValidationError("Inline editing is only allowed for dropdowns, slugs and input fields.", "InlineEditable", "Editor")
            });
        }
コード例 #10
0
        public void Should_add_error_if_content_type_is_not_valid()
        {
            var sut = new StringFieldProperties {
                ContentType = (StringContentType)123
            };

            var errors = FieldPropertiesValidator.Validate(sut).ToList();

            errors.Should().BeEquivalentTo(
                new List <ValidationError>
            {
                new ValidationError("Content type is not a valid value.", "ContentType")
            });
        }
コード例 #11
0
        public void Should_add_error_if_min_words_greater_than_max()
        {
            var sut = new StringFieldProperties {
                MinWords = 10, MaxWords = 5
            };

            var errors = FieldPropertiesValidator.Validate(sut).ToList();

            errors.Should().BeEquivalentTo(
                new List <ValidationError>
            {
                new ValidationError("Max words must be greater or equal to min words.", "MinWords", "MaxWords")
            });
        }
コード例 #12
0
        public void Should_add_error_if_min_length_greater_than_max()
        {
            var sut = new StringFieldProperties {
                MinLength = 10, MaxLength = 5
            };

            var errors = FieldPropertiesValidator.Validate(sut).ToList();

            errors.Should().BeEquivalentTo(
                new List <ValidationError>
            {
                new ValidationError("Max length must be greater or equal to min length.", "MinLength", "MaxLength")
            });
        }
コード例 #13
0
        public void Should_add_error_if_allowed_values_and_min_value_is_specified()
        {
            var sut = new StringFieldProperties {
                MaxLength = 10, AllowedValues = ReadOnlyCollection.Create("4")
            };

            var errors = FieldPropertiesValidator.Validate(sut).ToList();

            errors.Should().BeEquivalentTo(
                new List <ValidationError>
            {
                new ValidationError("Either allowed values or min and max length can be defined.", "AllowedValues", "MinLength", "MaxLength")
            });
        }
コード例 #14
0
        public void Should_add_error_if_pattern_is_not_valid_regex()
        {
            var sut = new StringFieldProperties {
                Pattern = "[0-9{1}"
            };

            var errors = FieldPropertiesValidator.Validate(sut).ToList();

            errors.Should().BeEquivalentTo(
                new List <ValidationError>
            {
                new ValidationError("Pattern is not a valid value.", "Pattern")
            });
        }
コード例 #15
0
        public IEnumerable <ValidationError> Visit(StringFieldProperties properties)
        {
            if (!properties.Editor.IsEnumValue())
            {
                yield return(new ValidationError(Not.Valid("Editor"),
                                                 nameof(properties.Editor)));
            }

            if ((properties.Editor == StringFieldEditor.Radio || properties.Editor == StringFieldEditor.Dropdown) && (properties.AllowedValues == null || properties.AllowedValues.Count == 0))
            {
                yield return(new ValidationError("Radio buttons or dropdown list need allowed values.",
                                                 nameof(properties.AllowedValues)));
            }

            if (properties.Pattern != null && !properties.Pattern.IsValidRegex())
            {
                yield return(new ValidationError(Not.Valid("Pattern"),
                                                 nameof(properties.Pattern)));
            }

            if (properties.MaxLength.HasValue && properties.MinLength.HasValue && properties.MinLength.Value > properties.MaxLength.Value)
            {
                yield return(new ValidationError(Not.GreaterEquals("Max length", "min length"),
                                                 nameof(properties.MinLength),
                                                 nameof(properties.MaxLength)));
            }

            if (properties.AllowedValues != null && properties.AllowedValues.Count > 0 && (properties.MinLength.HasValue || properties.MaxLength.HasValue))
            {
                yield return(new ValidationError("Either allowed values or min and max length can be defined.",
                                                 nameof(properties.AllowedValues),
                                                 nameof(properties.MinLength),
                                                 nameof(properties.MaxLength)));
            }

            if (properties.InlineEditable && properties.Editor != StringFieldEditor.Dropdown && properties.Editor != StringFieldEditor.Input && properties.Editor != StringFieldEditor.Slug)
            {
                yield return(new ValidationError("Inline editing is only allowed for dropdowns, slugs and input fields.",
                                                 nameof(properties.InlineEditable),
                                                 nameof(properties.Editor)));
            }
        }
コード例 #16
0
        public void Should_compare_two_string_fields_as_equal()
        {
            var lhs = new StringFieldProperties
            {
                DefaultValues = new LocalizedValue <string?>(new Dictionary <string, string?>
                {
                    ["iv"] = "ABC"
                })
            };

            var rhs = new StringFieldProperties
            {
                DefaultValues = new LocalizedValue <string?>(new Dictionary <string, string?>
                {
                    ["iv"] = "ABC"
                })
            };

            Assert.Equal(lhs, rhs);
        }
コード例 #17
0
        public void Should_create_events_if_field_updated()
        {
            var properties = new StringFieldProperties {
                IsRequired = true
            };

            var sourceSchema =
                new Schema("source")
                .AddString(stringId.Id, stringId.Name, Partitioning.Invariant);

            var targetSchema =
                new Schema("target")
                .AddString(stringId.Id, stringId.Name, Partitioning.Invariant, properties);

            var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);

            events.ShouldHaveSameEvents(
                new FieldUpdated {
                Properties = properties, FieldId = stringId
            }
                );
        }
コード例 #18
0
        public IEnumerable <IValidator> Visit(StringFieldProperties properties)
        {
            if (properties.IsRequired)
            {
                yield return(new RequiredStringValidator());
            }

            if (properties.MinLength.HasValue || properties.MaxLength.HasValue)
            {
                yield return(new StringLengthValidator(properties.MinLength, properties.MaxLength));
            }

            if (!string.IsNullOrWhiteSpace(properties.Pattern))
            {
                yield return(new PatternValidator(properties.Pattern, properties.PatternMessage));
            }

            if (properties.AllowedValues != null)
            {
                yield return(new AllowedValuesValidator <string>(properties.AllowedValues.ToArray()));
            }
        }
コード例 #19
0
        public void Should_create_events_if_nested_field_updated()
        {
            var properties = new StringFieldProperties {
                IsRequired = true
            };

            var sourceSchema =
                new Schema("source")
                .AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
                          .AddString(nestedId.Id, nestedId.Name));

            var targetSchema =
                new Schema("target")
                .AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
                          .AddString(nestedId.Id, nestedId.Name, properties));

            var events = sourceSchema.Synchronize(targetSchema, idGenerator);

            events.ShouldHaveSameEvents(
                new FieldUpdated {
                Properties = properties, FieldId = nestedId, ParentFieldId = arrayId
            }
                );
        }
コード例 #20
0
 public FieldPropertiesDto Visit(StringFieldProperties properties)
 {
     return(SimpleMapper.Map(properties, new StringFieldPropertiesDto()));
 }
コード例 #21
0
 public static Field <StringFieldProperties> String(long id, string name, Partitioning partitioning, StringFieldProperties properties = null)
 {
     return(new Field <StringFieldProperties>(id, name, partitioning, properties ?? new StringFieldProperties()));
 }
コード例 #22
0
 public JToken Visit(StringFieldProperties properties)
 {
     return(properties.DefaultValue);
 }
コード例 #23
0
ファイル: StringFieldTests.cs プロジェクト: jrlost/squidex
 private static RootField <StringFieldProperties> Field(StringFieldProperties properties)
 {
     return(Fields.String(1, "myString", Partitioning.Invariant, properties));
 }
コード例 #24
0
        public IJsonValue Visit(StringFieldProperties properties, Args args)
        {
            var value = GetDefaultValue(properties.DefaultValue, properties.DefaultValues, args.Partition);

            return(JsonValue.Create(value));
        }
コード例 #25
0
 private static StringField Field(StringFieldProperties properties)
 {
     return(new StringField(1, "my-string", Partitioning.Invariant, properties));
 }
コード例 #26
0
 public static Schema AddString(this Schema schema, long id, string name, Partitioning partitioning, StringFieldProperties properties = null)
 {
     return(schema.AddField(String(id, name, partitioning, properties)));
 }