示例#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));
        }
示例#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)));
        }
示例#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));
        }