public override void Initialize(ContextBase context, SchemaScope?parent, int initialDepth, ScopeType type)
        {
            base.Initialize(context, parent, initialDepth, type);

            if (parent != null)
            {
                parent.SchemaChildren.Add(this);
            }
        }
Esempio n. 2
0
        public override void Initialize(ContextBase context, SchemaScope?parent, int initialDepth, ScopeType type)
        {
            ValidationUtils.Assert(parent != null);

            base.Initialize(context, parent, initialDepth, type);

            ChildScopes.Clear();
            ParentSchemaScope  = parent;
            ConditionalContext = ConditionalContext.Create(context, parent.ShouldValidateUnevaluated());
        }
        public override void Initialize(ContextBase context, SchemaScope?parent, int initialDepth, ScopeType type)
        {
            base.Initialize(context, parent, initialDepth, type);

            If   = null;
            Then = null;
            Else = null;

            ThenContext = null;
            ElseContext = null;
        }
        public void Initialize(ContextBase context, SchemaScope?parent, int initialDepth, JSchema schema)
        {
            Initialize(context, parent, initialDepth, ScopeType.Object);
            InitializeSchema(schema);

            _propertyCount       = 0;
            _currentPropertyName = null;

            if (!schema._required.IsNullOrEmpty())
            {
                if (_requiredProperties != null)
                {
                    _requiredProperties.Clear();
                }
                else
                {
                    _requiredProperties = new List <string>(schema._required.Count);
                }

                foreach (string required in schema._required)
                {
                    _requiredProperties.Add(required);
                }
            }

            // Need to track the properties read when:
            //  - Schema has dependencies or,
            //  - Need to validate unevaluated properties
            if (HasDependencies())
            {
                if (ReadProperties != null)
                {
                    ReadProperties.Clear();
                }
                else
                {
                    ReadProperties = new List <string>();
                }
            }

            if (ShouldValidateUnevaluated())
            {
                if (_unevaluatedScopes != null)
                {
                    _unevaluatedScopes.Clear();
                }
                else
                {
                    _unevaluatedScopes = new Dictionary <string, UnevaluatedContext>(StringComparer.Ordinal);
                }
            }
        }
Esempio n. 5
0
        public virtual void Initialize(ContextBase context, SchemaScope?parent, int initialDepth, ScopeType type)
        {
            Context      = context;
            Parent       = parent;
            InitialDepth = initialDepth;
            Type         = type;
            Complete     = false;

#if DEBUG
            Interlocked.Increment(ref LastDebugId);
            DebugId = LastDebugId;
#endif
        }
Esempio n. 6
0
        public void Initialize(ContextBase context, SchemaScope?parent, int initialDepth, JSchema schema)
        {
            Initialize(context, parent, initialDepth, ScopeType.Array);
            InitializeSchema(schema);

            _index      = -1;
            _matchCount = 0;

            if (schema.Contains != null)
            {
                if (_containsContexts != null)
                {
                    _containsContexts.Clear();
                }
                else
                {
                    _containsContexts = new List <ConditionalContext>();
                }
            }

            if (schema.UniqueItems)
            {
                if (_uniqueArrayItems != null)
                {
                    _uniqueArrayItems.Clear();
                }
                else
                {
                    _uniqueArrayItems = new List <JToken>();
                }
            }

            if (ShouldValidateUnevaluated())
            {
                if (_unevaluatedScopes != null)
                {
                    _unevaluatedScopes.Clear();
                }
                else
                {
                    _unevaluatedScopes = new Dictionary <int, UnevaluatedContext>();
                }
            }
        }
