Esempio n. 1
0
        protected override bool EvaluateTokenCore(JsonToken token, object value, int depth)
        {
            if (!GetChildrenAllValid(token, value, depth))
            {
                List <int> invalidIndexes = new List <int>();
                int        index          = 0;
                foreach (SchemaScope schemaScope in ChildScopes)
                {
                    if (!schemaScope.IsValid)
                    {
                        invalidIndexes.Add(index);
                    }
                    else
                    {
                        ConditionalContext.TrackEvaluatedSchema(schemaScope.Schema);
                    }

                    index++;
                }

                IFormattable message = $"JSON does not match all schemas from 'allOf'. Invalid schema indexes: {StringHelpers.Join(", ", invalidIndexes)}.";
                RaiseError(message, ErrorType.AllOf, ParentSchemaScope.Schema, null, ConditionalContext.Errors);
            }
            else
            {
                for (int i = 0; i < ChildScopes.Count; i++)
                {
                    ConditionalContext.TrackEvaluatedSchema(ChildScopes[i].Schema);
                }
            }

            return(true);
        }
Esempio n. 2
0
        protected override bool EvaluateTokenCore(JsonToken token, object?value, int depth)
        {
            if (TryGetChildrenAnyValid(token, value, depth, out bool anyValid))
            {
                if (!anyValid)
                {
                    RaiseError($"JSON does not match any schemas from 'anyOf'.", ErrorType.AnyOf, ParentSchemaScope.Schema, null, ConditionalContext.Errors);
                }

                // TODO: A little inefficent to find the valid children again
                foreach (SchemaScope childScope in ChildScopes)
                {
                    if (childScope.IsValid)
                    {
                        ConditionalContext.TrackEvaluatedSchema(childScope.Schema);
                    }
                }
            }
            else
            {
                RaiseCircularDependencyError(ErrorType.AnyOf);
            }

            return(true);
        }
Esempio n. 3
0
        protected override bool EvaluateTokenCore(JsonToken token, object?value, int depth)
        {
            if (!TryGetSchemaScopeBySchema(If !, token, value, depth, out SchemaScope? ifScope))
            {
                RaiseCircularDependencyError(ErrorType.None);
                return(true);
            }

            if (ifScope.IsValid)
            {
                ConditionalContext.TrackEvaluatedSchema(ifScope.Schema);

                if (Then != null)
                {
                    if (!TryGetSchemaScopeBySchema(Then, token, value, depth, out SchemaScope? thenScope))
                    {
                        RaiseCircularDependencyError(ErrorType.Then);
                        return(true);
                    }

                    if (!thenScope.IsValid)
                    {
                        RaiseError($"JSON does not match schema from 'then'.", ErrorType.Then, Then, null, thenScope.GetValidationErrors());
                    }
                    else
                    {
                        ConditionalContext.TrackEvaluatedSchema(thenScope.Schema);
                    }
                }
            }
            else
            {
                if (Else != null)
                {
                    if (!TryGetSchemaScopeBySchema(Else, token, value, depth, out SchemaScope? elseScope))
                    {
                        RaiseCircularDependencyError(ErrorType.Else);
                        return(true);
                    }

                    if (!elseScope.IsValid)
                    {
                        RaiseError($"JSON does not match schema from 'else'.", ErrorType.Else, Else, null, elseScope.GetValidationErrors());
                    }
                    else
                    {
                        ConditionalContext.TrackEvaluatedSchema(elseScope.Schema);
                    }
                }
            }

            return(true);
        }
        protected override bool EvaluateTokenCore(JsonToken token, object?value, int depth)
        {
            // If there is a recursive chain of $ref schemas then this will stack overflow.
            // If the ref scope is already evaluating then exit without checking children.
            if (_isReentrant)
            {
                return(true);
            }

            _isReentrant = true;
            try
            {
                if (TryGetChildrenAllValid(token, value, depth, out bool allValid))
                {
                    if (!allValid)
                    {
                        List <int> invalidIndexes = new List <int>();
                        int        index          = 0;
                        foreach (SchemaScope schemaScope in ChildScopes)
                        {
                            if (!schemaScope.IsValid)
                            {
                                invalidIndexes.Add(index);
                            }
                            else
                            {
                                ConditionalContext.TrackEvaluatedSchema(schemaScope.Schema);
                            }

                            index++;
                        }

                        IFormattable message = $"JSON does not match schema from '$ref'.";
                        RaiseError(message, ErrorType.Ref, ParentSchemaScope.Schema, null, ConditionalContext.Errors);
                    }
                    else
                    {
                        ConditionalContext.TrackEvaluatedSchema(ChildScopes[0].Schema);
                    }
                }
                else
                {
                    RaiseCircularDependencyError(ErrorType.Ref);
                }

                return(true);
            }
            finally
            {
                _isReentrant = false;
            }
        }
        protected override bool EvaluateTokenCore(JsonToken token, object?value, int depth)
        {
            if (TryGetChildrenValidCount(token, value, depth, out int validCount))
            {
                if (validCount != 1)
                {
                    List <int> validIndexes = new List <int>();
                    int        index        = 0;
                    foreach (SchemaScope schemaScope in ChildScopes)
                    {
                        if (schemaScope.IsValid)
                        {
                            validIndexes.Add(index);
                        }

                        index++;
                    }

                    IFormattable message;
                    if (validIndexes.Count > 0)
                    {
                        message = $"JSON is valid against more than one schema from 'oneOf'. Valid schema indexes: {StringHelpers.Join(", ", validIndexes)}.";
                    }
                    else
                    {
                        message = $"JSON is valid against no schemas from 'oneOf'.";
                    }

                    RaiseError(message, ErrorType.OneOf, ParentSchemaScope.Schema, null, ConditionalContext.Errors);
                }
                else
                {
                    // TODO: A little inefficent to find the valid child again
                    foreach (SchemaScope childScope in ChildScopes)
                    {
                        if (childScope.IsValid)
                        {
                            ConditionalContext.TrackEvaluatedSchema(childScope.Schema);
                        }
                    }
                }
            }
            else
            {
                RaiseCircularDependencyError(ErrorType.OneOf);
            }

            return(true);
        }
