/// <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>
 /// 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);
 }
Beispiel #4
0
        public static void IsEquivalentTo(IJsonSchemaKeyword expected, IJsonSchemaKeyword actual)
        {
            if (expected == null)
            {
                Assert.Null(actual);
                return;
            }

            Assert.NotNull(actual);

            Assert.IsType(expected.GetType(), actual);
            Assert.Equal(expected.Keyword(), actual.Keyword());

            switch (expected)
            {
            case AdditionalItemsKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (AdditionalItemsKeyword)actual);
                break;

            case AdditionalPropertiesKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (AdditionalPropertiesKeyword)actual);
                break;

            case AllOfKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (AllOfKeyword)actual);
                break;

            case AnchorKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (AnchorKeyword)actual);
                break;

            case AnyOfKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (AnyOfKeyword)actual);
                break;

            case CommentKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (CommentKeyword)actual);
                break;

            case ConstKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (ConstKeyword)actual);
                break;

            case ContainsKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (ContainsKeyword)actual);
                break;

            case ContentMediaEncodingKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (ContentMediaEncodingKeyword)actual);
                break;

            case ContentMediaTypeKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (ContentMediaTypeKeyword)actual);
                break;

            case ContentSchemaKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (ContentSchemaKeyword)actual);
                break;

            case DefaultKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (DefaultKeyword)actual);
                break;

            case DefinitionsKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (DefinitionsKeyword)actual);
                break;

            case DefsKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (DefsKeyword)actual);
                break;

            case DependenciesKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (DependenciesKeyword)actual);
                break;

            case DependentRequiredKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (DependentRequiredKeyword)actual);
                break;

            case DependentSchemasKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (DependentSchemasKeyword)actual);
                break;

            case DeprecatedKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (DeprecatedKeyword)actual);
                break;

            case DescriptionKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (DescriptionKeyword)actual);
                break;

            case DynamicAnchorKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (DynamicAnchorKeyword)actual);
                break;

            case DynamicRefKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (DynamicRefKeyword)actual);
                break;

            case ElseKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (ElseKeyword)actual);
                break;

            case EnumKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (EnumKeyword)actual);
                break;

            case ExamplesKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (ExamplesKeyword)actual);
                break;

            case ExclusiveMaximumKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (ExclusiveMaximumKeyword)actual);
                break;

            case ExclusiveMinimumKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (ExclusiveMinimumKeyword)actual);
                break;

            case FormatKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (FormatKeyword)actual);
                break;

            case IdKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (IdKeyword)actual);
                break;

            case IfKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (IfKeyword)actual);
                break;

            case ItemsKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (ItemsKeyword)actual);
                break;

            case MaxContainsKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (MaxContainsKeyword)actual);
                break;

            case MaximumKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (MaximumKeyword)actual);
                break;

            case MaxItemsKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (MaxItemsKeyword)actual);
                break;

            case MaxLengthKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (MaxLengthKeyword)actual);
                break;

            case MaxPropertiesKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (MaxPropertiesKeyword)actual);
                break;

            case MinContainsKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (MinContainsKeyword)actual);
                break;

            case MinimumKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (MinimumKeyword)actual);
                break;

            case MinItemsKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (MinItemsKeyword)actual);
                break;

            case MinLengthKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (MinLengthKeyword)actual);
                break;

            case MinPropertiesKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (MinPropertiesKeyword)actual);
                break;

            case MultipleOfKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (MultipleOfKeyword)actual);
                break;

            case NotKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (NotKeyword)actual);
                break;

            case OneOfKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (OneOfKeyword)actual);
                break;

            case PatternKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (PatternKeyword)actual);
                break;

            case PatternPropertiesKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (PatternPropertiesKeyword)actual);
                break;

            case PrefixItemsKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (PrefixItemsKeyword)actual);
                break;

            case PropertiesKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (PropertiesKeyword)actual);
                break;

            case PropertyNamesKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (PropertyNamesKeyword)actual);
                break;

            case ReadOnlyKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (ReadOnlyKeyword)actual);
                break;

            case RecursiveAnchorKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (RecursiveAnchorKeyword)actual);
                break;

            case RecursiveRefKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (RecursiveRefKeyword)actual);
                break;

            case RefKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (RefKeyword)actual);
                break;

            case RequiredKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (RequiredKeyword)actual);
                break;

            case SchemaKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (SchemaKeyword)actual);
                break;

            case ThenKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (ThenKeyword)actual);
                break;

            case TitleKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (TitleKeyword)actual);
                break;

            case TypeKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (TypeKeyword)actual);
                break;

            case UnevaluatedItemsKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (UnevaluatedItemsKeyword)actual);
                break;

            case UnevaluatedPropertiesKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (UnevaluatedPropertiesKeyword)actual);
                break;

            case UniqueItemsKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (UniqueItemsKeyword)actual);
                break;

            case VocabularyKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (VocabularyKeyword)actual);
                break;

            case WriteOnlyKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (WriteOnlyKeyword)actual);
                break;

            case InfoKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (InfoKeyword)actual);
                break;

            case XsdAnyAttributeKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (XsdAnyAttributeKeyword)actual);
                break;

            case XsdAnyKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (XsdAnyKeyword)actual);
                break;

            case XsdAttributeKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (XsdAttributeKeyword)actual);
                break;

            case XsdRestrictionsKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (XsdRestrictionsKeyword)actual);
                break;

            case XsdStructureKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (XsdStructureKeyword)actual);
                break;

            case XsdTypeKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (XsdTypeKeyword)actual);
                break;

            case XsdUnhandledAttributesKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (XsdUnhandledAttributesKeyword)actual);
                break;

            case XsdNamespacesKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (XsdNamespacesKeyword)actual);
                break;

            case XsdSchemaAttributesKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (XsdSchemaAttributesKeyword)actual);
                break;

            case XsdUnhandledEnumAttributesKeyword expectedKeyword:
                KeywordEqual(expectedKeyword, (XsdUnhandledEnumAttributesKeyword)actual);
                break;

            default:
                throw new ArgumentOutOfRangeException(expected.GetType().Name, "Unknown Json Schema Keyword");
            }
        }