Exemplo n.º 1
0
        public static void IsEquivalentTo(JsonSchema expected, JsonSchema actual)
        {
            if (expected.Keywords == null)
            {
                Assert.Null(actual.Keywords);
                return;
            }

            foreach (IJsonSchemaKeyword expectedKeyword in expected.Keywords)
            {
                IJsonSchemaKeyword actualKeyword = actual.Keywords !.SingleOrDefault(kw => string.Equals(expectedKeyword.Keyword(), kw.Keyword()));
                if (actualKeyword == null)
                {
                    throw new ContainsException(expectedKeyword.Keyword(), actual.Keywords);
                }

                IsEquivalentTo(expectedKeyword, actualKeyword);
            }

            foreach (IJsonSchemaKeyword actualKeyword in actual.Keywords !)
            {
                IJsonSchemaKeyword expectedKeyword = expected.Keywords.SingleOrDefault(kw => string.Equals(actualKeyword.Keyword(), kw.Keyword()));
                if (expectedKeyword == null)
                {
                    throw new DoesNotContainException(expected.Keywords, actualKeyword.Keyword());
                }

                IsEquivalentTo(expectedKeyword, actualKeyword);
            }
        }
Exemplo n.º 2
0
        internal static IJsonSchemaKeyword Build(string keywordName, JsonValue json, JsonSerializer serializer)
        {
            if (!_cache.TryGetValue(keywordName, out var list) || !list.Any())
            {
                return(null);
            }

            IJsonSchemaKeyword keyword = null;
            var specials = list.Where(t => t.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IJsonSchemaKeywordPlus)))
                           .Select(t => (IJsonSchemaKeywordPlus)_resolver.Resolve(t))
                           .ToList();

            if (specials.Any())
            {
                keyword = specials.FirstOrDefault(k => k.Handles(json));
            }

            if (keyword == null)
            {
                keyword = (IJsonSchemaKeyword)_resolver.Resolve(list.First());
            }
            keyword.FromJson(json, serializer);

            return(keyword);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Gets all immediate subschemas for a keyword.
 /// </summary>
 /// <param name="keyword">The keyword.</param>
 /// <returns>An `IEnumerable&lt;JsonSchema&gt;`.</returns>
 public static IEnumerable <JsonSchema> GetSubschemas(this IJsonSchemaKeyword keyword)
 {
     return(keyword switch
     {
         ISchemaContainer container => new[] { container.Schema },
         ISchemaCollector collector => collector.Schemas,
         IKeyedSchemaCollector collector => collector.Schemas.Values,
         _ => Enumerable.Empty <JsonSchema>()
     });
Exemplo n.º 4
0
        /// <summary>
        /// Recursively analyzes all schemas for the provided keyword.
        /// </summary>
        /// <param name="path"><see cref="JsonPointer"/> representing the actual path to the keyword being provided.</param>
        /// <param name="keyword">The keyword to be analyzed.</param>
        protected void AnalyzeKeyword(JsonPointer path, IJsonSchemaKeyword keyword)
        {
            switch (keyword)
            {
            case AllOfKeyword item:
                for (var i = 0; i < item.Schemas.Count; i++)
                {
                    AnalyzeSchema(path.Combine(JsonPointer.Parse($"/[{i}]")), item.Schemas[i]);
                }

                break;

            case AnyOfKeyword item:
                for (var i = 0; i < item.Schemas.Count; i++)
                {
                    AnalyzeSchema(path.Combine(JsonPointer.Parse($"/[{i}]")), item.Schemas[i]);
                }

                break;

            case OneOfKeyword item:
                for (var i = 0; i < item.Schemas.Count; i++)
                {
                    AnalyzeSchema(path.Combine(JsonPointer.Parse($"/[{i}]")), item.Schemas[i]);
                }

                break;

            case DefinitionsKeyword item:
                foreach (var(name, definition) in item.Definitions)
                {
                    AnalyzeSchema(path.Combine(JsonPointer.Parse($"/{name}")), definition);
                }

                break;

            case DefsKeyword item:
                foreach (var(name, definition) in item.Definitions)
                {
                    AnalyzeSchema(path.Combine(JsonPointer.Parse($"/{name}")), definition);
                }

                break;

            case PropertiesKeyword item:
                foreach (var(name, definition) in item.Properties)
                {
                    AnalyzeSchema(path.Combine(JsonPointer.Parse($"/{name}")), definition);
                }

                break;

            case ISchemaContainer schemaContainer:
                AnalyzeSchema(path, schemaContainer.Schema);
                break;
            }
        }
Exemplo n.º 5
0
        private void ProcessKeyword(JsonPointer path, IJsonSchemaKeyword keyword, SchemaContext context)
        {
            switch (keyword)
            {
            // We only travese the actual schema and their referenced/used sub-schemas.
            // This means that there might be types defined in $def/definitions that is
            // not included in the generated model - which is fine since they are not in use.
            case SchemaKeyword:
            case IdKeyword:
            case TypeKeyword:
            case ConstKeyword:
            case XsdNamespacesKeyword:
            case XsdSchemaAttributesKeyword:
            case XsdUnhandledAttributesKeyword:
            case XsdUnhandledEnumAttributesKeyword:
            case XsdTypeKeyword:
            case XsdAttributeKeyword:
            case XsdAnyAttributeKeyword:
            case InfoKeyword:
            case RequiredKeyword:
            case EnumKeyword:
            case DefinitionsKeyword:
            case DefsKeyword:
                break;

            case RefKeyword k:
                ProcessRefKeyword(path, k, context);
                break;

            case OneOfKeyword k:
                ProcessOneOfKeyword(path, k, context);
                break;

            case AllOfKeyword k:
                ProcessAllOfKeyword(path, k, context);
                break;

            case AnyOfKeyword k:
                ProcessAnyOfKeyword(path, k, context);
                break;

            case PropertiesKeyword k:
                ProcessPropertiesKeyword(path, k, context);
                break;

            default:
                throw new NotImplementedException($"Keyword {keyword.Keyword()} not processed!");
            }

            OnKeywordProcessed(new KeywordProcessedEventArgs()
            {
                Path = path, Keyword = keyword
            });
        }
Exemplo n.º 6
0
 /// <summary>
 /// Gets all immediate subschemas for a keyword.
 /// </summary>
 /// <param name="keyword">The keyword.</param>
 /// <returns>An `IEnumerable&lt;JsonSchema&gt;`.</returns>
 public static IEnumerable <JsonSchema> GetSubschemas(this IJsonSchemaKeyword keyword)
 {
     return(keyword switch
     {
         // ReSharper disable once ConditionIsAlwaysTrueOrFalse
         ISchemaContainer container => container.Schema == null?Enumerable.Empty <JsonSchema>() : new[] { container.Schema },
         // ReSharper disable ConstantNullCoalescingCondition
         ISchemaCollector collector => collector.Schemas ?? Enumerable.Empty <JsonSchema>(),
         IKeyedSchemaCollector collector => collector.Schemas.Values ?? Enumerable.Empty <JsonSchema>(),
         // ReSharper restore ConstantNullCoalescingCondition
         _ => Enumerable.Empty <JsonSchema>()
     });
Exemplo n.º 7
0
        private IEnumerable <IJsonSchemaKeyword> NormalizeKeyword(JsonSchema schema, IJsonSchemaKeyword keyword)
        {
            var keywords = new List <IJsonSchemaKeyword>();

            if (keyword is AllOfKeyword allOf && HasSingleSubschema(allOf))
            {
                var subSchema = NormalizeSchema(allOf.Schemas[0]);

                if (schema.Keywords !.Count > 1 && subSchema.HasKeyword <RefKeyword>())
                {
                    // $ref is not allowed together with other keywords
                    keywords.Add(new AllOfKeyword(subSchema));
                }
        /// <summary>
        /// Gets the keyword priority.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <returns>The priority.</returns>
        public static long Priority(this IJsonSchemaKeyword keyword)
        {
            if (keyword == null)
            {
                throw new ArgumentNullException(nameof(keyword));
            }

            var priorityAttribute = keyword.GetType().GetCustomAttribute <SchemaPriorityAttribute>();

            if (priorityAttribute == null)
            {
                return(0);
            }

            return(priorityAttribute.ActualPriority);
        }
        /// <summary>
        /// Gets the keyword string.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <returns>The keyword string.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="keyword"/> is null.</exception>
        /// <exception cref="InvalidOperationException">The keyword does not carry the <see cref="SchemaKeywordAttribute"/>.</exception>
        public static string Keyword(this IJsonSchemaKeyword keyword)
        {
            if (keyword == null)
            {
                throw new ArgumentNullException(nameof(keyword));
            }

            var keywordType = keyword.GetType();

            if (!_attributes.TryGetValue(keywordType, out var name))
            {
                name = keywordType.GetCustomAttribute <SchemaKeywordAttribute>()?.Name;
                if (name == null)
                {
                    throw new InvalidOperationException($"Type {keywordType.Name} must be decorated with {nameof(SchemaKeywordAttribute)}");
                }

                _attributes[keywordType] = name;
            }

            return(name);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Adds a new keyword.
 /// </summary>
 /// <param name="keyword">The keyword to add.</param>
 public void Add(IJsonSchemaKeyword keyword)
 {
     _keywords[keyword.Keyword()] = keyword;
 }
Exemplo n.º 11
0
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as ContentMediaTypeKeyword));
 }
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <param name="other">An object to compare with this object.</param>
 /// <returns>true if the current object is equal to the <paramref name="other">other</paramref> parameter; otherwise, false.</returns>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as UnevaluatedPropertiesKeyword));
 }
Exemplo n.º 13
0
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as DefaultKeyword));
 }
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as ExclusiveMaximumDraft04Keyword));
 }
