1This document lists all the validators supported and gives users are guideline on how to use them. 2 3### if-then-else 4 5* Specification(s): draft7 6* Contributor(s): @andersonf 7* Reference: https://json-schema.org/understanding-json-schema/reference/conditionals.html 8* Issues and PRs: https://github.com/networknt/json-schema-validator/pull/206 9 10The `if`, `then` and `else` keywords allow the application of a subschema based on the outcome of another schema, much like the `if/then/else` constructs you’ve probably seen in traditional programming languages. 11 12If `if` is valid, `then` must also be valid (and `else` is ignored.) If `if` is invalid, `else` must also be valid (and `then` is ignored). 13 14For usage, please refer to the test cases at https://github.com/networknt/json-schema-validator/blob/master/src/test/resources/draft7/if-then-else.json 15 16### Custom Validators 17````java 18@Bean 19public JsonSchemaFactory mySchemaFactory() { 20 // base on JsonMetaSchema.V201909 copy code below 21 String URI = "https://json-schema.org/draft/2019-09/schema"; 22 String ID = "$id"; 23 List<Format> BUILTIN_FORMATS = new ArrayList<Format>(JsonMetaSchema.COMMON_BUILTIN_FORMATS); 24 25 JsonMetaSchema myJsonMetaSchema = new JsonMetaSchema.Builder(URI) 26 .idKeyword(ID) 27 .formats(BUILTIN_FORMATS) 28 .keywords(ValidatorTypeCode.getFormatKeywords(SpecVersion.VersionFlag.V201909)) 29 // keywords that may validly exist, but have no validation aspect to them 30 .keywords(Arrays.asList( 31 new NonValidationKeyword("$schema"), 32 new NonValidationKeyword("$id"), 33 new NonValidationKeyword("title"), 34 new NonValidationKeyword("description"), 35 new NonValidationKeyword("default"), 36 new NonValidationKeyword("definitions"), 37 new NonValidationKeyword("$defs") // newly added in 2018-09 release. 38 )) 39 // add your custom keyword 40 .keyword(new GroovyKeyword()) 41 .build(); 42 43 return new JsonSchemaFactory.Builder().defaultMetaSchemaIri(myJsonMetaSchema.getIri()) 44 .metaSchema(myJsonMetaSchema) 45 .build(); 46} 47 48public class GroovyKeyword extends AbstractKeyword { 49 private static final Logger logger = LoggerFactory.getLogger(GroovyKeyword.class); 50 51 public GroovyKeyword() { 52 super("groovy"); 53 } 54 55 @Override 56 public AbstractJsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) throws JsonSchemaException, Exception { 57 // you can read validator config here 58 String config = schemaNode.asText(); 59 return new AbstractJsonValidator(this.getValue()) { 60 @Override 61 public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) { 62 // you can do validate here 63 logger.info("config:{} path:{} node:{}", config, at, node); 64 65 return Collections.emptySet(); 66 } 67 }; 68 } 69} 70```` 71You can use GroovyKeyword like below: 72````json 73{ 74 "type": "object", 75 "properties": { 76 "someProperty": { 77 "type": "string", 78 "groovy": "SomeScript.groovy" 79 } 80 } 81} 82```` 83 84### Override Email/UUID/DateTime Validator 85 86In this library, if the format keyword is "email", "uuid", "date", "date-time", default validator provided by the library will be used. 87 88If you want to override this behavior, do as below. 89 90```java 91public JsonSchemaFactory mySchemaFactory() { 92 // base on JsonMetaSchema.V201909 copy code below 93 String URI = "https://json-schema.org/draft/2019-09/schema"; 94 String ID = "$id"; 95 96 JsonMetaSchema overrideEmailValidatorMetaSchema = new JsonMetaSchema.Builder(URI) 97 .idKeyword(ID) 98 // Override EmailValidator 99 .format(new PatternFormat("email", "^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$")) 100 .build(); 101 102 return new JsonSchemaFactory.Builder().defaultMetaSchemaIri(overrideEmailValidatorMetaSchema.getIri()) 103 .metaSchema(overrideEmailValidatorMetaSchema) 104 .build(); 105} 106``` 107