Esempio n. 6
0
        protected override bool EvaluateTokenCore(JsonToken token, object value, int depth)
        {
            SchemaScope ifScope = GetSchemaScopeBySchema(If, token, value, depth);

            if (ifScope.IsValid)
            {
                ConditionalContext.TrackEvaluatedSchema(ifScope.Schema);

                if (Then != null)
                {
                    SchemaScope thenScope = GetSchemaScopeBySchema(Then, token, value, depth);

                    if (!thenScope.IsValid)
                    {
                        ConditionalContext context = (ConditionalContext)thenScope.Context;
                        RaiseError($"JSON does not match schema from 'then'.", ErrorType.Then, Then, null, context.Errors);
                    }
                    else
                    {
                        ConditionalContext.TrackEvaluatedSchema(thenScope.Schema);
                    }
                }
            }
            else
            {
                if (Else != null)
                {
                    SchemaScope elseScope = GetSchemaScopeBySchema(Else, token, value, depth);

                    if (!elseScope.IsValid)
                    {
                        ConditionalContext context = (ConditionalContext)elseScope.Context;
                        RaiseError($"JSON does not match schema from 'else'.", ErrorType.Else, Else, null, context.Errors);
                    }
                    else
                    {
                        ConditionalContext.TrackEvaluatedSchema(elseScope.Schema);
                    }
                }
            }

            return(true);
        }
Esempio n. 7
0
        protected override bool EvaluateTokenCore(JsonToken token, object?value, int depth)
        {
            if (Parent is ObjectScope objectScope)
            {
                var readProperties = objectScope.ReadProperties;
                ValidationUtils.Assert(readProperties != null);
                ValidationUtils.Assert(PropertyName != null);

                if (readProperties.Contains(PropertyName))
                {
                    if (TryGetChildrenAnyValid(token, value, depth, out bool anyValid))
                    {
                        if (!anyValid)
                        {
                            RaiseError($"Dependencies for property '{PropertyName}' failed.", ErrorType.Dependencies, ParentSchemaScope.Schema, null, ConditionalContext.Errors);
                        }
                        else
                        {
                            foreach (SchemaScope childScope in ChildScopes)
                            {
                                if (childScope.IsValid)
                                {
                                    ConditionalContext.TrackEvaluatedSchema(childScope.Schema);
                                }
                            }
                        }
                    }
                    else
                    {
                        RaiseCircularDependencyError(ErrorType.Dependencies);
                    }
                }
            }

            return(true);
        }