Exemple #1
0
        public void should_not_return_optional_members_that_do_not_have_a_value_in_serialize_mode()
        {
            var @object = new OptionalMembers {
                HasValue = "fark"
            };
            var members = CreateAccessModifierNode(
                value: new SimpleValue(@object, typeof(OptionalMembers).ToCachedType()),
                mode: Mode.Serialize).ToList();

            members.ShouldTotal(1);
            members.ShouldNotContainNode(nameof(OptionalMembers.MissingValue));
            members.ShouldContainNode(nameof(OptionalMembers.HasValue));
        }
Exemple #2
0
        public OptionalMemberGenerationMode GetOptionalMemberGenerationMode()
        {
            if (string.IsNullOrEmpty(OptionalMembers))
            {
                return(OptionalMemberGenerationMode.None);
            }

            switch (OptionalMembers.ToLowerInvariant())
            {
            case "none":
                return(OptionalMemberGenerationMode.None);

            case "datamemberattribute":
                return(OptionalMemberGenerationMode.UseDataMemberAttribute);

            default:
                throw new NotSupportedException($"Specified required member discovery option is not supported: {OptionalMembers}");
            }
        }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (!value.IsDefault())
            {
                return(ValidationResult.Success);
            }

            var members = GetMembers(validationContext.ObjectType, OptionalMembers);

            foreach (var member in members)
            {
                if (!member.GetValue(validationContext.ObjectInstance).IsDefault())
                {
                    return(ValidationResult.Success);
                }
            }

            return(new ValidationResult("A value is required.",
                                        OptionalMembers.Append(validationContext.MemberName)));
        }
Exemple #4
0
        public void should_not_return_optional_members_that_do_not_have_a_value_in_serialize_mode()
        {
            var @object = new OptionalMembers { HasValue = "fark" };
            var members = CreateAccessModifierNode(
                value: new SimpleValue(@object, typeof(OptionalMembers).ToCachedType()),
                mode: Mode.Serialize).ToList();

            members.ShouldTotal(1);
            members.ShouldNotContainNode(nameof(OptionalMembers.MissingValue));
            members.ShouldContainNode(nameof(OptionalMembers.HasValue));
        }