Ejemplo n.º 1
0
        public void Valid_values_case_sensitive(string value)
        {
            var untypedValidator = new EnumValidator(typeof(DayOfWeek), ignoreCase: false);
            var typedValidator   = new EnumValidator <DayOfWeek>(ignoreCase: false);

            Should.NotThrow(() => untypedValidator.Validate(value));
            Should.NotThrow(() => typedValidator.Validate(value));
        }
Ejemplo n.º 2
0
        public void Invalid_values(string value)
        {
            var untypedValidator = new EnumValidator(typeof(DayOfWeek));
            var typedValidator   = new EnumValidator <DayOfWeek>();

            Should.Throw <ValidationException>(() => untypedValidator.Validate(value));
            Should.Throw <ValidationException>(() => typedValidator.Validate(value));
        }
Ejemplo n.º 3
0
        public void Valid_values(string value)
        {
            var untypedValidator = new EnumValidator(typeof(DayOfWeek));
            var typedValidator   = new EnumValidator <DayOfWeek>();

            Should.NotThrow(() => untypedValidator.Validate(value));
            Should.NotThrow(() => typedValidator.Validate(value));
        }
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            var valueProviderResult =
                bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if (valueProviderResult == ValueProviderResult.None)
            {
                return(Task.CompletedTask);
            }

            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);

            System.Reflection.PropertyInfo propInfo = bindingContext.ModelMetadata.ContainerType.GetProperty(bindingContext.ModelMetadata.PropertyName);
            if (propInfo == null)
            {
                return(Task.CompletedTask);
            }

            var attribute = propInfo.GetCustomAttributes(typeof(OnlyEnumAttribute), false).FirstOrDefault() as OnlyEnumAttribute;

            if (attribute == null)
            {
                return(Task.CompletedTask);
            }

            if (!EnumValidator.Validate(attribute.EnumType, valueProviderResult.ToString(), out object parsedEnum))
            {
                bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, string.Format("Object \"{0}\" of type {1} is not a {2} type", valueProviderResult.ToString(), "String", attribute.EnumType.Name));
            }
            else
            {
                if (propInfo.PropertyType == typeof(string))
                {
                    bindingContext.Result = ModelBindingResult.Success(parsedEnum.ToString());
                }
                else if (propInfo.PropertyType == typeof(int) || Nullable.GetUnderlyingType(propInfo.PropertyType) == typeof(int))
                {
                    bindingContext.Result = ModelBindingResult.Success((int)parsedEnum);
                }
                else if (propInfo.PropertyType == attribute.EnumType || Nullable.GetUnderlyingType(propInfo.PropertyType) == attribute.EnumType)
                {
                    bindingContext.Result = ModelBindingResult.Success(parsedEnum);
                }
                else
                {
                    bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, string.Format("Parameter of type {0} must be declared as an Integer, String or {1} type.", propInfo.PropertyType.Name, attribute.EnumType.Name));
                }
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 5
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.String || reader.TokenType == JsonToken.Integer)
            {
                if (objectType == typeof(string))
                {
                    if (EnumValidator.Validate(conversionType, reader.Value.ToString(), out object parsedEnum))
                    {
                        var newReader = CreateJsonReader("'" + parsedEnum.ToString() + "'");
                        return(defaultSerializer.Deserialize(newReader, objectType));
                    }
                }
                else if (objectType == typeof(int) || Nullable.GetUnderlyingType(objectType) == typeof(int))
                {
                    if (EnumValidator.Validate(conversionType, reader.Value.ToString(), out object parsedEnum))
                    {
                        var newReader = CreateJsonReader(((int)parsedEnum).ToString());
                        return(defaultSerializer.Deserialize(newReader, objectType));
                    }
                }
                else if (objectType == this.conversionType || Nullable.GetUnderlyingType(objectType) == this.conversionType)
                {
                    if (EnumValidator.Validate(conversionType, reader.Value.ToString(), out object parsedEnum))
                    {
                        var newReader = CreateJsonReader(((int)parsedEnum).ToString());
                        return(defaultSerializer.Deserialize(newReader, objectType));
                    }
                }
                else
                {
                    throw new JsonSerializationException(string.Format("Parameter of type {0} must be declared as an Integer, String or {1} type.", objectType.Name, conversionType.Name));
                }
            }

            throw new JsonSerializationException(string.Format("Object \"{0}\" is not a valid {1} value.", reader.Value, conversionType.Name));
        }