Exemplo n.º 15
0
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as DefinitionsKeyword));
 }
Exemplo n.º 16
0
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as ExamplesKeyword));
 }
Exemplo n.º 17
0
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as AllOfKeyword));
 }
Exemplo n.º 18
0
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as CommentKeyword));
 }
Exemplo n.º 19
0
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as DependenciesKeyword));
 }
Exemplo n.º 20
0
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <param name="other">An object to compare with this object.</param>
 /// <returns>true if the current object is equal to the <paramref name="other">other</paramref> parameter; otherwise, false.</returns>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as AdditionalPropertiesKeyword));
 }
Exemplo n.º 21
0
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as FormatKeyword));
 }
Exemplo n.º 22
0
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as PropertyNamesKeyword));
 }
Exemplo n.º 23
0
 /// <summary>
 /// Gets whether the keyword is an applicator (carries the <see cref="ApplicatorAttribute"/> attribute).
 /// </summary>
 /// <param name="keyword">The keyword.</param>
 /// <returns><code>true</code> if the keyword is an applicator; <code>false</code> otherwise.</returns>
 public static bool IsApplicator(this IJsonSchemaKeyword keyword)
 {
     return(keyword.GetType().GetCustomAttribute <ApplicatorAttribute>() != null);
 }
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as RecursiveAnchorKeyword));
 }
