Beispiel #1
0
            public FileSegment GetLookupSegment(ImageSymbolImport symbolImport)
            {
                FileSegment segment;

                if (!_lookupSegments.TryGetValue(symbolImport, out segment))
                {
                    if (symbolImport.IsImportByOrdinal)
                    {
                        segment = DataSegment.CreateNativeInteger(symbolImport.Lookup, Is32Bit);
                    }
                    else if (symbolImport.HintName != null)
                    {
                        _nameTableBuffer.AddHintNameSegment(symbolImport.HintName);
                        segment = new PointerSegment(symbolImport.HintName, _offsetConverter, Is32Bit);
                    }
                    else
                    {
                        segment = DataSegment.CreateNativeInteger(0, Is32Bit);
                    }

                    _lookupSegments.Add(symbolImport, segment);
                    Segments.Add(segment);
                }
                return(segment);
            }
        /// <summary>
        /// Validates as a subschema.  To be called from within keywords.
        /// </summary>
        /// <param name="context">The validation context for this validation pass.</param>
        public void ValidateSubschema(ValidationContext context)
        {
            if (BoolValue.HasValue)
            {
                context.Log(() => $"Found {(BoolValue.Value ? "true" : "false")} schema: {BoolValue.Value.GetValidityString()}");
                context.IsValid        = BoolValue.Value;
                context.SchemaLocation = context.SchemaLocation.Combine(PointerSegment.Create($"${BoolValue}".ToLowerInvariant()));
                if (!context.IsValid)
                {
                    context.Message = "All values fail against the false schema";
                }
                return;
            }

            var metaSchemaUri = Keywords !.OfType <SchemaKeyword>().FirstOrDefault()?.Schema;
            var keywords      = context.Options.FilterKeywords(Keywords !, metaSchemaUri, context.Options.SchemaRegistry);

            ValidationContext?newContext = null;
            var overallResult            = true;

            foreach (var keyword in keywords.OrderBy(k => k.Priority()))
            {
                var previousContext = newContext;
                newContext = ValidationContext.From(context,
                                                    subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create(keyword.Keyword())));
                newContext.NavigatedByDirectRef = context.NavigatedByDirectRef;
                newContext.ParentContext        = context;
                newContext.LocalSchema          = this;
                newContext.ImportAnnotations(previousContext);
                if (context.HasNestedContexts)
                {
                    newContext.SiblingContexts.AddRange(context.NestedContexts);
                }
                keyword.Validate(newContext);
                context.IsNewDynamicScope |= newContext.IsNewDynamicScope;
                overallResult             &= newContext.IsValid;
                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
                if (!newContext.Ignore)
                {
                    context.NestedContexts.Add(newContext);
                }
            }

            if (context.IsNewDynamicScope)
            {
                context.Options.SchemaRegistry.ExitingUriScope();
            }

            context.IsValid = overallResult;
            if (context.IsValid)
            {
                context.ImportAnnotations(newContext);
            }
        }
        /// <summary>
        /// Validates as a subschema.  To be called from within keywords.
        /// </summary>
        /// <param name="context">The validation context for this validation pass.</param>
        public void ValidateSubschema(ValidationContext context)
        {
            if (BoolValue.HasValue)
            {
                context.IsValid        = BoolValue.Value;
                context.SchemaLocation = context.SchemaLocation.Combine(PointerSegment.Create($"[{BoolValue}]".ToLowerInvariant()));
                if (!context.IsValid)
                {
                    context.Message = "All values fail against the false schema";
                }
                return;
            }

            var metaSchemaUri = Keywords.OfType <SchemaKeyword>().FirstOrDefault()?.Schema;
            var keywords      = context.Options.FilterKeywords(Keywords, metaSchemaUri, context.Options.SchemaRegistry);

            ValidationContext newContext = null;
            var overallResult            = true;

            foreach (var keyword in keywords.OrderBy(k => k.Priority()))
            {
                var previousContext = newContext;
                newContext = ValidationContext.From(context,
                                                    subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create(keyword.Keyword())));
                newContext.CurrentAnchor ??= previousContext?.CurrentAnchor;
                newContext.ParentContext = context;
                newContext.LocalSchema   = this;
                newContext.ImportAnnotations(previousContext);
                if (context.HasNestedContexts)
                {
                    newContext.SiblingContexts.AddRange(context.NestedContexts);
                }
                newContext.RequiredInResult = keyword.IsApplicator();
                keyword.Validate(newContext);
                overallResult &= newContext.IsValid;
                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
                if (!newContext.Ignore)
                {
                    context.NestedContexts.Add(newContext);
                }
            }

            context.IsValid = overallResult;
            if (context.IsValid)
            {
                context.ImportAnnotations(newContext);
            }
        }
