Skip to content

Commit daa6b47

Browse files
authored
Create 7-validate-json-with-domains.sql
1 parent 786cc0b commit daa6b47

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
REM script for 23c: 7-validate-json-with-domains.sql
2+
3+
-- create a domain p_recorddomain to validate a given JSON schema
4+
5+
drop domain p_recorddomain;
6+
7+
create domain p_recorddomain AS JSON VALIDATE USING '{
8+
"type": "object",
9+
"properties": {
10+
"first_name": { "type": "string" },
11+
"last_name": { "type": "string" },
12+
"birthday": { "type": "string", "format": "date" },
13+
"address": {
14+
"type": "object",
15+
"properties": {
16+
"street_address": { "type": "string" },
17+
"city": { "type": "string" },
18+
"state": { "type": "string" },
19+
"country": { "type" : "string" }
20+
}
21+
}
22+
}
23+
}' ;
24+
25+
26+
-- a check constraint is automatically created.
27+
28+
set long 1000
29+
col name format a20
30+
select name, generated, constraint_type, search_condition
31+
from user_domain_constraints where domain_name like 'P_RECORD%';
32+
33+
34+
-- create a table person_json
35+
36+
drop table person;
37+
38+
create table person (id NUMBER,
39+
p_record JSON DOMAIN p_recorddomain);
40+
41+
42+
-- insert valid and invalid data
43+
44+
insert into person values (1, '{
45+
"first_name": "George",
46+
"last_name": "Washington",
47+
"birthday": "1732-02-22",
48+
"address": {
49+
"street_address": "3200 Mount Vernon Memorial Highway",
50+
"city": "Mount Vernon",
51+
"state": "Virginia",
52+
"country": "United States"
53+
}
54+
}');
55+
56+
-- try to insert invalid data
57+
58+
insert into person values (2, '{
59+
"name": "George Washington",
60+
"birthday": "February 22, 1732",
61+
"address": "Mount Vernon, Virginia, United States"
62+
}');
63+
64+
65+
66+

0 commit comments

Comments
 (0)