Exemplo n.º 25
0
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as MultipleOfKeyword));
 }
Exemplo n.º 26
0
 /// <summary>
 /// Checks whether child errors and annotations should be reported.  Should be called during validation.
 /// </summary>
 /// <param name="keyword">The keyword currently executing validation.</param>
 /// <param name="context">The validation context.</param>
 /// <returns>`true` if child errors should be included in the error; `false` if the result should only
 /// contain the immediate error.</returns>
 public bool ShouldReportChildErrors(IJsonSchemaKeyword keyword, SchemaValidationContext context)
 {
     return(!_errorCollectionConditions.Any(c => c.ShouldExcludeChildErrors(keyword, context)));
 }
Exemplo n.º 27
0
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as MinimumKeyword));
 }
Exemplo n.º 28
0
 public bool ShouldExcludeChildErrors(IJsonSchemaKeyword keyword, SchemaValidationContext context)
 {
     return(context.RelativeLocation.IsChildOf(Location));
 }
Exemplo n.º 29
0
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(IJsonSchemaKeyword other)
 {
     return(Equals(other as PatternPropertiesKeyword));
 }
Exemplo n.º 30
0
 public bool ShouldExcludeChildErrors(IJsonSchemaKeyword keyword, SchemaValidationContext context)
 {
     return(Type.IsInstanceOfType(keyword));
 }