Beispiel #4
0
        private static IEnumerable <PathMatch> GetChildren(PathMatch match)
        {
            switch (match.Value.ValueKind)
            {
            case JsonValueKind.Object:
                yield return(match);

                foreach (var prop in match.Value.EnumerateObject())
                {
                    var newMatch = new PathMatch(prop.Value, match.Location.Combine(PointerSegment.Create(prop.Name)));
                    foreach (var child in GetChildren(newMatch))
                    {
                        yield return(child);
                    }
                }
                break;

            case JsonValueKind.Array:
                yield return(match);

                foreach (var(item, index) in match.Value.EnumerateArray().Select((item, i) => (item, i)))
                {
                    var newMatch = new PathMatch(item, match.Location.Combine(PointerSegment.Create($"{index}")));
                    foreach (var child in GetChildren(newMatch))
                    {
                        yield return(child);
                    }
                }
                break;

            case JsonValueKind.String:
            case JsonValueKind.Number:
            case JsonValueKind.True:
            case JsonValueKind.False:
            case JsonValueKind.Null:
                yield return(match);

                break;

            case JsonValueKind.Undefined:
            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Beispiel #5
0
        protected override IEnumerable <PathMatch> ProcessMatch(PathMatch match)
        {
            switch (match.Value.ValueKind)
            {
            case JsonValueKind.Array:
                var array = match.Value.EnumerateArray().ToArray();
                IEnumerable <int> indices;
                indices = _ranges?.OfType <IArrayIndexExpression>()
                          .SelectMany(r => r.GetIndices(match.Value))
                          .OrderBy(i => i)
                          .Where(i => 0 <= i && i < array.Length)
                          .Distinct() ??
                          Enumerable.Range(0, array.Length);
                foreach (var index in indices)
                {
                    yield return(new PathMatch(array[index], match.Location.Combine(PointerSegment.Create(index.ToString()))));
                }
                break;

            case JsonValueKind.Object:
                if (_ranges != null)
                {
                    var props = _ranges.OfType <IObjectIndexExpression>()
                                .SelectMany(r => r.GetProperties(match.Value))
                                .Distinct();
                    foreach (var prop in props)
                    {
                        if (!match.Value.TryGetProperty(prop, out var value))
                        {
                            continue;
                        }
                        yield return(new PathMatch(value, match.Location.Combine(PointerSegment.Create(prop))));
                    }
                }
                else
                {
                    foreach (var prop in match.Value.EnumerateObject())
                    {
                        yield return(new PathMatch(prop.Value, match.Location.Combine(PointerSegment.Create(prop.Name))));
                    }
                }
                break;
            }
        }
Beispiel #6
0
        protected override IEnumerable <PathMatch> ProcessMatch(PathMatch match)
        {
            if (_name == null)
            {
                switch (match.Value.ValueKind)
                {
                case JsonValueKind.Object:
                    foreach (var propPair in match.Value.EnumerateObject())
                    {
                        yield return(new PathMatch(propPair.Value, match.Location.Combine(PointerSegment.Create(propPair.Name))));
                    }
                    break;

                case JsonValueKind.Array:
                    foreach (var(value, index) in match.Value.EnumerateArray().Select((v, i) => (v, i)))
                    {
                        yield return(new PathMatch(value, match.Location.Combine(PointerSegment.Create(index.ToString()))));
                    }
                    break;
                }

                yield break;
            }

            if (match.Value.ValueKind != JsonValueKind.Object)
            {
                yield break;
            }

            if (!match.Value.TryGetProperty(_name, out var prop))
            {
                yield break;
            }

            yield return(new PathMatch(prop, match.Location.Combine(PointerSegment.Create(_name))));
        }
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            if (context.LocalInstance.ValueKind != JsonValueKind.Object)
            {
                context.IsValid = true;
                return;
            }

            var overallResult = true;

            foreach (var name in context.LocalInstance.EnumerateObject().Select(p => p.Name))
            {
                var instance   = name.AsJsonElement();
                var subContext = ValidationContext.From(context,
                                                        context.InstanceLocation.Combine(PointerSegment.Create($"{name}")),
                                                        instance);
                Schema.ValidateSubschema(subContext);
                overallResult &= subContext.IsValid;
                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
                context.NestedContexts.Add(subContext);
            }

            context.IsValid = overallResult;
        }
Beispiel #8
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            context.EnterKeyword(Name);
            if (context.LocalInstance.ValueKind != JsonValueKind.String)
            {
                context.WrongValueKind(context.LocalInstance.ValueKind);
                context.IsValid = true;
                return;
            }

            var subContext = ValidationContext.From(context,
                                                    subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create(Name)));

            Schema.ValidateSubschema(subContext);
            context.NestedContexts.Add(subContext);
            context.IsValid = subContext.IsValid;
            context.ConsolidateAnnotations();
            context.ExitKeyword(Name, context.IsValid);
        }
