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); } }
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); }
/// <summary> /// Gets all immediate subschemas for a keyword. /// </summary> /// <param name="keyword">The keyword.</param> /// <returns>An `IEnumerable<JsonSchema>`.</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>() });
/// <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; } }
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 }); }
/// <summary> /// Gets all immediate subschemas for a keyword. /// </summary> /// <param name="keyword">The keyword.</param> /// <returns>An `IEnumerable<JsonSchema>`.</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>() });
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); }
/// <summary> /// Adds a new keyword. /// </summary> /// <param name="keyword">The keyword to add.</param> public void Add(IJsonSchemaKeyword keyword) { _keywords[keyword.Keyword()] = keyword; }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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))); }
/// <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)); }
public bool ShouldExcludeChildErrors(IJsonSchemaKeyword keyword, SchemaValidationContext context) { return(context.RelativeLocation.IsChildOf(Location)); }
/// <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)); }
public bool ShouldExcludeChildErrors(IJsonSchemaKeyword keyword, SchemaValidationContext context) { return(Type.IsInstanceOfType(keyword)); }