Пример #1
0
 protected void Initialize(ContextBase context, Scope parent, int initialDepth, ScopeType type)
 {
     Context = context;
     Parent = parent;
     InitialDepth = initialDepth;
     Type = type;
     Complete = false;
 }
        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;
        }
Пример #3
0
        public ObjectScope(ContextBase context, Scope parent, int initialDepth, JSchema schema)
            : base(context, parent, initialDepth, schema)
        {
            if (schema._required != null)
                _requiredProperties = schema._required.ToList();

            if (schema._dependencies != null && schema._dependencies.Count > 0)
            {
                _readProperties = new List<string>();

                if (schema._dependencies.Values.OfType<JSchema>().Any())
                    _dependencyScopes = new Dictionary<string, SchemaScope>();
            }
        }
        public void Initialize(ContextBase context, Scope 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);
                }
            }

            if (!schema._dependencies.IsNullOrEmpty())
            {
                if (_readProperties != null)
                {
                    _readProperties.Clear();
                }
                else
                {
                    _readProperties = new List<string>();
                }

                if (schema._dependencies.HasSchemas)
                {
                    if (_dependencyScopes != null)
                    {
                        _dependencyScopes.Clear();
                    }
                    else
                    {
                        _dependencyScopes = new Dictionary<string, SchemaScope>(StringComparer.Ordinal);
                    }
                }
            }
        }
Пример #5
0
        public void Initialize(ContextBase context, Scope parent, int initialDepth, JSchema schema)
        {
            Initialize(context, parent, initialDepth, ScopeType.Array);
            InitializeSchema(schema);

            _index = -1;

            if (schema.UniqueItems)
            {
                if (_uniqueArrayItems != null)
                {
                    _uniqueArrayItems.Clear();
                }
                else
                {
                    _uniqueArrayItems = new List<JToken>();
                }
            }
        }
        public ObjectScope(ContextBase context, Scope parent, int initialDepth, JSchema schema)
            : base(context, parent, initialDepth, schema)
        {
            if (schema._required != null)
                _requiredProperties = schema._required.ToList();

            if (schema._dependencies != null && schema._dependencies.Count > 0)
            {
                _readProperties = new List<string>();

                foreach (KeyValuePair<string, object> dependency in schema._dependencies)
                {
                    if (dependency.Value is JSchema)
                    {
                        _dependencyScopes = new Dictionary<string, SchemaScope>(StringComparer.Ordinal);
                        break;
                    }
                }
            }
        }
 public PrimativeScope(ContextBase context, Scope parent, int initialDepth, JSchema schema)
     : base(context, parent, initialDepth, schema)
 {
 }
Пример #8
0
 protected Scope(ContextBase context, Scope parent, int initialDepth)
 {
     Context = context;
     Parent = parent;
     InitialDepth = initialDepth;
 }
 protected SchemaScope(ContextBase context, Scope parent, int initialDepth, JSchema schema)
     : base(context, parent, initialDepth)
 {
     Schema = schema;
     IsValid = true;
 }
Пример #10
0
 public ArrayScope(ContextBase context, Scope parent, int initialDepth, JSchema schema)
     : base(context, parent, initialDepth, schema)
 {
     if (schema.UniqueItems)
         _uniqueArrayItems = new List<JToken>();
 }
 public void Initialize(ContextBase context, Scope parent, int initialDepth, JSchema schema)
 {
     Initialize(context, parent, initialDepth, ScopeType.Primitive);
     InitializeSchema(schema);
 }
 internal JsonValidatorContext(Scope scope, JSchema schema)
 {
     _scope = scope;
     _schema = schema;
 }