Beispiel #9
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            context.EnterKeyword(Name);
            if (context.LocalInstance.ValueKind != JsonValueKind.Object)
            {
                context.WrongValueKind(context.LocalInstance.ValueKind);
                context.IsValid = true;
                return;
            }

            context.Options.LogIndentLevel++;
            var overallResult = true;

            foreach (var name in context.LocalInstance.EnumerateObject().Select(p => p.Name))
            {
                context.Log(() => $"Validating property name '{name}'.");
                var instance   = name.AsJsonElement();
                var subContext = ValidationContext.From(context,
                                                        context.InstanceLocation.Combine(PointerSegment.Create($"{name}")),
                                                        instance);
                Schema.ValidateSubschema(subContext);
                overallResult &= subContext.IsValid;
                context.Log(() => $"Property name '{name}' {subContext.IsValid.GetValidityString()}.");
                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
                context.NestedContexts.Add(subContext);
            }
            context.Options.LogIndentLevel--;

            context.IsValid = overallResult;
            context.ExitKeyword(Name, context.IsValid);
        }
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            var validCount = 0;

            for (var i = 0; i < Schemas.Count; i++)
            {
                var schema     = Schemas[i];
                var subContext = ValidationContext.From(context, subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create($"{i}")));
                schema.ValidateSubschema(subContext);
                validCount += subContext.IsValid ? 1 : 0;
                if (validCount > 1 && context.ApplyOptimizations)
                {
                    break;
                }
                context.NestedContexts.Add(subContext);
            }

            context.ConsolidateAnnotations();
            context.IsValid = validCount == 1;
            if (!context.IsValid)
            {
                context.Message = $"Expected 1 matching subschema but found {validCount}";
            }
        }
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            var overallResult = false;

            for (var i = 0; i < Schemas.Count; i++)
            {
                var schema     = Schemas[i];
                var subContext = ValidationContext.From(context, subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create($"{i}")));
                schema.ValidateSubschema(subContext);
                overallResult |= subContext.IsValid;
                context.NestedContexts.Add(subContext);
            }

            if (overallResult)
            {
                context.ConsolidateAnnotations();
            }
            context.IsValid = overallResult;
        }
        protected override IEnumerable <PathMatch> ProcessMatch(PathMatch match)
        {
            switch (match.Value.ValueKind)
            {
            case JsonValueKind.Object:
                yield return(new PathMatch(match.Value.EnumerateObject().Count().AsJsonElement(), match.Location.Combine(PointerSegment.Create("$length"))));

                break;

            case JsonValueKind.Array:
                yield return(new PathMatch(match.Value.GetArrayLength().AsJsonElement(), match.Location.Combine(PointerSegment.Create("$length"))));

                break;
            }
        }
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            if (context.LocalInstance.ValueKind != JsonValueKind.Array)
            {
                context.IsValid = true;
                return;
            }

            bool overwriteAnnotation = !(context.TryGetAnnotation(Name) is bool);
            var  overallResult       = true;
            var  maxEvaluations      = Math.Min(ArraySchemas.Count, context.LocalInstance.GetArrayLength());

            for (int i = 0; i < maxEvaluations; i++)
            {
                var schema     = ArraySchemas[i];
                var item       = context.LocalInstance[i];
                var subContext = ValidationContext.From(context,
                                                        context.InstanceLocation.Combine(PointerSegment.Create($"{i}")),
                                                        item,
                                                        context.SchemaLocation.Combine(PointerSegment.Create($"{i}")));
                schema.ValidateSubschema(subContext);
                overallResult &= subContext.IsValid;
                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
                context.NestedContexts.Add(subContext);
            }

            if (overwriteAnnotation)
            {
                // TODO: add message
                if (overallResult)
                {
                    if (maxEvaluations == context.LocalInstance.GetArrayLength())
                    {
                        context.SetAnnotation(Name, true);
                    }
                    else
                    {
                        context.SetAnnotation(Name, maxEvaluations);
                    }
                }
            }

            context.IsValid = overallResult;
        }
