• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.networknt.schema;
17 
18 import java.util.LinkedHashSet;
19 import java.util.Objects;
20 import java.util.Set;
21 
22 /**
23  * Represents a vocabulary in meta-schema.
24  * <p>
25  * This contains the IRI and the keywords in the vocabulary.
26  */
27 public class Vocabulary {
28 
29     // 2019-09
30     public static final Vocabulary V201909_CORE = new Vocabulary("https://json-schema.org/draft/2019-09/vocab/core",
31             new NonValidationKeyword("$id"), new NonValidationKeyword("$schema"), new NonValidationKeyword("$anchor"),
32             ValidatorTypeCode.REF, ValidatorTypeCode.RECURSIVE_REF, new NonValidationKeyword("$recursiveAnchor"),
33             new NonValidationKeyword("$vocabulary"), new NonValidationKeyword("$comment"),
34             new NonValidationKeyword("$defs"));
35     public static final Vocabulary V201909_APPLICATOR = new Vocabulary(
36             "https://json-schema.org/draft/2019-09/vocab/applicator", new NonValidationKeyword("additionalItems"),
37             ValidatorTypeCode.UNEVALUATED_ITEMS, ValidatorTypeCode.ITEMS, ValidatorTypeCode.CONTAINS,
38             ValidatorTypeCode.ADDITIONAL_PROPERTIES, ValidatorTypeCode.UNEVALUATED_PROPERTIES,
39             ValidatorTypeCode.PROPERTIES, ValidatorTypeCode.PATTERN_PROPERTIES, ValidatorTypeCode.DEPENDENT_SCHEMAS,
40             ValidatorTypeCode.PROPERTYNAMES, ValidatorTypeCode.IF_THEN_ELSE, new NonValidationKeyword("then"),
41             new NonValidationKeyword("else"), ValidatorTypeCode.ALL_OF, ValidatorTypeCode.ANY_OF,
42             ValidatorTypeCode.ONE_OF, ValidatorTypeCode.NOT);
43     public static final Vocabulary V201909_VALIDATION = new Vocabulary(
44             "https://json-schema.org/draft/2019-09/vocab/validation", ValidatorTypeCode.MULTIPLE_OF,
45             ValidatorTypeCode.MAXIMUM, ValidatorTypeCode.EXCLUSIVE_MAXIMUM, ValidatorTypeCode.MINIMUM,
46             ValidatorTypeCode.EXCLUSIVE_MINIMUM, ValidatorTypeCode.MAX_LENGTH, ValidatorTypeCode.MIN_LENGTH,
47             ValidatorTypeCode.PATTERN, ValidatorTypeCode.MAX_ITEMS, ValidatorTypeCode.MIN_ITEMS,
48             ValidatorTypeCode.UNIQUE_ITEMS, ValidatorTypeCode.MAX_CONTAINS, ValidatorTypeCode.MIN_CONTAINS,
49             ValidatorTypeCode.MAX_PROPERTIES, ValidatorTypeCode.MIN_PROPERTIES, ValidatorTypeCode.REQUIRED,
50             ValidatorTypeCode.DEPENDENT_REQUIRED, ValidatorTypeCode.CONST, ValidatorTypeCode.ENUM,
51             ValidatorTypeCode.TYPE);
52     public static final Vocabulary V201909_META_DATA = new Vocabulary(
53             "https://json-schema.org/draft/2019-09/vocab/meta-data", new AnnotationKeyword("title"),
54             new AnnotationKeyword("description"), new AnnotationKeyword("default"), new AnnotationKeyword("deprecated"),
55             ValidatorTypeCode.READ_ONLY, ValidatorTypeCode.WRITE_ONLY, new AnnotationKeyword("examples"));
56     public static final Vocabulary V201909_FORMAT = new Vocabulary("https://json-schema.org/draft/2019-09/vocab/format",
57             ValidatorTypeCode.FORMAT);
58     public static final Vocabulary V201909_CONTENT = new Vocabulary(
59             "https://json-schema.org/draft/2019-09/vocab/content", new AnnotationKeyword("contentMediaType"),
60             new AnnotationKeyword("contentEncoding"), new AnnotationKeyword("contentSchema"));
61 
62     // 2020-12
63     public static final Vocabulary V202012_CORE = new Vocabulary("https://json-schema.org/draft/2020-12/vocab/core",
64             new NonValidationKeyword("$id"), new NonValidationKeyword("$schema"), ValidatorTypeCode.REF,
65             new NonValidationKeyword("$anchor"), ValidatorTypeCode.DYNAMIC_REF,
66             new NonValidationKeyword("$dynamicAnchor"), new NonValidationKeyword("$vocabulary"),
67             new NonValidationKeyword("$comment"), new NonValidationKeyword("$defs"));
68     public static final Vocabulary V202012_APPLICATOR = new Vocabulary(
69             "https://json-schema.org/draft/2020-12/vocab/applicator", ValidatorTypeCode.PREFIX_ITEMS,
70             ValidatorTypeCode.ITEMS_202012, ValidatorTypeCode.CONTAINS, ValidatorTypeCode.ADDITIONAL_PROPERTIES,
71             ValidatorTypeCode.PROPERTIES, ValidatorTypeCode.PATTERN_PROPERTIES, ValidatorTypeCode.DEPENDENT_SCHEMAS,
72             ValidatorTypeCode.PROPERTYNAMES, ValidatorTypeCode.IF_THEN_ELSE, new NonValidationKeyword("then"),
73             new NonValidationKeyword("else"), ValidatorTypeCode.ALL_OF, ValidatorTypeCode.ANY_OF,
74             ValidatorTypeCode.ONE_OF, ValidatorTypeCode.NOT);
75     public static final Vocabulary V202012_UNEVALUATED = new Vocabulary(
76             "https://json-schema.org/draft/2020-12/vocab/unevaluated", ValidatorTypeCode.UNEVALUATED_ITEMS,
77             ValidatorTypeCode.UNEVALUATED_PROPERTIES);
78     public static final Vocabulary V202012_VALIDATION = new Vocabulary(
79             "https://json-schema.org/draft/2020-12/vocab/validation", ValidatorTypeCode.TYPE, ValidatorTypeCode.CONST,
80             ValidatorTypeCode.ENUM, ValidatorTypeCode.MULTIPLE_OF, ValidatorTypeCode.MAXIMUM,
81             ValidatorTypeCode.EXCLUSIVE_MAXIMUM, ValidatorTypeCode.MINIMUM, ValidatorTypeCode.EXCLUSIVE_MINIMUM,
82             ValidatorTypeCode.MAX_LENGTH, ValidatorTypeCode.MIN_LENGTH, ValidatorTypeCode.PATTERN,
83             ValidatorTypeCode.MAX_ITEMS, ValidatorTypeCode.MIN_ITEMS, ValidatorTypeCode.UNIQUE_ITEMS,
84             ValidatorTypeCode.MAX_CONTAINS, ValidatorTypeCode.MIN_CONTAINS, ValidatorTypeCode.MAX_PROPERTIES,
85             ValidatorTypeCode.MIN_PROPERTIES, ValidatorTypeCode.REQUIRED, ValidatorTypeCode.DEPENDENT_REQUIRED);
86     public static final Vocabulary V202012_META_DATA = new Vocabulary(
87             "https://json-schema.org/draft/2020-12/vocab/meta-data", new AnnotationKeyword("title"),
88             new AnnotationKeyword("description"), new AnnotationKeyword("default"), new AnnotationKeyword("deprecated"),
89             ValidatorTypeCode.READ_ONLY, ValidatorTypeCode.WRITE_ONLY, new AnnotationKeyword("examples"));
90     public static final Vocabulary V202012_FORMAT_ANNOTATION = new Vocabulary(
91             "https://json-schema.org/draft/2020-12/vocab/format-annotation", ValidatorTypeCode.FORMAT);
92     public static final Vocabulary V202012_FORMAT_ASSERTION = new Vocabulary(
93             "https://json-schema.org/draft/2020-12/vocab/format-assertion", ValidatorTypeCode.FORMAT);
94     public static final Vocabulary V202012_CONTENT = new Vocabulary(
95             "https://json-schema.org/draft/2020-12/vocab/content", new AnnotationKeyword("contentEncoding"),
96             new AnnotationKeyword("contentMediaType"), new AnnotationKeyword("contentSchema"));
97 
98     private final String iri;
99     private final Set<Keyword> keywords;
100 
101     /**
102      * Constructor.
103      *
104      * @param iri       the iri
105      * @param keywords the keywords
106      */
Vocabulary(String iri, Keyword... keywords)107     public Vocabulary(String iri, Keyword... keywords) {
108         this.iri = iri;
109         this.keywords = new LinkedHashSet<>();
110         for (Keyword keyword : keywords) {
111             this.keywords.add(keyword);
112         }
113     }
114 
115     /**
116      * The iri of the vocabulary.
117      *
118      * @return the iri
119      */
getIri()120     public String getIri() {
121         return iri;
122     }
123 
124     /**
125      * The keywords in the vocabulary.
126      *
127      * @return the keywords
128      */
getKeywords()129     public Set<Keyword> getKeywords() {
130         return keywords;
131     }
132 
133     @Override
hashCode()134     public int hashCode() {
135         return Objects.hash(iri, keywords);
136     }
137 
138     @Override
equals(Object obj)139     public boolean equals(Object obj) {
140         if (this == obj)
141             return true;
142         if (obj == null)
143             return false;
144         if (getClass() != obj.getClass())
145             return false;
146         Vocabulary other = (Vocabulary) obj;
147         return Objects.equals(iri, other.iri) && Objects.equals(keywords, other.keywords);
148     }
149 
150     @Override
toString()151     public String toString() {
152         return "Vocabulary [iri=" + iri + ", keywords=" + keywords + "]";
153     }
154 
155 }
156