-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadWriteContextValidation.java
68 lines (59 loc) · 2.49 KB
/
ReadWriteContextValidation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.github.erosb.jsonsKema.examples;
import com.github.erosb.jsonsKema.JsonParser;
import com.github.erosb.jsonsKema.JsonValue;
import com.github.erosb.jsonsKema.ReadWriteContext;
import com.github.erosb.jsonsKema.Schema;
import com.github.erosb.jsonsKema.SchemaLoader;
import com.github.erosb.jsonsKema.Validator;
import com.github.erosb.jsonsKema.ValidatorConfig;
public class ReadWriteContextValidation {
public static void main(String[] args) {
JsonValue schemaJson = new JsonParser("""
{
"type": "object",
"properties": {
"id": {
"readOnly": true,
"type": "number",
"minimum": 0
},
"name": {
"type": "string"
},
"password": {
"type": "string",
"writeOnly": true
}
}
}
""").parse();
// map the raw json to a reusable Schema instance
Schema schema = new SchemaLoader(schemaJson).load();
// creating write-context validator, it will report validation failures
// for read-only properties that are included in the instance
var writeContextValidator = Validator.create(schema, ValidatorConfig.builder()
.readWriteContext(ReadWriteContext.WRITE)
.build()
);
// creating the json document which will be validated (first in write context, then in read context)
JsonValue instance = new JsonParser("""
{
"id": 1,
"name": "John Doe",
"password": "v3ry_s3cur3"
}
""").parse();
var writeContextFailure = writeContextValidator.validate(instance);
// prints failure because the read-only property "id" is present in write context
System.out.println(writeContextFailure);
// creating read-context validator, it will report validation failures
// for write-only properties that are included in the instance
var readContextValidator = Validator.create(schema, ValidatorConfig.builder()
.readWriteContext(ReadWriteContext.READ)
.build()
);
var readContextFailure = readContextValidator.validate(instance);
// prints failure because the write-only property "password" is present in read context
System.out.println(readContextFailure);
}
}