Beispiel #14
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            context.EnterKeyword(Name);
            if (context.LocalInstance.ValueKind != JsonValueKind.Object)
            {
                context.WrongValueKind(context.LocalInstance.ValueKind);
                context.IsValid = true;
                return;
            }

            var overallResult       = true;
            var evaluatedProperties = new List <string>();

            foreach (var property in Requirements)
            {
                context.Log(() => $"Validating property '{property.Key}'.");
                var requirements = property.Value;
                var name         = property.Key;
                if (!context.LocalInstance.TryGetProperty(name, out _))
                {
                    context.Log(() => $"Property '{property.Key}' does not exist. Skipping.");
                    continue;
                }

                context.Options.LogIndentLevel++;
                if (requirements.Schema != null)
                {
                    context.Log(() => "Found schema requirement.");
                    var subContext = ValidationContext.From(context,
                                                            subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create($"{name}")));
                    requirements.Schema.ValidateSubschema(subContext);
                    overallResult &= subContext.IsValid;
                    context.NestedContexts.Add(subContext);
                    if (subContext.IsValid)
                    {
                        evaluatedProperties.Add(name);
                    }
                    context.Log(() => $"Property '{property.Key}' {subContext.IsValid.GetValidityString()}.");
                }
                else
                {
                    context.Log(() => "Found property list requirement.");
                    var missingDependencies = new List <string>();
                    foreach (var dependency in requirements.Requirements !)
                    {
                        if (context.LocalInstance.TryGetProperty(dependency, out _))
                        {
                            continue;
                        }

                        overallResult = false;
                        missingDependencies.Add(dependency);
                    }

                    if (!missingDependencies.Any())
                    {
                        evaluatedProperties.Add(name);
                    }
                    else
                    {
                        context.Log(() => $"Missing properties [{string.Join(",", missingDependencies.Select(x => $"'{x}'"))}].");
                        overallResult = false;
                    }
                }
                context.Options.LogIndentLevel--;

                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
            }

            context.IsValid = overallResult;
            if (!context.IsValid)
            {
                context.Message = $"The following properties failed their dependent schemas: {JsonSerializer.Serialize(evaluatedProperties)}";
            }
            context.ExitKeyword(Name, context.IsValid);
        }
Beispiel #15
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            context.EnterKeyword(Name);
            if (context.LocalInstance.ValueKind != JsonValueKind.Array)
            {
                context.WrongValueKind(context.LocalInstance.ValueKind);
                context.IsValid = true;
                return;
            }

            context.Options.LogIndentLevel++;
            var overallResult = true;
            var annotation    = context.TryGetAnnotation(ItemsKeyword.Name);

            if (annotation == null)
            {
                context.NotApplicable(() => $"No annotations from {ItemsKeyword.Name}.");
                context.IsValid = true;
                return;
            }
            context.Log(() => $"Annotation from {ItemsKeyword.Name}: {annotation}.");
            if (annotation is bool)
            {
                context.IsValid = true;
                context.ExitKeyword(Name, context.IsValid);
                return;
            }
            var startIndex = (int)annotation;

            for (int i = startIndex; i < context.LocalInstance.GetArrayLength(); i++)
            {
                context.Log(() => $"Validating item at index {i}.");
                var item       = context.LocalInstance[i];
                var subContext = ValidationContext.From(context,
                                                        context.InstanceLocation.Combine(PointerSegment.Create($"{i}")),
                                                        item);
                Schema.ValidateSubschema(subContext);
                overallResult &= subContext.IsValid;
                context.Log(() => $"Item at index {i} {subContext.IsValid.GetValidityString()}.");
                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
            }
            context.Options.LogIndentLevel--;

            if (overallResult)
            {
                context.SetAnnotation(Name, true);
            }
            context.IsValid = overallResult;
            context.ExitKeyword(Name, context.IsValid);
        }
Beispiel #16
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            context.EnterKeyword(Name);
            var validCount = 0;

            for (var i = 0; i < Schemas.Count; i++)
            {
                context.Log(() => $"Processing {Name}[{i}]...");
                var schema     = Schemas[i];
                var subContext = ValidationContext.From(context, subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create($"{i}")));
                schema.ValidateSubschema(subContext);
                validCount += subContext.IsValid ? 1 : 0;
                context.Log(() => $"{Name}[{i}] {subContext.IsValid.GetValidityString()}.");
                if (validCount > 1 && context.ApplyOptimizations)
                {
                    break;
                }
                context.NestedContexts.Add(subContext);
            }

            context.ConsolidateAnnotations();
            context.IsValid = validCount == 1;
            if (!context.IsValid)
            {
                context.Message = $"Expected 1 matching subschema but found {validCount}";
            }
            context.ExitKeyword(Name, context.IsValid);
        }
Beispiel #17
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            context.EnterKeyword(Name);
            var subContext = ValidationContext.From(context,
                                                    subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create(Name)));

            Schema.ValidateSubschema(subContext);
            context.NestedContexts.Add(subContext);
            context.IsValid = !subContext.IsValid;
            context.ConsolidateAnnotations();
            context.Options.LogIndentLevel++;
            context.Log(() => $"Subschema {subContext.IsValid.GetValidityString()}.");
            context.Options.LogIndentLevel--;
            context.ExitKeyword(Name, context.IsValid);
        }
