public static SchemaScope CreateTokenScope(JsonToken token, JSchema schema, ContextBase context, Scope parent, int depth)
        {
            SchemaScope scope;

            switch (token)
            {
            case JsonToken.StartObject:
                var objectScope = new ObjectScope(context, parent, depth, schema);
                context.Scopes.Add(objectScope);

                objectScope.InitializeScopes(token);

                scope = objectScope;
                break;

            case JsonToken.StartArray:
            case JsonToken.StartConstructor:
                scope = new ArrayScope(context, parent, depth, schema);
                context.Scopes.Add(scope);
                break;

            default:
                scope = new PrimativeScope(context, parent, depth, schema);
                context.Scopes.Add(scope);
                break;
            }

            if (schema._allOf != null && schema._allOf.Count > 0)
            {
                AllOfScope allOfScope = new AllOfScope(scope, context, depth);
                context.Scopes.Add(allOfScope);

                allOfScope.InitializeScopes(token, schema._allOf);
            }
            if (schema._anyOf != null && schema._anyOf.Count > 0)
            {
                AnyOfScope anyOfScope = new AnyOfScope(scope, context, depth);
                context.Scopes.Add(anyOfScope);

                anyOfScope.InitializeScopes(token, schema._anyOf);
            }
            if (schema._oneOf != null && schema._oneOf.Count > 0)
            {
                OneOfScope oneOfScope = new OneOfScope(scope, context, depth);
                context.Scopes.Add(oneOfScope);

                oneOfScope.InitializeScopes(token, schema._oneOf);
            }
            if (schema.Not != null)
            {
                NotScope notScope = new NotScope(scope, context, depth);
                context.Scopes.Add(notScope);

                notScope.InitializeScopes(token, Enumerable.Repeat(schema.Not, 1));
            }

            return(scope);
        }
        public static SchemaScope CreateTokenScope(JsonToken token, JSchema schema, ContextBase context, Scope parent, int depth)
        {
            SchemaScope scope;

            switch (token)
            {
                case JsonToken.StartObject:
                    var objectScope = new ObjectScope(context, parent, depth, schema);
                    context.Scopes.Add(objectScope);

                    objectScope.InitializeScopes(token);

                    scope = objectScope;
                    break;
                case JsonToken.StartArray:
                case JsonToken.StartConstructor:
                    scope = new ArrayScope(context, parent, depth, schema);
                    context.Scopes.Add(scope);
                    break;
                default:
                    scope = new PrimativeScope(context, parent, depth, schema);
                    context.Scopes.Add(scope);
                    break;
            }

            if (schema._allOf != null && schema._allOf.Count > 0)
            {
                AllOfScope allOfScope = new AllOfScope(scope, context, depth);
                context.Scopes.Add(allOfScope);

                allOfScope.InitializeScopes(token, schema._allOf);
            }
            if (schema._anyOf != null && schema._anyOf.Count > 0)
            {
                AnyOfScope anyOfScope = new AnyOfScope(scope, context, depth);
                context.Scopes.Add(anyOfScope);

                anyOfScope.InitializeScopes(token, schema._anyOf);
            }
            if (schema._oneOf != null && schema._oneOf.Count > 0)
            {
                OneOfScope oneOfScope = new OneOfScope(scope, context, depth);
                context.Scopes.Add(oneOfScope);

                oneOfScope.InitializeScopes(token, schema._oneOf);
            }
            if (schema.Not != null)
            {
                NotScope notScope = new NotScope(scope, context, depth);
                context.Scopes.Add(notScope);

                notScope.InitializeScopes(token, Enumerable.Repeat(schema.Not, 1));
            }

            return scope;
        }
