Exemple #1
0
 public void InitializeScopes(JsonToken token, List <JSchema> schemas)
 {
     foreach (JSchema schema in schemas)
     {
         SchemaScope.CreateTokenScope(token, schema, ConditionalContext, this, InitialDepth);
     }
 }
        public void ValidateCurrentToken(JsonToken token, object value, int depth)
        {
            if (_scopes.Count == 0)
            {
                if (Schema == null)
                {
                    throw new JSchemaException("No schema has been set for the validator.");
                }

                LicenseHelpers.IncrementAndCheckValidationCount();
                SchemaScope.CreateTokenScope(token, Schema, _context, null, depth);
            }

            TokenWriter?.WriteToken(token, value);

            for (int i = _scopes.Count - 1; i >= 0; i--)
            {
                Scope scope = _scopes[i];

                if (!scope.Complete)
                {
                    scope.EvaluateToken(token, value, depth);
                }
                else
                {
                    _scopes.RemoveAt(i);
                    _scopesCache.Add(scope);
                }
            }

            if (TokenWriter != null && TokenWriter.Top == 0)
            {
                TokenWriter = null;
            }
        }
        public void ValidateCurrentToken(JsonToken token, object?value, int depth)
        {
            if (depth == 0)
            {
                // Handle validating multiple content
                RemoveCompletedScopes();
            }

            if (_scopes.Count == 0)
            {
                if (Schema == null)
                {
                    throw new JSchemaException("No schema has been set for the validator.");
                }

                if (!_hasValidatedLicense)
                {
                    LicenseHelpers.IncrementAndCheckValidationCount();
                    _hasValidatedLicense = true;
                }

                SchemaScope.CreateTokenScope(token, Schema, _context, null, depth);
            }

            if (TokenWriter != null)
            {
                // JTokenReader can return JsonToken.String with a null value which WriteToken doesn't like.
                // Hacky - change token to JsonToken.Null. Can be removed when fixed Newtonsoft.Json is public.
                JsonToken fixedToken = (token == JsonToken.String && value == null) ? JsonToken.Null : token;

                TokenWriter.WriteToken(fixedToken, value);
            }

            for (int i = _scopes.Count - 1; i >= 0; i--)
            {
                Scope scope = _scopes[i];

                if (scope.Complete != CompleteState.Completed)
                {
                    scope.EvaluateToken(token, value, depth);
                }
                else
                {
                    _scopes.RemoveAt(i);
                    _scopesCache.Add(scope);
                }
            }

            if (TokenWriter != null && (TokenWriter.WriteState == WriteState.Start || TokenWriter.WriteState == WriteState.Closed))
            {
                TokenWriter = null;
            }
        }
Exemple #4
0
        public void InitializeScopes(JsonToken token, List <JSchema> schemas)
        {
            foreach (JSchema schema in schemas)
            {
                // check to see whether a scope with the same schema exists
                SchemaScope childScope = GetExistingSchemaScope(schema);

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

                ChildScopes.Add(childScope);
            }
        }
Exemple #5
0
        public void ValidateCurrentToken(JsonToken token, object value, int depth)
        {
            if (depth == 0)
            {
                // Handle validating multiple content
                RemoveCompletedScopes();
            }

            if (_scopes.Count == 0)
            {
                if (Schema == null)
                {
                    throw new JSchemaException("No schema has been set for the validator.");
                }

                if (!_hasValidatedLicense)
                {
                    LicenseHelpers.IncrementAndCheckValidationCount();
                    _hasValidatedLicense = true;
                }

                SchemaScope.CreateTokenScope(token, Schema, _context, null, depth);
            }

            TokenWriter?.WriteToken(token, value);

            for (int i = _scopes.Count - 1; i >= 0; i--)
            {
                Scope scope = _scopes[i];

                if (!scope.Complete)
                {
                    scope.EvaluateToken(token, value, depth);
                }
                else
                {
                    _scopes.RemoveAt(i);
                    _scopesCache.Add(scope);
                }
            }

            if (TokenWriter != null && (TokenWriter.WriteState == WriteState.Start || TokenWriter.WriteState == WriteState.Closed))
            {
                TokenWriter = null;
            }
        }
        public void InitializeScopes(JsonToken token, List <JSchema> schemas, int scopeIndex)
        {
            foreach (JSchema schema in schemas)
            {
                // 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, ConditionalContext, null, InitialDepth);
                }

                ChildScopes.Add(childScope);
            }
        }
Exemple #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);
        }