Beispiel #18
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            if (context.LocalInstance.ValueKind != JsonValueKind.Object)
            {
                context.IsValid = true;
                return;
            }

            var overallResult       = true;
            var evaluatedProperties = new List <string>();

            foreach (var property in Requirements)
            {
                var requirements = property.Value;
                var name         = property.Key;
                if (!context.LocalInstance.TryGetProperty(name, out _))
                {
                    continue;
                }

                if (requirements.Schema != null)
                {
                    var subContext = ValidationContext.From(context,
                                                            subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create($"{name}")));
                    requirements.Schema.ValidateSubschema(subContext);
                    overallResult &= subContext.IsValid;
                    context.NestedContexts.Add(subContext);
                    if (subContext.IsValid)
                    {
                        evaluatedProperties.Add(name);
                    }
                }
                else
                {
                    var missingDependencies = new List <string>();
                    foreach (var dependency in requirements.Requirements !)
                    {
                        if (context.LocalInstance.TryGetProperty(dependency, out _))
                        {
                            continue;
                        }

                        overallResult = false;
                        missingDependencies.Add(dependency);
                    }

                    if (!missingDependencies.Any())
                    {
                        evaluatedProperties.Add(name);
                    }
                    else
                    {
                        overallResult = false;
                    }
                }

                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
            }

            context.IsValid = overallResult;
            if (!context.IsValid)
            {
                context.Message = $"The following properties failed their dependent schemas: {JsonSerializer.Serialize(evaluatedProperties)}";
            }
        }
Beispiel #19
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            context.EnterKeyword(Name);
            var overallResult = true;

            for (var i = 0; i < Schemas.Count; i++)
            {
                context.Log(() => $"Processing {Name}[{i}]...");
                var schema     = Schemas[i];
                var subContext = ValidationContext.From(context, subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create($"{i}")));
                schema.ValidateSubschema(subContext);
                overallResult &= subContext.IsValid;
                context.Log(() => $"{Name}[{i}] {subContext.IsValid.GetValidityString()}.");
                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
                context.NestedContexts.Add(subContext);
            }

            context.ConsolidateAnnotations();
            context.IsValid = overallResult;
            context.ExitKeyword(Name, context.IsValid);
        }
Beispiel #20
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            context.EnterKeyword(Name);
            if (context.LocalInstance.ValueKind != JsonValueKind.Object)
            {
                context.WrongValueKind(context.LocalInstance.ValueKind);
                context.IsValid = true;
                return;
            }

            context.Options.LogIndentLevel++;
            var overallResult       = true;
            var evaluatedProperties = new List <string>();
            var instanceProperties  = context.LocalInstance.EnumerateObject().ToList();

            foreach (var entry in Patterns)
            {
                var schema  = entry.Value;
                var pattern = entry.Key;
                foreach (var instanceProperty in instanceProperties.Where(p => pattern.IsMatch(p.Name)))
                {
                    context.Log(() => $"Validating property '{instanceProperty.Name}'.");
                    var subContext = ValidationContext.From(context,
                                                            context.InstanceLocation.Combine(PointerSegment.Create($"{instanceProperty.Name}")),
                                                            instanceProperty.Value,
                                                            context.SchemaLocation.Combine(PointerSegment.Create($"{pattern}")));
                    schema.ValidateSubschema(subContext);
                    overallResult &= subContext.IsValid;
                    context.Log(() => $"Property '{instanceProperty.Name}' {subContext.IsValid.GetValidityString()}.");
                    if (!overallResult && context.ApplyOptimizations)
                    {
                        break;
                    }
                    context.NestedContexts.Add(subContext);
                    evaluatedProperties.Add(instanceProperty.Name);
                }
            }
            context.Options.LogIndentLevel--;

            if (overallResult)
            {
                if (context.TryGetAnnotation(Name) is List <string> annotation)
                {
                    annotation.AddRange(evaluatedProperties);
                }
                else
                {
                    context.SetAnnotation(Name, evaluatedProperties);
                }
            }
            context.IsValid = overallResult;
            context.ExitKeyword(Name, context.IsValid);
        }