Exemple #3
0
        public static SchemaScope CreateTokenScope(JsonToken token, JSchema schema, ContextBase context, SchemaScope parent, int depth)
        {
            SchemaScope scope;

            switch (token)
            {
            case JsonToken.StartObject:
                ObjectScope objectScope = context.Validator.GetCachedScope <ObjectScope>(ScopeType.Object);
                if (objectScope == null)
                {
                    objectScope = new ObjectScope();
                }
                objectScope.Initialize(context, parent, depth, schema);
                context.Scopes.Add(objectScope);

                objectScope.InitializeScopes(token);

                scope = objectScope;
                break;

            case JsonToken.StartArray:
            case JsonToken.StartConstructor:
                ArrayScope arrayScope = context.Validator.GetCachedScope <ArrayScope>(ScopeType.Array);
                if (arrayScope == null)
                {
                    arrayScope = new ArrayScope();
                }
                arrayScope.Initialize(context, parent, depth, schema);

                context.Scopes.Add(arrayScope);
                scope = arrayScope;
                break;

            default:
                PrimativeScope primativeScope = context.Validator.GetCachedScope <PrimativeScope>(ScopeType.Primitive);
                if (primativeScope == null)
                {
                    primativeScope = new PrimativeScope();
                }
                primativeScope.Initialize(context, parent, depth, schema);

                scope = primativeScope;
                context.Scopes.Add(scope);
                break;
            }

            if (!schema._allOf.IsNullOrEmpty())
            {
                AllOfScope allOfScope = context.Validator.GetCachedScope <AllOfScope>(ScopeType.AllOf);
                if (allOfScope == null)
                {
                    allOfScope = new AllOfScope();
                }
                allOfScope.Initialize(context, scope, depth, ScopeType.AllOf);
                context.Scopes.Add(allOfScope);

                allOfScope.InitializeScopes(token, schema._allOf.GetInnerList());
            }
            if (!schema._anyOf.IsNullOrEmpty())
            {
                AnyOfScope anyOfScope = context.Validator.GetCachedScope <AnyOfScope>(ScopeType.AnyOf);
                if (anyOfScope == null)
                {
                    anyOfScope = new AnyOfScope();
                }
                anyOfScope.Initialize(context, scope, depth, ScopeType.AnyOf);
                context.Scopes.Add(anyOfScope);

                anyOfScope.InitializeScopes(token, schema._anyOf.GetInnerList());
            }
            if (!schema._oneOf.IsNullOrEmpty())
            {
                OneOfScope oneOfScope = context.Validator.GetCachedScope <OneOfScope>(ScopeType.OneOf);
                if (oneOfScope == null)
                {
                    oneOfScope = new OneOfScope();
                }
                oneOfScope.Initialize(context, scope, depth, ScopeType.OneOf);
                context.Scopes.Add(oneOfScope);

                oneOfScope.InitializeScopes(token, schema._oneOf.GetInnerList());
            }
            if (schema.Not != null)
            {
                NotScope notScope = context.Validator.GetCachedScope <NotScope>(ScopeType.Not);
                if (notScope == null)
                {
                    notScope = new NotScope();
                }
                notScope.Initialize(context, scope, depth, ScopeType.Not);
                context.Scopes.Add(notScope);

                notScope.InitializeScopes(token, new List <JSchema> {
                    schema.Not
                });
            }

            return(scope);
        }
