|
| 1 | +The library supports V4, V6, V7, and V2019-09 JSON schema specifications. By default, V4 is used for backward compatibility. |
| 2 | + |
| 3 | +### For Users |
| 4 | + |
| 5 | +To create a draft V4 JsonSchemaFactory |
| 6 | + |
| 7 | +``` |
| 8 | +protected ObjectMapper mapper = new ObjectMapper(); |
| 9 | +protected JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance()).objectMapper(mapper).build(); |
| 10 | +``` |
| 11 | + |
| 12 | +The above code is exactly the same as before. Internally, it will default to the SpecVersion.VersionFlag.V4 as the parameter. |
| 13 | + |
| 14 | +To create a draft V6 JsonSchemaFactory |
| 15 | + |
| 16 | +``` |
| 17 | +protected ObjectMapper mapper = new ObjectMapper(); |
| 18 | +protected JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V6)).objectMapper(mapper).build(); |
| 19 | +
|
| 20 | +``` |
| 21 | + |
| 22 | +To create a draft V7 JsonSchemaFactory |
| 23 | + |
| 24 | +``` |
| 25 | +protected ObjectMapper mapper = new ObjectMapper(); |
| 26 | +protected JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build(); |
| 27 | +``` |
| 28 | + |
| 29 | +To create a draft 2019-09 JsonSchemaFactory |
| 30 | + |
| 31 | +``` |
| 32 | +protected ObjectMapper mapper = new ObjectMapper(); |
| 33 | +protected JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).objectMapper(mapper).build(); |
| 34 | +``` |
| 35 | + |
| 36 | +### For Developers |
| 37 | + |
| 38 | +#### SpecVersion |
| 39 | + |
| 40 | +A new class SpecVersion has been introduced to indicate which version of the specification is used when creating the JsonSchemaFactory. The SpecVersion has an enum and two methods to convert a long to an EnumSet or a set of VersionFlags to a long value. |
| 41 | + |
| 42 | +``` |
| 43 | + public enum VersionFlag { |
| 44 | +
|
| 45 | + V4(1<<0), |
| 46 | + V6(1<<1), |
| 47 | + V7(1<<2), |
| 48 | + V201909(1<<3); |
| 49 | +
|
| 50 | +``` |
| 51 | + |
| 52 | +In the long value, we are using 4 bits now as we are supporting 4 versions at the moment. |
| 53 | + |
| 54 | +V4 -> 0001 -> 1 |
| 55 | +V6 -> 0010 -> 2 |
| 56 | +V7 -> 0100 -> 4 |
| 57 | +V201909 -> 1000 -> 8 |
| 58 | + |
| 59 | +If we have a new version added, it should be |
| 60 | + |
| 61 | +V202009 -> 10000 -> 16 |
| 62 | + |
| 63 | +#### ValidatorTypeCode |
| 64 | + |
| 65 | +A new field versionCode is added to indicate which version the validator is supported. |
| 66 | + |
| 67 | +For most of the validators, the version code should be 15, which is 1111. This means the validator will be loaded for every version of the specification. |
| 68 | + |
| 69 | +For example. |
| 70 | + |
| 71 | +``` |
| 72 | + MAXIMUM("maximum", "1011", new MessageFormat("{0}: must have a maximum value of {1}"), MaximumValidator.class, 15), |
| 73 | +``` |
| 74 | + |
| 75 | +Since if-then-else was introduced in the V7, it only works for V7 and V2019-09 |
| 76 | + |
| 77 | +``` |
| 78 | + IF_THEN_ELSE("if", "1037", null, IfValidator.class, 12), // V7|V201909 1100 |
| 79 | +``` |
| 80 | + |
| 81 | +For exclusiveMaximum, it was introduced from V6 |
| 82 | + |
| 83 | +``` |
| 84 | + EXCLUSIVE_MAXIMUM("exclusiveMaximum", "1038", new MessageFormat("{0}: must have a exclusive maximum value of {1}"), ExclusiveMaximumValidator.class, 14), // V6|V7|V201909 |
| 85 | +``` |
| 86 | + |
| 87 | +The getNonFormatKeywords method is updated to accept a SpecVersion.VersionFlag so that only the keywords supported by the specification will be loaded. |
| 88 | + |
| 89 | +``` |
| 90 | + public static List<ValidatorTypeCode> getNonFormatKeywords(SpecVersion.VersionFlag versionFlag) { |
| 91 | + final List<ValidatorTypeCode> result = new ArrayList<ValidatorTypeCode>(); |
| 92 | + for (ValidatorTypeCode keyword: values()) { |
| 93 | + if (!FORMAT.equals(keyword) && specVersion.getVersionFlags(keyword.versionCode).contains(versionFlag)) { |
| 94 | + result.add(keyword); |
| 95 | + } |
| 96 | + } |
| 97 | + return result; |
| 98 | + } |
| 99 | +``` |
| 100 | + |
| 101 | +### JsonMetaSchema |
| 102 | + |
| 103 | +We have created four different static classes V4, V6, V7, and V201909 to build different JsonMetaSchema instances. |
| 104 | + |
| 105 | +For the BUILDIN_FORMATS, there is a common section, and each static class has its version-specific BUILDIN_FORMATS section. |
| 106 | + |
| 107 | + |
| 108 | +### JsonSchemaFactory |
| 109 | + |
| 110 | +The getInstance supports a parameter SpecVersion.VersionFlag to get the right instance of the JsonMetaShema to create the factory. If there is no parameter, then V4 is used by default. |
| 111 | + |
| 112 | +``` |
| 113 | + public static JsonSchemaFactory getInstance() { |
| 114 | + return getInstance(SpecVersion.VersionFlag.V4); |
| 115 | + } |
| 116 | +
|
| 117 | + public static JsonSchemaFactory getInstance(SpecVersion.VersionFlag versionFlag) { |
| 118 | + if(versionFlag == SpecVersion.VersionFlag.V201909) { |
| 119 | + JsonMetaSchema v201909 = JsonMetaSchema.getV201909(); |
| 120 | + return builder() |
| 121 | + .defaultMetaSchemaURI(v201909.getUri()) |
| 122 | + .addMetaSchema(v201909) |
| 123 | + .build(); |
| 124 | + } else if(versionFlag == SpecVersion.VersionFlag.V7) { |
| 125 | + JsonMetaSchema v7 = JsonMetaSchema.getV7(); |
| 126 | + return builder() |
| 127 | + .defaultMetaSchemaURI(v7.getUri()) |
| 128 | + .addMetaSchema(v7) |
| 129 | + .build(); |
| 130 | + } else if(versionFlag == SpecVersion.VersionFlag.V6) { |
| 131 | + JsonMetaSchema v6 = JsonMetaSchema.getV6(); |
| 132 | + return builder() |
| 133 | + .defaultMetaSchemaURI(v6.getUri()) |
| 134 | + .addMetaSchema(v6) |
| 135 | + .build(); |
| 136 | + } else if(versionFlag == SpecVersion.VersionFlag.V4) { |
| 137 | + JsonMetaSchema v4 = JsonMetaSchema.getV4(); |
| 138 | + return builder() |
| 139 | + .defaultMetaSchemaURI(v4.getUri()) |
| 140 | + .addMetaSchema(v4) |
| 141 | + .build(); |
| 142 | + } |
| 143 | + return null; |
| 144 | + } |
| 145 | +
|
| 146 | +``` |
| 147 | + |
| 148 | +### For Testers |
| 149 | + |
| 150 | +In the test resource folder, we have created and copied all draft version's test suite. They are located in draft4, draft6, draft7, and draft2019-09 folder. |
| 151 | + |
| 152 | +The existing JsonSchemaTest has been renamed to V4JsonSchemaTest, and the following test classes are added. |
| 153 | + |
| 154 | +``` |
| 155 | +V6JsonSchemaTest |
| 156 | +V7JsonSchemaTest |
| 157 | +V201909JsonSchemaTest |
| 158 | +``` |
| 159 | + |
| 160 | +These new test classes are not completed yet, and only some sample test cases are added. |
| 161 | + |
0 commit comments