Beispiel #21
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            if (context.LocalInstance.ValueKind != JsonValueKind.Object)
            {
                context.IsValid = true;
                return;
            }

            var overallResult       = true;
            var annotation          = context.TryGetAnnotation(PropertiesKeyword.Name);
            var evaluatedProperties = (annotation as List <string>)?.ToList() ?? new List <string>();

            annotation = context.TryGetAnnotation(PatternPropertiesKeyword.Name);
            evaluatedProperties.AddRange(annotation as List <string> ?? Enumerable.Empty <string>());
            annotation = context.TryGetAnnotation(AdditionalPropertiesKeyword.Name);
            evaluatedProperties.AddRange(annotation as List <string> ?? Enumerable.Empty <string>());
            annotation = context.TryGetAnnotation(Name);
            evaluatedProperties.AddRange(annotation as List <string> ?? Enumerable.Empty <string>());
            var unevaluatedProperties = context.LocalInstance.EnumerateObject().Where(p => !evaluatedProperties.Contains(p.Name)).ToList();

            evaluatedProperties.Clear();
            foreach (var property in unevaluatedProperties)
            {
                if (!context.LocalInstance.TryGetProperty(property.Name, out var item))
                {
                    continue;
                }

                var subContext = ValidationContext.From(context,
                                                        context.InstanceLocation.Combine(PointerSegment.Create($"{property.Name}")),
                                                        item);
                Schema.ValidateSubschema(subContext);
                overallResult &= subContext.IsValid;
                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
                context.NestedContexts.Add(subContext);
                if (subContext.IsValid)
                {
                    evaluatedProperties.Add(property.Name);
                }
            }

            if (overallResult)
            {
                context.SetAnnotation(Name, evaluatedProperties);
            }
            // TODO: add message
            context.IsValid = overallResult;
            context.ConsolidateAnnotations();
        }
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            if (context.LocalInstance.ValueKind != JsonValueKind.Object)
            {
                context.IsValid = true;
                return;
            }

            var overallResult       = true;
            var evaluatedProperties = new List <string>();
            var instanceProperties  = context.LocalInstance.EnumerateObject().ToList();

            foreach (var entry in Patterns)
            {
                var schema  = entry.Value;
                var pattern = entry.Key;
                foreach (var instanceProperty in instanceProperties.Where(p => pattern.IsMatch(p.Name)))
                {
                    var subContext = ValidationContext.From(context,
                                                            context.InstanceLocation.Combine(PointerSegment.Create($"{instanceProperty.Name}")),
                                                            instanceProperty.Value,
                                                            context.SchemaLocation.Combine(PointerSegment.Create($"{pattern}")));
                    schema.ValidateSubschema(subContext);
                    overallResult &= subContext.IsValid;
                    if (!overallResult && context.ApplyOptimizations)
                    {
                        break;
                    }
                    context.NestedContexts.Add(subContext);
                    evaluatedProperties.Add(instanceProperty.Name);
                }
            }

            if (overallResult)
            {
                if (context.TryGetAnnotation(Name) is List <string> annotation)
                {
                    annotation.AddRange(evaluatedProperties);
                }
                else
                {
                    context.SetAnnotation(Name, evaluatedProperties);
                }
            }
            // TODO: add message
            context.IsValid = overallResult;
        }
Beispiel #23
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            var subContext = ValidationContext.From(context,
                                                    subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create(Name)));

            Schema.ValidateSubschema(subContext);
            context.NestedContexts.Add(subContext);
            context.IsValid = !subContext.IsValid;
            context.ConsolidateAnnotations();
        }
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            if (context.LocalInstance.ValueKind != JsonValueKind.Array)
            {
                context.IsValid = true;
                return;
            }

            var count = context.LocalInstance.GetArrayLength();

            for (int i = 0; i < count; i++)
            {
                var subContext = ValidationContext.From(context,
                                                        context.InstanceLocation.Combine(PointerSegment.Create($"{i}")),
                                                        context.LocalInstance[i]);
                Schema.ValidateSubschema(subContext);
                context.NestedContexts.Add(subContext);
            }

            var found = context.NestedContexts.Count(r => r.IsValid);
            var minContainsKeyword = context.LocalSchema.Keywords.OfType <MinContainsKeyword>().FirstOrDefault();

            if (minContainsKeyword != null && minContainsKeyword.Value == 0)
            {
                context.IsValid = true;
            }
            else
            {
                context.IsValid = found != 0;
            }
            if (context.IsValid)
            {
                context.SetAnnotation(Name, found);
            }
            else
            {
                context.Message = "Expected array to contain at least one item that matched the schema, but it did not";
            }
        }
            public FileSegment GetLookupSegment(ImageSymbolImport symbolImport)
            {
                FileSegment segment;
                if (!_lookupSegments.TryGetValue(symbolImport, out segment))
                {
                    if (symbolImport.IsImportByOrdinal)
                        segment = DataSegment.CreateNativeInteger(symbolImport.Lookup, Is32Bit);
                    else if (symbolImport.HintName != null)
                    {
                        _nameTableBuilder.AddHintNameSegment(symbolImport.HintName);
                        segment = new PointerSegment(symbolImport.HintName, _offsetConverter, Is32Bit);
                    }
                    else
                        segment = DataSegment.CreateNativeInteger(0, Is32Bit);

                    _lookupSegments.Add(symbolImport, segment);
                    Segments.Add(segment);
                }
                return segment;
            }
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            if (context.LocalInstance.ValueKind != JsonValueKind.Object)
            {
                context.IsValid = true;
                return;
            }

            var overallResult       = true;
            var evaluatedProperties = new List <string>();

            foreach (var property in Schemas)
            {
                var schema = property.Value;
                var name   = property.Key;
                if (!context.LocalInstance.TryGetProperty(name, out _))
                {
                    continue;
                }

                var subContext = ValidationContext.From(context,
                                                        subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create($"{name}")));
                schema.ValidateSubschema(subContext);
                overallResult &= subContext.IsValid;
                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
                context.NestedContexts.Add(subContext);
                if (subContext.IsValid)
                {
                    evaluatedProperties.Add(name);
                }
            }

            context.ConsolidateAnnotations();
            context.IsValid = overallResult;
            if (!context.IsValid)
            {
                context.Message = $"The following properties failed their dependent schemas: {JsonSerializer.Serialize(evaluatedProperties)}";
            }
        }
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            var overallResult = true;

            for (var i = 0; i < Schemas.Count; i++)
            {
                var schema     = Schemas[i];
                var subContext = ValidationContext.From(context, subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create($"{i}")));
                schema.ValidateSubschema(subContext);
                overallResult &= subContext.IsValid;
                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
                context.NestedContexts.Add(subContext);
            }

            context.ConsolidateAnnotations();
            context.IsValid = overallResult;
        }