Exemple #4
0
        public static SchemaScope CreateTokenScope(JsonToken token, JSchema schema, ContextBase context, SchemaScope parent, int depth)
        {
            SchemaScope scope;

            switch (token)
            {
            case JsonToken.StartObject:
                ObjectScope objectScope = context.Validator.GetCachedScope <ObjectScope>(ScopeType.Object);
                if (objectScope == null)
                {
                    objectScope = new ObjectScope();
                }
                objectScope.Initialize(context, parent, depth, schema);
                context.Scopes.Add(objectScope);

                objectScope.InitializeScopes(token);

                scope = objectScope;
                break;

            case JsonToken.StartArray:
            case JsonToken.StartConstructor:
                ArrayScope arrayScope = context.Validator.GetCachedScope <ArrayScope>(ScopeType.Array);
                if (arrayScope == null)
                {
                    arrayScope = new ArrayScope();
                }
                arrayScope.Initialize(context, parent, depth, schema);

                context.Scopes.Add(arrayScope);
                scope = arrayScope;
                break;

            default:
                PrimativeScope primativeScope = context.Validator.GetCachedScope <PrimativeScope>(ScopeType.Primitive);
                if (primativeScope == null)
                {
                    primativeScope = new PrimativeScope();
                }
                primativeScope.Initialize(context, parent, depth, schema);

                scope = primativeScope;
                context.Scopes.Add(scope);
                break;
            }

            if (!schema._allOf.IsNullOrEmpty())
            {
                AllOfScope allOfScope = context.Validator.GetCachedScope <AllOfScope>(ScopeType.AllOf);
                if (allOfScope == null)
                {
                    allOfScope = new AllOfScope();
                }
                allOfScope.Initialize(context, scope, depth, ScopeType.AllOf);
                scope.AddChildScope(allOfScope);

                allOfScope.InitializeScopes(token, schema._allOf.GetInnerList(), context.Scopes.Count - 1);
            }
            if (!schema._anyOf.IsNullOrEmpty())
            {
                AnyOfScope anyOfScope = context.Validator.GetCachedScope <AnyOfScope>(ScopeType.AnyOf);
                if (anyOfScope == null)
                {
                    anyOfScope = new AnyOfScope();
                }
                anyOfScope.Initialize(context, scope, depth, ScopeType.AnyOf);
                scope.AddChildScope(anyOfScope);

                anyOfScope.InitializeScopes(token, schema._anyOf.GetInnerList(), context.Scopes.Count - 1);
            }
            if (!schema._oneOf.IsNullOrEmpty())
            {
                OneOfScope oneOfScope = context.Validator.GetCachedScope <OneOfScope>(ScopeType.OneOf);
                if (oneOfScope == null)
                {
                    oneOfScope = new OneOfScope();
                }
                oneOfScope.Initialize(context, scope, depth, ScopeType.OneOf);
                scope.AddChildScope(oneOfScope);

                oneOfScope.InitializeScopes(token, schema._oneOf.GetInnerList(), context.Scopes.Count - 1);
            }
            if (schema.Not != null)
            {
                NotScope notScope = context.Validator.GetCachedScope <NotScope>(ScopeType.Not);
                if (notScope == null)
                {
                    notScope = new NotScope();
                }
                notScope.Initialize(context, scope, depth, ScopeType.Not);
                scope.AddChildScope(notScope);

                notScope.InitializeScopes(token, new List <JSchema> {
                    schema.Not
                }, context.Scopes.Count - 1);
            }
            // only makes sense to eval if/then/else when there is an if and either a then or a else
            if (schema.If != null && (schema.Then != null || schema.Else != null))
            {
                IfThenElseScope ifThenElseScope = context.Validator.GetCachedScope <IfThenElseScope>(ScopeType.IfThenElse);
                if (ifThenElseScope == null)
                {
                    ifThenElseScope = new IfThenElseScope();
                }
                ifThenElseScope.Initialize(context, scope, depth, ScopeType.IfThenElse);
                scope.AddChildScope(ifThenElseScope);

                ifThenElseScope.If = schema.If;
                if (schema.Then != null)
                {
                    ifThenElseScope.Then        = schema.Then;
                    ifThenElseScope.ThenContext = ConditionalContext.Create(context);
                }
                if (schema.Else != null)
                {
                    ifThenElseScope.Else        = schema.Else;
                    ifThenElseScope.ElseContext = ConditionalContext.Create(context);
                }

                ifThenElseScope.InitializeScopes(token, context.Scopes.Count - 1);
            }

            return(scope);
        }
