private static IEnumerable <HttpValidationError> ValidateQuery(
            RouteSpecRequestParameter parameter,
            IQueryCollection query)
        {
            var hasParameter = query.TryGetValue(parameter.Name, out var parameterValues);

            if (!hasParameter)
            {
                if (parameter.Required)
                {
                    yield return(ValidationError.ParameterRequired(parameter.Name));
                }

                yield break;
            }

            if (parameterValues.Count == 0 && !parameter.AllowEmptyValue)
            {
                yield return(ValidationError.ParameterMustHaveValue(parameter.Name));

                yield break;
            }

            var value        = parameter.GetValue(parameterValues);
            var schemaErrors = parameter.Schema.ValidateValue(value).ToArray();

            if (schemaErrors.Any())
            {
                yield return(ValidationError.InvalidParameter(parameter.Name, schemaErrors));
            }
        }
Beispiel #2
0
        public RouteSpecBuilder WithQueryParameter(string name, JSchema schema, bool required = false)
        {
            var param = new RouteSpecRequestParameter
            {
                In       = ParameterLocation.Query,
                Name     = name,
                Schema   = schema,
                Required = required,
            };

            Parameters.Add(param);
            return(this);
        }
        public static object GetValue(this RouteSpecRequestParameter parameter, StringValues values)
        {
            if (parameter.In == ParameterLocation.Path)
            {
                throw new NotImplementedException("Path parameters validation are not implemented just yet.");
            }


            var style     = parameter.Style ?? GetDefaultStyle(parameter.In);
            var delimeter = GetDelimeter(style);
            var explode   = values.Count > 1;
            var type      = parameter.Schema.GetSchemaType();

            switch (type)
            {
            case OpenApiSchemaType.Any:
            case OpenApiSchemaType.String:
            case OpenApiSchemaType.Boolean:
            case OpenApiSchemaType.Integer:
            case OpenApiSchemaType.Number:
                return(ParseScalarValue(values, parameter.Schema));

            case OpenApiSchemaType.Array:
                return(explode
                                   ? ParseArrayValue(values, parameter.Schema)
                                   : ParseArrayValue(values.Single().Split(delimeter), parameter.Schema));

            case OpenApiSchemaType.Object:
                throw new NotImplementedException(ObjectNotSupported);

            case OpenApiSchemaType.Null:
                return(null);

            case OpenApiSchemaType.Combined:
                throw new NotSupportedException();

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
 private static IEnumerable <HttpValidationError> ValidatePath(
     RouteSpecRequestParameter parameter,
     RouteData routeData)
 {
     yield break;
 }
 private static IEnumerable <HttpValidationError> ValidateCookie(RouteSpecRequestParameter parameter)
 {
     yield break;
 }
 private static IEnumerable <HttpValidationError> ValidateHeaders(
     RouteSpecRequestParameter parameter,
     IHeaderDictionary headers)
 {
     yield break;
 }