Beispiel #28
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            if (context.LocalInstance.ValueKind != JsonValueKind.Array)
            {
                context.IsValid = true;
                return;
            }

            var overallResult = true;
            int startIndex    = 0;
            var annotation    = context.TryGetAnnotation(PrefixItemsKeyword.Name);

            if (annotation != null)
            {
                if (annotation is bool)                 // is only ever true or a number
                {
                    context.IsValid = true;
                    return;
                }
                startIndex = (int)annotation;
            }
            annotation = context.TryGetAnnotation(ItemsKeyword.Name);
            if (annotation != null)
            {
                if (annotation is bool)                 // is only ever true or a number
                {
                    context.IsValid = true;
                    return;
                }
                startIndex = (int)annotation;
            }
            annotation = context.TryGetAnnotation(AdditionalItemsKeyword.Name);
            if (annotation is bool)             // is only ever true
            {
                context.IsValid = true;
                return;
            }
            annotation = context.TryGetAnnotation(Name);
            if (annotation is bool)             // is only ever true
            {
                context.IsValid = true;
                return;
            }
            for (int i = startIndex; i < context.LocalInstance.GetArrayLength(); i++)
            {
                var item       = context.LocalInstance[i];
                var subContext = ValidationContext.From(context,
                                                        context.InstanceLocation.Combine(PointerSegment.Create($"{i}")),
                                                        item);
                Schema.ValidateSubschema(subContext);
                overallResult &= subContext.IsValid;
                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
                context.NestedContexts.Add(subContext);
            }

            if (overallResult)
            {
                context.SetAnnotation(Name, true);
            }
            context.IsValid = overallResult;
        }
Beispiel #29
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            context.EnterKeyword(Name);
            if (context.LocalInstance.ValueKind != JsonValueKind.Array)
            {
                context.WrongValueKind(context.LocalInstance.ValueKind);
                context.IsValid = true;
                return;
            }

            bool overwriteAnnotation = !(context.TryGetAnnotation(Name) is bool);
            var  overallResult       = true;

            if (SingleSchema != null)
            {
                context.Options.LogIndentLevel++;
                int startIndex;
                var annotation = context.TryGetAnnotation(PrefixItemsKeyword.Name);
                if (annotation == null)
                {
                    startIndex = 0;
                }
                else
                {
                    context.Log(() => $"Annotation from {PrefixItemsKeyword.Name}: {annotation}");
                    if (annotation is bool)
                    {
                        context.IsValid = true;
                        context.ExitKeyword(Name, context.IsValid);
                        return;
                    }

                    startIndex = (int)annotation;
                }

                for (int i = startIndex; i < context.LocalInstance.GetArrayLength(); i++)
                {
                    context.Log(() => $"Validating item at index {i}.");
                    var item       = context.LocalInstance[i];
                    var subContext = ValidationContext.From(context,
                                                            context.InstanceLocation.Combine(PointerSegment.Create($"{i}")),
                                                            item);
                    SingleSchema.ValidateSubschema(subContext);
                    context.CurrentUri ??= subContext.CurrentUri;
                    overallResult &= subContext.IsValid;
                    context.Log(() => $"Item at index {i} {subContext.IsValid.GetValidityString()}.");
                    if (!overallResult && context.ApplyOptimizations)
                    {
                        break;
                    }
                    context.NestedContexts.Add(subContext);
                }
                context.Options.LogIndentLevel--;

                if (overwriteAnnotation)
                {
                    // TODO: add message
                    if (overallResult)
                    {
                        context.SetAnnotation(Name, true);
                    }
                }
            }
            else             // array
            {
                if (context.Options.ValidatingAs == Draft.Draft202012)
                {
                    context.IsValid = false;
                    context.Message = $"Array form of {Name} is invalid for draft 2020-12 and later";
                    context.Log(() => $"Array form of {Name} is invalid for draft 2020-12 and later");
                    context.ExitKeyword(Name, context.IsValid);
                    return;
                }
                context.Options.LogIndentLevel++;
                var maxEvaluations = Math.Min(ArraySchemas !.Count, context.LocalInstance.GetArrayLength());
                for (int i = 0; i < maxEvaluations; i++)
                {
                    context.Log(() => $"Validating item at index {i}.");
                    var schema     = ArraySchemas[i];
                    var item       = context.LocalInstance[i];
                    var subContext = ValidationContext.From(context,
                                                            context.InstanceLocation.Combine(PointerSegment.Create($"{i}")),
                                                            item,
                                                            context.SchemaLocation.Combine(PointerSegment.Create($"{i}")));
                    schema.ValidateSubschema(subContext);
                    overallResult &= subContext.IsValid;
                    context.Log(() => $"Item at index {i} {subContext.IsValid.GetValidityString()}.");
                    if (!overallResult && context.ApplyOptimizations)
                    {
                        break;
                    }
                    context.NestedContexts.Add(subContext);
                }
                context.Options.LogIndentLevel--;

                if (overwriteAnnotation)
                {
                    // TODO: add message
                    if (overallResult)
                    {
                        if (maxEvaluations == context.LocalInstance.GetArrayLength())
                        {
                            context.SetAnnotation(Name, true);
                        }
                        else
                        {
                            context.SetAnnotation(Name, maxEvaluations);
                        }
                    }
                }
            }

            context.IsValid = overallResult;
            context.ExitKeyword(Name, context.IsValid);
        }