Esempio n. 7
0
        public void InitializeScope(JsonToken token, int scopeIndex, JSchema schema, ContextBase context)
        {
            // cache this for performance
            int scopeCurrentIndex = scopeIndex;

            // check to see whether a scope with the same schema exists
            SchemaScope?childScope = GetExistingSchemaScope(schema, ref scopeCurrentIndex);

            if (childScope == null)
            {
                childScope = SchemaScope.CreateTokenScope(token, schema, context, null, InitialDepth);
            }
            else
            {
                if (childScope.Context != context)
                {
                    // The schema scope needs to be part of a different conditional contexts.
                    // We need to create a composite so that errors are raised to both.
                    CompositeContext?compositeContext = childScope.Context as CompositeContext;
                    if (compositeContext == null)
                    {
                        compositeContext = new CompositeContext(context.Validator);
                        compositeContext.Contexts.Add(childScope.Context);
                        compositeContext.Contexts.Add(context);

                        childScope.Context = compositeContext;
                    }
                    else
                    {
                        if (!compositeContext.Contexts.Contains(context))
                        {
                            compositeContext.Contexts.Add(context);
                        }
                    }
                }
            }

#if DEBUG
            childScope.ConditionalParents.Add(this);
#endif

            ChildScopes.Add(childScope);
        }
        private void InitializeScope(JsonToken token, int scopeIndex, JSchema schema, ConditionalContext context)
        {
            // cache this for performance
            int scopeCurrentIndex = scopeIndex;

            // check to see whether a scope with the same schema exists
            SchemaScope?childScope = GetExistingSchemaScope(schema, ref scopeCurrentIndex);

            if (childScope == null)
            {
                childScope = SchemaScope.CreateTokenScope(token, schema, context, null, InitialDepth);
            }

#if DEBUG
            childScope.ConditionalParents.Add(this);
#endif

            ChildScopes.Add(childScope);
        }
Esempio n. 9
0
        public override void Initialize(ContextBase context, SchemaScope?parent, int initialDepth, ScopeType type)
        {
            base.Initialize(context, parent, initialDepth, type);

            PropertyName = null;
        }
        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.Ref != null)
            {
                RefScope?refScope = context.Validator.GetCachedScope <RefScope>(ScopeType.Ref);
                if (refScope == null)
                {
                    refScope = new RefScope();
                }
                refScope.Initialize(context, scope, depth, ScopeType.Ref);
                scope.AddChildScope(refScope);

                refScope.InitializeScopes(token, new List <JSchema> {
                    schema.Ref
                }, context.Scopes.Count - 1);
            }
            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 = scope.CreateConditionalContext();
                }
                if (schema.Else != null)
                {
                    ifThenElseScope.Else        = schema.Else;
                    ifThenElseScope.ElseContext = scope.CreateConditionalContext();
                }

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

            return(scope);
        }
        protected SchemaScope CreateScopesAndEvaluateToken(JsonToken token, object?value, int depth, JSchema schema, SchemaScope?parent, ContextBase context)
        {
            int startCount = Context.Scopes.Count;

            SchemaScope createdScope = CreateTokenScope(token, schema, context, parent, depth);

            int start = Context.Scopes.Count - 1;
            int end   = startCount;

            for (int i = start; i >= end; i--)
            {
                Scope newScope = Context.Scopes[i];
                newScope.EvaluateToken(token, value, depth);
            }

            return(createdScope);
        }
Esempio n. 12
0
 public void Initialize(ContextBase context, SchemaScope?parent, int initialDepth, JSchema schema)
 {
     Initialize(context, parent, initialDepth, ScopeType.Primitive);
     InitializeSchema(schema);
 }
Esempio n. 13
0
        protected bool TryGetSchemaScopeBySchema(JSchema schema, JsonToken token, object?value, int depth, [NotNullWhen(true)] out SchemaScope?schemaScope)
        {
            for (int i = 0; i < ChildScopes.Count; i++)
            {
                schemaScope = ChildScopes[i];

                if (schemaScope.Schema == schema)
                {
                    if (!AssertScopeComplete(schemaScope, token, value, depth))
                    {
                        schemaScope = null;
                        return(false);
                    }

                    return(true);
                }
            }

            throw new InvalidOperationException("Expected to find schema scope for schema.");
        }