Ejemplo n.º 1
0
        private void ValidateNumber <T>(JToken node, ICollection <string> errorMessages) where T : KongObject
        {
            var number = (double)node;

            if (OneOf.Any())
            {
                var options = OneOf.Select(x => x.Value <double>()).ToArray();
                if (!options.Contains(number))
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' should be one of '{string.Join(", ", options)}').");
                }
            }
            if (Between != null)
            {
                if (number < Between[0] || number > Between[1])
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' should be between '{Between[0]}' and '{Between[1]}').");
                }
            }
            if (Gt.HasValue)
            {
                if (number <= Gt.Value)
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' should be greater than '{Gt.Value}').");
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>Gets a value indicating whether the validated data can be null.</summary>
        public virtual bool IsNullable(NullHandling nullHandling)
        {
            if (Type.HasFlag(JsonObjectType.Null) && OneOf.Count == 0)
            {
                return(true);
            }

            return((Type == JsonObjectType.None || Type.HasFlag(JsonObjectType.Null)) && OneOf.Any(o => o.IsNullable(nullHandling)));
        }
Ejemplo n.º 3
0
        /// <summary>Gets a value indicating whether the validated data can be null.</summary>
        /// <param name="schemaType">The schema type.</param>
        /// <returns>true if the type can be null.</returns>
        public virtual bool IsNullable(SchemaType schemaType)
        {
            if (IsEnumeration && Enumeration.Contains(null))
            {
                return(true);
            }

            if (Type.HasFlag(JsonObjectType.Null) && OneOf.Count == 0)
            {
                return(true);
            }

            return((Type == JsonObjectType.None || Type.HasFlag(JsonObjectType.Null)) && OneOf.Any(o => o.IsNullable(schemaType)));
        }
Ejemplo n.º 4
0
        /// <summary>Gets a value indicating whether the validated data can be null.</summary>
        /// <param name="schemaType">The schema type.</param>
        /// <returns>true if the type can be null.</returns>
        public virtual bool IsNullable(SchemaType schemaType)
        {
            if (IsNullableRaw == true)
            {
                return(true);
            }

            if (IsEnumeration && Enumeration.Contains(null))
            {
                return(true);
            }

            if (Type.HasFlag(JsonObjectType.Null))
            {
                return(true);
            }

            if ((Type == JsonObjectType.None || Type.HasFlag(JsonObjectType.Null)) && OneOf.Any(o => o.IsNullable(schemaType)))
            {
                return(true);
            }

            if (ActualSchema != this && ActualSchema.IsNullable(schemaType))
            {
                return(true);
            }

            if (ActualTypeSchema != this && ActualTypeSchema.IsNullable(schemaType))
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 5
0
        private void ValidateString <T>(JToken node, ICollection <string> errorMessages) where T : KongObject
        {
            var value = (string)node;

            if (OneOf.Any())
            {
                var options = OneOf.Select(x => x.Value <string>()).ToArray();
                if (!options.Contains(value))
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' should be one of '{string.Join(", ", options)}').");
                }
            }
            if (StartsWith != null)
            {
                if (value == null || !value.StartsWith(StartsWith))
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' should start with '{StartsWith}').");
                }
            }
            if (Match != null)
            {
                if (!LuaPattern.IsMatch(value, Match))
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' should match lua pattern '{Match}').");
                }
            }
            if (MatchAny != null)
            {
                if (MatchAny.Patterns.All(x => !LuaPattern.IsMatch(value, x)))
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' {MatchAny.Error}).");
                }
            }
            if (MatchNone.Any())
            {
                foreach (var fieldCheck in MatchNone)
                {
                    if (LuaPattern.IsMatch(value, fieldCheck.Pattern))
                    {
                        errorMessages.Add($"{Violation<T>()} (field '{node.Path}' {fieldCheck.Error}).");
                    }
                }
            }
            if (MatchAll.Any())
            {
                foreach (var fieldCheck in MatchAll)
                {
                    if (!LuaPattern.IsMatch(value, fieldCheck.Pattern))
                    {
                        errorMessages.Add($"{Violation<T>()} (field '{node.Path}' {fieldCheck.Error}).");
                    }
                }
            }
            if (LenMin.HasValue)
            {
                var len = value?.Length;
                if (len < LenMin)
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' should have min length '{LenMin.Value}').");
                }
            }
            if (Uuid)
            {
                if (!Guid.TryParse(value, out _))
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' should be a 'uuid').");
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>Gets a value indicating whether the validated data can be null.</summary>
        /// <param name="schemaType">The schema type.</param>
        /// <returns>true if the type can be null.</returns>
        public virtual bool IsNullable(SchemaType schemaType)
        {
            if (schemaType == SchemaType.OpenApi3 && IsNullableRaw.HasValue)
                return IsNullableRaw.Value;

            if (IsEnumeration && Enumeration.Contains(null))
                return true;

            if (Type.HasFlag(JsonObjectType.Null) && OneOf.Count == 0)
                return true;

            return (Type == JsonObjectType.None || Type.HasFlag(JsonObjectType.Null)) && OneOf.Any(o => o.IsNullable(schemaType));
        }