Beispiel #30
0
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            context.EnterKeyword(Name);
            if (context.LocalInstance.ValueKind != JsonValueKind.Object)
            {
                context.WrongValueKind(context.LocalInstance.ValueKind);
                context.IsValid = true;
                return;
            }

            var overallResult       = true;
            var evaluatedProperties = new List <string>();

            foreach (var property in Schemas)
            {
                context.Options.LogIndentLevel++;
                context.Log(() => $"Validating property '{property.Key}'.");
                var schema = property.Value;
                var name   = property.Key;
                if (!context.LocalInstance.TryGetProperty(name, out _))
                {
                    context.Log(() => $"Property '{property.Key}' does not exist. Skipping.");
                    continue;
                }

                var subContext = ValidationContext.From(context,
                                                        subschemaLocation: context.SchemaLocation.Combine(PointerSegment.Create($"{name}")));
                schema.ValidateSubschema(subContext);
                overallResult &= subContext.IsValid;
                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
                context.NestedContexts.Add(subContext);
                if (subContext.IsValid)
                {
                    evaluatedProperties.Add(name);
                }
                context.Log(() => $"Property '{property.Key}' {subContext.IsValid.GetValidityString()}.");
                context.Options.LogIndentLevel--;
            }

            context.ConsolidateAnnotations();
            context.IsValid = overallResult;
            if (!context.IsValid)
            {
                context.Message = $"The following properties failed their dependent schemas: {JsonSerializer.Serialize(evaluatedProperties)}";
            }
            context.ExitKeyword(Name, context.IsValid);
        }
        /// <summary>
        /// Provides validation for the keyword.
        /// </summary>
        /// <param name="context">Contextual details for the validation process.</param>
        public void Validate(ValidationContext context)
        {
            if (context.LocalInstance.ValueKind != JsonValueKind.Object)
            {
                context.IsValid = true;
                return;
            }

            var overallResult       = true;
            var evaluatedProperties = new List <string>();

            foreach (var property in Properties)
            {
                var schema = property.Value;
                var name   = property.Key;
                if (!context.LocalInstance.TryGetProperty(name, out var item))
                {
                    continue;
                }

                var subContext = ValidationContext.From(context,
                                                        context.InstanceLocation.Combine(PointerSegment.Create($"{name}")),
                                                        item,
                                                        context.SchemaLocation.Combine(PointerSegment.Create($"{name}")));
                schema.ValidateSubschema(subContext);
                overallResult &= subContext.IsValid;
                if (!overallResult && context.ApplyOptimizations)
                {
                    break;
                }
                context.NestedContexts.Add(subContext);
                if (subContext.IsValid)
                {
                    evaluatedProperties.Add(name);
                }
            }

            if (overallResult)
            {
                if (context.TryGetAnnotation(Name) is List <string> annotation)
                {
                    annotation.AddRange(evaluatedProperties);
                }
                else
                {
                    context.SetAnnotation(Name, evaluatedProperties);
                }
            }
            // TODO: add message
            context.IsValid = overallResult;
        }