Exemple #5
0
        protected override bool EvaluateTokenCore(JsonToken token, object value, int depth)
        {
            int relativeDepth = depth - InitialDepth;

            if (relativeDepth == 0)
            {
                EnsureEnum(token, value);

                switch (token)
                {
                case JsonToken.StartObject:
                    EnsureValid(value);
                    TestType(Schema, JSchemaType.Object);
                    return(false);

                case JsonToken.EndObject:
                    if (!Schema._required.IsNullOrEmpty() && _requiredProperties.Count > 0)
                    {
                        RaiseError($"Required properties are missing from object: {StringHelpers.Join(", ", _requiredProperties)}.", ErrorType.Required, Schema, _requiredProperties, null);
                    }

                    if (Schema.MaximumProperties != null && _propertyCount > Schema.MaximumProperties)
                    {
                        RaiseError($"Object property count {_propertyCount} exceeds maximum count of {Schema.MaximumProperties}.", ErrorType.MaximumProperties, Schema, _propertyCount, null);
                    }

                    if (Schema.MinimumProperties != null && _propertyCount < Schema.MinimumProperties)
                    {
                        RaiseError($"Object property count {_propertyCount} is less than minimum count of {Schema.MinimumProperties}.", ErrorType.MinimumProperties, Schema, _propertyCount, null);
                    }

                    if (!Schema._dependencies.IsNullOrEmpty())
                    {
                        foreach (string readProperty in _readProperties)
                        {
                            if (Schema._dependencies.TryGetValue(readProperty, out object dependency))
                            {
                                if (dependency is List <string> requiredProperties)
                                {
                                    if (!requiredProperties.All(r => _readProperties.Contains(r)))
                                    {
                                        IEnumerable <string> missingRequiredProperties = requiredProperties.Where(r => !_readProperties.Contains(r));
                                        IFormattable         message = $"Dependencies for property '{readProperty}' failed. Missing required keys: {StringHelpers.Join(", ", missingRequiredProperties)}.";

                                        RaiseError(message, ErrorType.Dependencies, Schema, readProperty, null);
                                    }
                                }
                                else
                                {
                                    SchemaScope dependencyScope = _dependencyScopes[readProperty];
                                    if (dependencyScope.Context.HasErrors)
                                    {
                                        IFormattable message = $"Dependencies for property '{readProperty}' failed.";
                                        RaiseError(message, ErrorType.Dependencies, Schema, readProperty, ((ConditionalContext)dependencyScope.Context).Errors);
                                    }
                                }
                            }
                        }
                    }

                    if (Schema._patternProperties != null)
                    {
                        foreach (PatternSchema patternSchema in Schema.GetPatternSchemas())
                        {
                            if (!patternSchema.TryGetPatternRegex(
#if !(NET35 || NET40)
                                    Context.Validator.RegexMatchTimeout,
#endif
                                    out Regex _,
                                    out string errorMessage))
                            {
                                RaiseError($"Could not test property names with regex pattern '{patternSchema.Pattern}'. There was an error parsing the regex: {errorMessage}",
                                           ErrorType.PatternProperties,
                                           Schema,
                                           patternSchema.Pattern,
                                           null);
                            }
                        }
                    }
                    return(true);

                default:
                    throw new InvalidOperationException("Unexpected token when evaluating object: " + token);
                }
            }

            if (relativeDepth == 1)
            {
                if (token == JsonToken.PropertyName)
                {
                    _propertyCount++;
                    _currentPropertyName = (string)value;

                    if (!Schema._required.IsNullOrEmpty())
                    {
                        _requiredProperties.Remove(_currentPropertyName);
                    }
                    if (!Schema._dependencies.IsNullOrEmpty())
                    {
                        _readProperties.Add(_currentPropertyName);
                    }

                    if (Schema._propertyNames != null)
                    {
                        EnsureValid(this, Schema._propertyNames, value);
                        PrimativeScope.ValidateString(this, Schema._propertyNames, _currentPropertyName);
                    }

                    if (!Schema.AllowAdditionalProperties)
                    {
                        if (!IsPropertyDefinied(Schema, _currentPropertyName))
                        {
                            IFormattable message = $"Property '{_currentPropertyName}' has not been defined and the schema does not allow additional properties.";
                            RaiseError(message, ErrorType.AdditionalProperties, Schema, _currentPropertyName, null);
                        }
                    }
                }
                else
                {
                    if (JsonTokenHelpers.IsPrimitiveOrStartToken(token))
                    {
                        bool matched = false;
                        if (Schema._properties != null)
                        {
                            if (Schema._properties.TryGetValue(_currentPropertyName, out JSchema propertySchema))
                            {
                                CreateScopesAndEvaluateToken(token, value, depth, propertySchema);
                                matched = true;
                            }
                        }

                        if (Schema._patternProperties != null)
                        {
                            foreach (PatternSchema patternProperty in Schema.GetPatternSchemas())
                            {
                                if (patternProperty.TryGetPatternRegex(
#if !(NET35 || NET40)
                                        Context.Validator.RegexMatchTimeout,
#endif
                                        out Regex regex,
                                        out string _))
                                {
                                    if (RegexHelpers.IsMatch(regex, patternProperty.Pattern, _currentPropertyName))
                                    {
                                        CreateScopesAndEvaluateToken(token, value, depth, patternProperty.Schema);
                                        matched = true;
                                    }
                                }
                            }
                        }

                        if (!matched)
                        {
                            if (Schema.AllowAdditionalProperties && Schema.AdditionalProperties != null)
                            {
                                CreateScopesAndEvaluateToken(token, value, depth, Schema.AdditionalProperties);
                            }
                        }
                    }
                }
            }

            return(false);
        }