Пример #13
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:
                    ValidateConditionalChildren(token, value, depth);

                    if (!Schema._required.IsNullOrEmpty() && _requiredProperties.Count > 0)
                    {
                        //capture required properties at current depth as we may clear _requiredProperties later on
                        List <string> capturedRequiredProperties = _requiredProperties.ToList();
                        RaiseError($"Required properties are missing from object: {StringHelpers.Join(", ", capturedRequiredProperties)}.", ErrorType.Required, Schema, capturedRequiredProperties, 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 (HasDependencies())
                    {
                        foreach (string readProperty in _readProperties)
                        {
                            object         dependency         = null;
                            IList <string> requiredProperties = null;
                            if (Schema._dependencies?.TryGetValue(readProperty, out dependency) ?? false)
                            {
                                requiredProperties = dependency as IList <string>;
                                if (requiredProperties != null)
                                {
                                    ValidateDependantProperties(readProperty, requiredProperties);
                                }
                                else
                                {
                                    ValidateDependantSchema(readProperty);
                                }
                            }

                            if (Schema._dependentRequired?.TryGetValue(readProperty, out requiredProperties) ?? false)
                            {
                                ValidateDependantProperties(readProperty, requiredProperties);
                            }

                            if (Schema._dependentSchemas?.TryGetValue(readProperty, out _) ?? false)
                            {
                                ValidateDependantSchema(readProperty);
                            }
                        }
                    }

                    // Evaluate after dependency schemas have been validated
                    if (!_unevaluatedScopes.IsNullOrEmpty())
                    {
                        foreach (KeyValuePair <string, UnevaluatedContext> item in _unevaluatedScopes)
                        {
                            if (!item.Value.Evaluated)
                            {
                                IFormattable message = $"Property '{item.Key}' has not been successfully evaluated and the schema does not allow unevaluated properties.";
                                RaiseError(message, ErrorType.UnevaluatedProperties, Schema, item.Key, item.Value.SchemaScope.GetValidationErrors());
                            }
                        }
                    }

                    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 (HasDependencies())
                    {
                        _readProperties.Add(_currentPropertyName);
                    }

                    if (Schema._propertyNames != null)
                    {
                        CreateScopesAndEvaluateToken(token, value, depth, Schema._propertyNames);
                    }

                    if (!Schema.AllowAdditionalProperties)
                    {
                        if (!IsPropertyDefined(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);
                            }

                            if (ShouldValidateUnevaluated())
                            {
                                _unevaluatedScopes[_currentPropertyName] = Schema.UnevaluatedProperties != null
                                    ? new UnevaluatedContext(CreateScopesAndEvaluateToken(token, value, depth, Schema.UnevaluatedProperties, this, CreateConditionalContext()))
                                    : new UnevaluatedContext(AlwaysFalseScope.Instance);
                            }
                        }
                    }

                    if (JsonTokenHelpers.IsPrimitiveOrEndToken(token))
                    {
                        if (ShouldValidateUnevaluated() &&
                            _unevaluatedScopes.TryGetValue(_currentPropertyName, out UnevaluatedContext unevaluatedContext))
                        {
                            // Property is valid against unevaluatedProperties schema so no need to search further
                            bool isValid = unevaluatedContext.SchemaScope.IsValid;
                            unevaluatedContext.Evaluated = isValid;

                            if (!isValid)
                            {
                                for (int i = Context.Scopes.Count - 1; i >= 0; i--)
                                {
                                    Scope scope = Context.Scopes[i];
                                    if (scope.InitialDepth == InitialDepth + 1)
                                    {
                                        // Schema for a property
                                        if (scope.Parent != null && scope is SchemaScope schemaScope && schemaScope.IsValid)
                                        {
                                            unevaluatedContext.AddValidScope(schemaScope.Parent.Schema);
                                        }
                                    }
                                    else if (scope.InitialDepth == InitialDepth)
                                    {
                                        // Schema for the current object.
                                        // Need to check these for oneOf, allOf, etc.
                                        if (scope is SchemaScope schemaScope)
                                        {
                                            if (schemaScope.Schema._allowAdditionalProperties.GetValueOrDefault() ||
                                                schemaScope.Schema.AdditionalProperties != null ||
                                                schemaScope.Schema.AllowUnevaluatedProperties.GetValueOrDefault())
                                            {
                                                unevaluatedContext.AddValidScope(schemaScope.Schema);
                                            }
                                        }
                                    }
                                    else if (scope.InitialDepth < InitialDepth)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(false);
        }
Пример #14
0
 public PrimativeScope(ContextBase context, Scope parent, int initialDepth, JSchema schema)
     : base(context, parent, initialDepth, schema)
 {
 }