| 1 |
# See that SQL NULL is handled properly |
| 2 |
SELECT JSON_SCHEMA_VALID(NULL, NULL); |
| 3 |
JSON_SCHEMA_VALID(NULL, NULL) |
| 4 |
NULL |
| 5 |
SELECT JSON_SCHEMA_VALID('{}', NULL); |
| 6 |
JSON_SCHEMA_VALID('{}', NULL) |
| 7 |
NULL |
| 8 |
SELECT JSON_SCHEMA_VALID(NULL, '{}'); |
| 9 |
JSON_SCHEMA_VALID(NULL, '{}') |
| 10 |
NULL |
| 11 |
# Check some basic scenarios to verify that everything is working as |
| 12 |
# expected. Note that the rapidjson library contains a bunch of tests to |
| 13 |
# verify the correctness of the JSON Schema validation, so we don't do |
| 14 |
# any extensive testing of the validation process itself here. |
| 15 |
SELECT JSON_SCHEMA_VALID('{ |
| 16 |
"type": "object", |
| 17 |
"properties": { |
| 18 |
"latitude": { |
| 19 |
"type": "number", |
| 20 |
"minimum": 63 |
| 21 |
}, |
| 22 |
"longitude": { |
| 23 |
"type": "number" |
| 24 |
} |
| 25 |
}, |
| 26 |
"required": ["latitude", "longitude"] |
| 27 |
}','{ |
| 28 |
"latitude": 63.444697, |
| 29 |
"longitude": 10.445118 |
| 30 |
}') AS should_be_valid; |
| 31 |
should_be_valid |
| 32 |
1 |
| 33 |
SELECT JSON_SCHEMA_VALID('{ |
| 34 |
"type": "object", |
| 35 |
"properties": { |
| 36 |
"latitude": { |
| 37 |
"type": "number", |
| 38 |
"minimum": 63 |
| 39 |
}, |
| 40 |
"longitude": { |
| 41 |
"type": "number" |
| 42 |
} |
| 43 |
}, |
| 44 |
"required": ["latitude", "longitude"] |
| 45 |
}','{ |
| 46 |
"longitude": 10.445118 |
| 47 |
}') AS should_be_invalid; |
| 48 |
should_be_invalid |
| 49 |
0 |
| 50 |
SELECT JSON_SCHEMA_VALID('{ |
| 51 |
"type": "object", |
| 52 |
"properties": { |
| 53 |
"latitude": { |
| 54 |
"type": "number", |
| 55 |
"minimum": 63 |
| 56 |
}, |
| 57 |
"longitude": { |
| 58 |
"type": "number" |
| 59 |
} |
| 60 |
}, |
| 61 |
"required": ["latitude", "longitude"] |
| 62 |
}','{ |
| 63 |
"latitude": 62, |
| 64 |
"longitude": 10.445118 |
| 65 |
}') AS should_be_invalid; |
| 66 |
should_be_invalid |
| 67 |
0 |
| 68 |
SELECT JSON_SCHEMA_VALID('{ |
| 69 |
"type": "object", |
| 70 |
"properties": { |
| 71 |
"a_string": { |
| 72 |
"type": "string", |
| 73 |
"pattern": "[5-9]" |
| 74 |
} |
| 75 |
} |
| 76 |
}','{ |
| 77 |
"a_string": "8" |
| 78 |
}') AS should_be_valid; |
| 79 |
should_be_valid |
| 80 |
1 |
| 81 |
SELECT JSON_SCHEMA_VALID('{ |
| 82 |
"type": "object", |
| 83 |
"properties": { |
| 84 |
"a_string": { |
| 85 |
"type": "string", |
| 86 |
"pattern": "[5-9]" |
| 87 |
} |
| 88 |
} |
| 89 |
}','{ |
| 90 |
"a_string": "4" |
| 91 |
}') AS should_be_invalid; |
| 92 |
should_be_invalid |
| 93 |
0 |
| 94 |
SELECT JSON_SCHEMA_VALID('{ |
| 95 |
"type": "object", |
| 96 |
"properties": { "$ref": "http://example.com" } |
| 97 |
}',' |
| 98 |
{ |
| 99 |
"latitude": 63.444697 |
| 100 |
}') AS invalid; |
| 101 |
ERROR 42000: This version of MySQL doesn't yet support 'references in JSON Schema' |
| 102 |
SELECT JSON_SCHEMA_VALID(repeat('[', 100000), json_object()); |
| 103 |
ERROR 22032: The JSON document exceeds the maximum depth. |
| 104 |
SELECT JSON_SCHEMA_VALID(json_object(), repeat('[', 100000)); |
| 105 |
ERROR 22032: The JSON document exceeds the maximum depth. |
| 106 |
# Check that we can use JSON_SCHEMA_VALID as a CHECK constraint. |
| 107 |
CREATE TABLE t1 ( |
| 108 |
geometry JSON, |
| 109 |
CHECK(JSON_SCHEMA_VALID('{ |
| 110 |
"type": "object", |
| 111 |
"properties": { |
| 112 |
"latitude": { |
| 113 |
"type": "number", |
| 114 |
"minimum": -90, |
| 115 |
"maximum": 90 |
| 116 |
}, |
| 117 |
"longitude": { |
| 118 |
"type": "number", |
| 119 |
"minimum": -180, |
| 120 |
"maximum": 180 |
| 121 |
} |
| 122 |
}, |
| 123 |
"required": ["latitude", "longitude"] |
| 124 |
}', geometry) |
| 125 |
) |
| 126 |
); |
| 127 |
INSERT INTO t1 VALUES ('{"latitude": 0, "longitude": 0}'); |
| 128 |
INSERT INTO t1 VALUES ('{"latitude": -90, "longitude": -180}'); |
| 129 |
INSERT INTO t1 VALUES ('{"latitude": 90, "longitude": 180}'); |
| 130 |
SELECT geometry FROM t1; |
| 131 |
geometry |
| 132 |
{"latitude": 0, "longitude": 0} |
| 133 |
{"latitude": -90, "longitude": -180} |
| 134 |
{"latitude": 90, "longitude": 180} |
| 135 |
INSERT INTO t1 VALUES ('{"latitude": 0}'); |
| 136 |
ERROR HY000: Check constraint 't1_chk_1' is violated. |
| 137 |
INSERT INTO t1 VALUES ('{"latitude": 181, "longitude": 0}'); |
| 138 |
ERROR HY000: Check constraint 't1_chk_1' is violated. |
| 139 |
DROP TABLE t1; |
| 140 |
# Negative test for wrong number of arguments |
| 141 |
SELECT JSON_SCHEMA_VALID(); |
| 142 |
ERROR 42000: Incorrect parameter count in the call to native function 'JSON_SCHEMA_VALID' |
| 143 |
SELECT JSON_SCHEMA_VALID(NULL); |
| 144 |
ERROR 42000: Incorrect parameter count in the call to native function 'JSON_SCHEMA_VALID' |
| 145 |
SELECT JSON_SCHEMA_VALID(NULL, NULL, NULL); |
| 146 |
ERROR 42000: Incorrect parameter count in the call to native function 'JSON_SCHEMA_VALID' |
| 147 |
# Invalid JSON document in either argument |
| 148 |
SET @invalid_json = '{"foo": "bar"'; |
| 149 |
SET @valid_json = '{}'; |
| 150 |
SELECT JSON_SCHEMA_VALID(@valid_json, @valid_json) AS should_be_true; |
| 151 |
should_be_true |
| 152 |
1 |
| 153 |
SELECT JSON_SCHEMA_VALID(@invalid_json, @valid_json); |
| 154 |
ERROR 22032: Invalid JSON text in argument 1 to function json_schema_valid: "Missing a comma or '}' after an object member." at position 13. |
| 155 |
SELECT JSON_SCHEMA_VALID(@valid_json, @invalid_json); |
| 156 |
ERROR 22032: Invalid JSON text in argument 2 to function json_schema_valid: "Missing a comma or '}' after an object member." at position 13. |
| 157 |
SELECT JSON_SCHEMA_VALID(@invalid_json, @invalid_json); |
| 158 |
ERROR 22032: Invalid JSON text in argument 1 to function json_schema_valid: "Missing a comma or '}' after an object member." at position 13. |
| 159 |
# Invalid regex patterns. rapidjson ignores invalid regex patterns, so |
| 160 |
# they are removed from the validation process. |
| 161 |
SELECT JSON_SCHEMA_VALID('{"type":"string","pattern":"("}', '"abc"') AS should_be_true; |
| 162 |
should_be_true |
| 163 |
1 |
| 164 |
SELECT JSON_SCHEMA_VALID('{"type":"string","pattern":"[asdf@123"}', '"abc"') AS should_be_true; |
| 165 |
should_be_true |
| 166 |
1 |
| 167 |
# Positive and negative tests for anchored regex patterns |
| 168 |
SELECT JSON_SCHEMA_VALID('{ |
| 169 |
"type": "object", |
| 170 |
"properties": { |
| 171 |
"a_string": { |
| 172 |
"type": "string", |
| 173 |
"pattern": "[5-9]" |
| 174 |
} |
| 175 |
} |
| 176 |
}','{ |
| 177 |
"a_string": "a8" |
| 178 |
}') AS should_be_valid; |
| 179 |
should_be_valid |
| 180 |
1 |
| 181 |
SELECT JSON_SCHEMA_VALID('{ |
| 182 |
"type": "object", |
| 183 |
"properties": { |
| 184 |
"a_string": { |
| 185 |
"type": "string", |
| 186 |
"pattern": "^[5-9]" |
| 187 |
} |
| 188 |
} |
| 189 |
}','{ |
| 190 |
"a_string": "a8" |
| 191 |
}') AS should_be_invalid; |
| 192 |
should_be_invalid |
| 193 |
0 |
| 194 |
SELECT JSON_SCHEMA_VALID('{ |
| 195 |
"type": "object", |
| 196 |
"properties": { |
| 197 |
"a_string": { |
| 198 |
"type": "string", |
| 199 |
"pattern": "[5-9]" |
| 200 |
} |
| 201 |
} |
| 202 |
}','{ |
| 203 |
"a_string": "8a" |
| 204 |
}') AS should_be_valid; |
| 205 |
should_be_valid |
| 206 |
1 |
| 207 |
SELECT JSON_SCHEMA_VALID('{ |
| 208 |
"type": "object", |
| 209 |
"properties": { |
| 210 |
"a_string": { |
| 211 |
"type": "string", |
| 212 |
"pattern": "[5-9]$" |
| 213 |
} |
| 214 |
} |
| 215 |
}','{ |
| 216 |
"a_string": "8a" |
| 217 |
}') AS should_be_invalid; |
| 218 |
should_be_invalid |
| 219 |
0 |
| 220 |
SELECT JSON_SCHEMA_VALID('{ |
| 221 |
"type": "object", |
| 222 |
"properties": { |
| 223 |
"a_string": { |
| 224 |
"type": "string", |
| 225 |
"pattern": "^[5-9]$" |
| 226 |
} |
| 227 |
} |
| 228 |
}','{ |
| 229 |
"a_string": "8" |
| 230 |
}') AS should_be_valid; |
| 231 |
should_be_valid |
| 232 |
1 |
| 233 |
SELECT JSON_SCHEMA_VALID('{ |
| 234 |
"type": "object", |
| 235 |
"properties": { |
| 236 |
"a_string": { |
| 237 |
"type": "string", |
| 238 |
"pattern": "^[5-9]$" |
| 239 |
} |
| 240 |
} |
| 241 |
}','{ |
| 242 |
"a_string": "a8" |
| 243 |
}') AS should_be_invalid; |
| 244 |
should_be_invalid |
| 245 |
0 |
| 246 |
SELECT JSON_SCHEMA_VALID('{ |
| 247 |
"type": "object", |
| 248 |
"properties": { |
| 249 |
"a_string": { |
| 250 |
"type": "string", |
| 251 |
"pattern": "^[5-9]$" |
| 252 |
} |
| 253 |
} |
| 254 |
}','{ |
| 255 |
"a_string": "8a" |
| 256 |
}') AS should_be_invalid; |
| 257 |
should_be_invalid |
| 258 |
0 |
| 259 |
# The JSON Schema must be an object, so see that we don't accept any |
| 260 |
# other type as the first argument. |
| 261 |
SELECT JSON_SCHEMA_VALID('[]', '{}'); |
| 262 |
ERROR 22032: Invalid JSON type in argument 1 to function json_schema_valid; an object is required. |
| 263 |
SELECT JSON_SCHEMA_VALID(CAST('test' AS JSON), '{}'); |
| 264 |
ERROR 22032: Invalid JSON text in argument 1 to function cast_as_json: "Invalid value." at position 1. |
| 265 |
SELECT JSON_SCHEMA_VALID('{"type":"object"}', '{'); |
| 266 |
ERROR 22032: Invalid JSON text in argument 2 to function json_schema_valid: "Missing a name for object member." at position 1. |
| 267 |
# Test some various scenarios with cached JSON schema (first argument is const) |
| 268 |
CREATE TABLE t1 (col1 JSON); |
| 269 |
INSERT INTO t1 VALUES ('{"latitude": 0, "longitude": 0}'); |
| 270 |
INSERT INTO t1 VALUES ('{"latitude": -90, "longitude": -180}'); |
| 271 |
INSERT INTO t1 VALUES (NULL); |
| 272 |
INSERT INTO t1 VALUES ('[]'); |
| 273 |
INSERT INTO t1 VALUES ('{"latitude": 90, "longitude": 180}'); |
| 274 |
# All rows should return SQL NULL |
| 275 |
SELECT JSON_SCHEMA_VALID(NULL, col1) FROM t1; |
| 276 |
JSON_SCHEMA_VALID(NULL, col1) |
| 277 |
NULL |
| 278 |
NULL |
| 279 |
NULL |
| 280 |
NULL |
| 281 |
NULL |
| 282 |
# We should get true, true, null, false, true |
| 283 |
SELECT JSON_SCHEMA_VALID('{"type":"object"}', col1) AS valid FROM t1; |
| 284 |
valid |
| 285 |
1 |
| 286 |
1 |
| 287 |
NULL |
| 288 |
0 |
| 289 |
1 |
| 290 |
# Both arguments should be JSON |
| 291 |
SELECT JSON_SCHEMA_VALID(JSON_DEPTH(col1), col1) FROM t1; |
| 292 |
ERROR 22032: Invalid data type for JSON data in argument 1 to function json_schema_valid; a JSON string or JSON type is required. |
| 293 |
SELECT JSON_SCHEMA_VALID('{"type":"object"}', JSON_DEPTH(col1)) FROM t1; |
| 294 |
ERROR 22032: Invalid data type for JSON data in argument 2 to function json_schema_valid; a JSON string or JSON type is required. |
| 295 |
SELECT JSON_SCHEMA_VALID(123, col1) FROM t1; |
| 296 |
ERROR 22032: Invalid data type for JSON data in argument 1 to function json_schema_valid; a JSON string or JSON type is required. |
| 297 |
DROP TABLE t1; |
| 298 |
# Test some scenarios with non-cached JSON schema (first argument is not const) |
| 299 |
CREATE TABLE t1 (col1 JSON); |
| 300 |
INSERT INTO t1 VALUES ('{"type":"object"}'); |
| 301 |
INSERT INTO t1 VALUES ('{"type":"array"}'); |
| 302 |
INSERT INTO t1 VALUES (NULL); |
| 303 |
INSERT INTO t1 VALUES ('{"type":"string"}'); |
| 304 |
# All rows should return SQL NULL |
| 305 |
SELECT JSON_SCHEMA_VALID(col1, NULL) FROM t1; |
| 306 |
JSON_SCHEMA_VALID(col1, NULL) |
| 307 |
NULL |
| 308 |
NULL |
| 309 |
NULL |
| 310 |
NULL |
| 311 |
# We should get false, true, null, false |
| 312 |
SELECT JSON_SCHEMA_VALID(col1, '[]') AS valid FROM t1; |
| 313 |
valid |
| 314 |
0 |
| 315 |
1 |
| 316 |
NULL |
| 317 |
0 |
| 318 |
DROP TABLE t1; |
| 319 |
CREATE TABLE t1(s VARCHAR(100), d VARCHAR(100)); |
| 320 |
# Invalid JSON document, non-const schema |
| 321 |
INSERT INTO t1 VALUES('{"type":"object"}', '{'); |
| 322 |
SELECT JSON_SCHEMA_VALID(s, d) FROM t1; |
| 323 |
ERROR 22032: Invalid JSON text in argument 2 to function json_schema_valid: "Missing a name for object member." at position 1. |
| 324 |
# Invalid, non-const schema |
| 325 |
UPDATE t1 SET s = '{', d = '{}'; |
| 326 |
SELECT JSON_SCHEMA_VALID(s, d) FROM t1; |
| 327 |
ERROR 22032: Invalid JSON text in argument 1 to function json_schema_valid: "Missing a name for object member." at position 1. |
| 328 |
DROP TABLE t1; |
| 329 |
# Ensure that our item tree transformation doesn't get stuck forever when |
| 330 |
# using prepared statements. |
| 331 |
PREPARE stmt FROM 'SELECT JSON_SCHEMA_VALID(?, ''{}'') FROM DUAL'; |
| 332 |
SET @json_schema = '{"type":"object"}'; |
| 333 |
SET @null = NULL; |
| 334 |
EXECUTE stmt USING @json_schema; |
| 335 |
JSON_SCHEMA_VALID(?, '{}') |
| 336 |
1 |
| 337 |
EXECUTE stmt USING @null; |
| 338 |
JSON_SCHEMA_VALID(?, '{}') |
| 339 |
NULL |
| 340 |
EXECUTE stmt USING @json_schema; |
| 341 |
JSON_SCHEMA_VALID(?, '{}') |
| 342 |
1 |
| 343 |
# |
| 344 |
# Bug#29366780 WL#11999: SIG6 IN SETUP_FIELDS() AT SQL/SQL_BASE.CC |
| 345 |
# |
| 346 |
SELECT JSON_SCHEMA_VALID(CAST('NULL' AS JSON), CAST('NULL' AS JSON)); |
| 347 |
ERROR 22032: Invalid JSON text in argument 1 to function cast_as_json: "Invalid value." at position 0. |
| 348 |
# See that we don't accept non-JSON types like geometry, bool, ints etc. |
| 349 |
SELECT JSON_SCHEMA_VALID(JSON_OBJECT(), 123); |
| 350 |
ERROR 22032: Invalid data type for JSON data in argument 2 to function json_schema_valid; a JSON string or JSON type is required. |
| 351 |
SELECT JSON_SCHEMA_VALID(JSON_OBJECT(), POINT(1, 1)); |
| 352 |
ERROR 22032: Invalid data type for JSON data in argument 2 to function json_schema_valid; a JSON string or JSON type is required. |
| 353 |
SELECT JSON_SCHEMA_VALID(JSON_OBJECT(), true); |
| 354 |
ERROR 22032: Invalid data type for JSON data in argument 2 to function json_schema_valid; a JSON string or JSON type is required. |
| 355 |
SELECT JSON_SCHEMA_VALID(123, JSON_OBJECT()); |
| 356 |
ERROR 22032: Invalid data type for JSON data in argument 1 to function json_schema_valid; a JSON string or JSON type is required. |
| 357 |
SELECT JSON_SCHEMA_VALID(POINT(1, 1), JSON_OBJECT()); |
| 358 |
ERROR 22032: Invalid data type for JSON data in argument 1 to function json_schema_valid; a JSON string or JSON type is required. |
| 359 |
SELECT JSON_SCHEMA_VALID(true, JSON_OBJECT()); |
| 360 |
ERROR 22032: Invalid data type for JSON data in argument 1 to function json_schema_valid; a JSON string or JSON type is required. |
| 361 |
# |
| 362 |
# Bug#29524331: WL#11999: ASSERTION FAILURE: `!ARGS[0]->CONST_ITEM()' |
| 363 |
# Bug#29528888: WL#11999: SIG6 IN ITEM_FUNC_JSON_SCHEMA_VALID::VAL_BOOL() |
| 364 |
# AT ITEM_JSON_FUNC.CC |
| 365 |
# |
| 366 |
CREATE TABLE t1 (pk INT PRIMARY KEY, j JSON); |
| 367 |
INSERT INTO t1 VALUES (1, '{"key": "foobar"}' ); |
| 368 |
SELECT JSON_SCHEMA_VALID(j, j) FROM t1 WHERE pk = 1; |
| 369 |
JSON_SCHEMA_VALID(j, j) |
| 370 |
1 |
| 371 |
SELECT JSON_SCHEMA_VALID(t2.j, t2.j) |
| 372 |
FROM t1, (SELECT * FROM t1 WHERE pk = 1) t2; |
| 373 |
JSON_SCHEMA_VALID(t2.j, t2.j) |
| 374 |
1 |
| 375 |
DROP TABLE t1; |
| 376 |
# |
| 377 |
# WL#13195 - TABLE WITH JSON SCHEMA VALIDATION CONSTRAINT SHOULD |
| 378 |
# RETURN ERROR FOR CONCRETE ROW |
| 379 |
# |
| 380 |
CREATE TABLE t1 (id INT, |
| 381 |
geometry JSON, |
| 382 |
CHECK(JSON_SCHEMA_VALID('{ |
| 383 |
"type": "object", |
| 384 |
"properties": { |
| 385 |
"latitude": { |
| 386 |
"type": "number", |
| 387 |
"minimum": -90, |
| 388 |
"maximum": 90 |
| 389 |
}, |
| 390 |
"longitude": { |
| 391 |
"type": "number", |
| 392 |
"minimum": -180, |
| 393 |
"maximum": 180 |
| 394 |
} |
| 395 |
}, |
| 396 |
"required": ["latitude", "longitude"] |
| 397 |
}', geometry) |
| 398 |
) |
| 399 |
); |
| 400 |
# With WL changes, on CHECK constraint violation detailed information |
| 401 |
# about the failed JSON schema validation is pushed to diagnostics area. |
| 402 |
# SHOW WARNINGS and GET DIAGNOSTICS lists detailed information about the |
| 403 |
# failed schema validation. |
| 404 |
# CHECK constraint evaluation fails as required property "longitude" is |
| 405 |
# missing. |
| 406 |
INSERT INTO t1 VALUES (1, '{"latitude": 0}'); |
| 407 |
ERROR HY000: Check constraint 't1_chk_1' is violated. |
| 408 |
SHOW WARNINGS; |
| 409 |
Level Code Message |
| 410 |
Error 3934 The JSON document location '#' failed requirement 'required' at JSON Schema location '#'. |
| 411 |
Error 3819 Check constraint 't1_chk_1' is violated. |
| 412 |
GET DIAGNOSTICS CONDITION 1 @p1 = MYSQL_ERRNO, @p2 = MESSAGE_TEXT; |
| 413 |
SELECT @p1, @p2; |
| 414 |
@p1 @p2 |
| 415 |
3934 The JSON document location '#' failed requirement 'required' at JSON Schema location '#'. |
| 416 |
# CHECK constraint evaluation fails as latitude is out of range. |
| 417 |
INSERT INTO t1 VALUES (1, '{"latitude": 181, "longitude": 0}'); |
| 418 |
ERROR HY000: Check constraint 't1_chk_1' is violated. |
| 419 |
SHOW WARNINGS; |
| 420 |
Level Code Message |
| 421 |
Error 3934 The JSON document location '#/latitude' failed requirement 'maximum' at JSON Schema location '#/properties/latitude'. |
| 422 |
Error 3819 Check constraint 't1_chk_1' is violated. |
| 423 |
GET DIAGNOSTICS CONDITION 1 @p1 = MYSQL_ERRNO, @p2 = MESSAGE_TEXT; |
| 424 |
SELECT @p1, @p2; |
| 425 |
@p1 @p2 |
| 426 |
3934 The JSON document location '#/latitude' failed requirement 'maximum' at JSON Schema location '#/properties/latitude'. |
| 427 |
# CHECK constraint evaluation fails for 3rd row as latitude is out of |
| 428 |
# range. |
| 429 |
INSERT INTO t1 VALUES (1, '{"latitude": 0, "longitude": 0}'), |
| 430 |
(2, '{"latitude": -90, "longitude": -180}'), |
| 431 |
(3, '{"latitude": 181, "longitude": 0}'), |
| 432 |
(4, '{"latitude": 90, "longitude": 180}'); |
| 433 |
ERROR HY000: Check constraint 't1_chk_1' is violated. |
| 434 |
SHOW WARNINGS; |
| 435 |
Level Code Message |
| 436 |
Error 3934 The JSON document location '#/latitude' failed requirement 'maximum' at JSON Schema location '#/properties/latitude'. |
| 437 |
Error 3819 Check constraint 't1_chk_1' is violated. |
| 438 |
GET DIAGNOSTICS CONDITION 1 @p1 = MYSQL_ERRNO, @p2 = MESSAGE_TEXT; |
| 439 |
SELECT @p1, @p2; |
| 440 |
@p1 @p2 |
| 441 |
3934 The JSON document location '#/latitude' failed requirement 'maximum' at JSON Schema location '#/properties/latitude'. |
| 442 |
INSERT INTO t1 VALUES (1, '{"latitude": 0, "longitude": 0}'), |
| 443 |
(2, '{"latitude": -90, "longitude": -180}'), |
| 444 |
(3, '{"latitude": 90, "longitude": 180}'); |
| 445 |
# CHECK constraint evaluation fails as required property "longitude" |
| 446 |
# is missing. |
| 447 |
UPDATE t1 SET geometry = '{"latitude": 90}'; |
| 448 |
ERROR HY000: Check constraint 't1_chk_1' is violated. |
| 449 |
SHOW WARNINGS; |
| 450 |
Level Code Message |
| 451 |
Error 3934 The JSON document location '#' failed requirement 'required' at JSON Schema location '#'. |
| 452 |
Error 3819 Check constraint 't1_chk_1' is violated. |
| 453 |
GET DIAGNOSTICS CONDITION 1 @p1 = MYSQL_ERRNO, @p2 = MESSAGE_TEXT; |
| 454 |
SELECT @p1, @p2; |
| 455 |
@p1 @p2 |
| 456 |
3934 The JSON document location '#' failed requirement 'required' at JSON Schema location '#'. |
| 457 |
# CHECK constraint evaluation fails as latitude is out of range. |
| 458 |
UPDATE t1 SET geometry = '{"latitude": 181, "longitude": 0}'; |
| 459 |
ERROR HY000: Check constraint 't1_chk_1' is violated. |
| 460 |
SHOW WARNINGS; |
| 461 |
Level Code Message |
| 462 |
Error 3934 The JSON document location '#/latitude' failed requirement 'maximum' at JSON Schema location '#/properties/latitude'. |
| 463 |
Error 3819 Check constraint 't1_chk_1' is violated. |
| 464 |
GET DIAGNOSTICS CONDITION 1 @p1 = MYSQL_ERRNO, @p2 = MESSAGE_TEXT; |
| 465 |
SELECT @p1, @p2; |
| 466 |
@p1 @p2 |
| 467 |
3934 The JSON document location '#/latitude' failed requirement 'maximum' at JSON Schema location '#/properties/latitude'. |
| 468 |
# CHECK constraint evaluation fails for 3rd row as latitude is out of |
| 469 |
# range. |
| 470 |
UPDATE t1 SET geometry = IF (id = 3, '{"latitude": 181, "longitude": 0}', |
| 471 |
'{"latitude": 90, "longitude": 0}'); |
| 472 |
ERROR HY000: Check constraint 't1_chk_1' is violated. |
| 473 |
SHOW WARNINGS; |
| 474 |
Level Code Message |
| 475 |
Error 3934 The JSON document location '#/latitude' failed requirement 'maximum' at JSON Schema location '#/properties/latitude'. |
| 476 |
Error 3819 Check constraint 't1_chk_1' is violated. |
| 477 |
GET DIAGNOSTICS CONDITION 1 @p1 = MYSQL_ERRNO, @p2 = MESSAGE_TEXT; |
| 478 |
SELECT @p1, @p2; |
| 479 |
@p1 @p2 |
| 480 |
3934 The JSON document location '#/latitude' failed requirement 'maximum' at JSON Schema location '#/properties/latitude'. |
| 481 |
DROP TABLE t1; |
| 482 |
# With ALTER TABLE and INSERT statements verify constraint evaluation |
| 483 |
# with NULL value. |
| 484 |
CREATE TABLE t1 (id INT AUTO_INCREMENT PRIMARY KEY); |
| 485 |
INSERT INTO t1 VALUES (1), (2); |
| 486 |
ALTER TABLE t1 ADD COLUMN jcol1 JSON, |
| 487 |
ADD COLUMN jcol2 JSON, |
| 488 |
ADD CONSTRAINT CHECK (json_schema_valid('{ |
| 489 |
"type": "object", |
| 490 |
"properties": { |
| 491 |
"id": { |
| 492 |
"type": "integer" |
| 493 |
}, |
| 494 |
"name": { |
| 495 |
"type": "string" |
| 496 |
}, |
| 497 |
"price": { |
| 498 |
"type": "number", |
| 499 |
"minimum": 0, |
| 500 |
"exclusiveMinimum": true |
| 501 |
} |
| 502 |
}, |
| 503 |
"required": ["id","name","price"]}',`jcol1`)), |
| 504 |
ADD CONSTRAINT CHECK (json_schema_valid('{ |
| 505 |
"type": "object", |
| 506 |
"properties": { |
| 507 |
"latitude": { |
| 508 |
"type": "number", |
| 509 |
"minimum": -90, |
| 510 |
"maximum": 90 |
| 511 |
}, |
| 512 |
"longitude": { |
| 513 |
"type": "number", |
| 514 |
"minimum": -180, |
| 515 |
"maximum": 180 |
| 516 |
} |
| 517 |
}, |
| 518 |
"required": ["latitude", "longitude"]}',`jcol2`)); |
| 519 |
INSERT INTO t1 ( jcol1 ) VALUES ('{"id": 2, "name": "shiva", "price": 1 }'); |
| 520 |
# With ALTER TABLE and INSERT statements verify constraint evaluation |
| 521 |
# with reference in JSON schema. |
| 522 |
ALTER TABLE t1 ADD COLUMN jcol3 JSON, |
| 523 |
ADD CONSTRAINT CHECK(json_schema_valid('{ |
| 524 |
"type": "object", |
| 525 |
"properties": { |
| 526 |
"id": { |
| 527 |
"type": "integer" |
| 528 |
}, |
| 529 |
"$ref": "http://example.com" |
| 530 |
}, |
| 531 |
"required": ["id","$ref"]}',`jcol3`)); |
| 532 |
INSERT INTO t1 ( jcol3 ) VALUES ('{"id": 3, "$ref": "http://example.com"}'); |
| 533 |
ERROR HY000: Check constraint 't1_chk_3' is violated. |
| 534 |
SHOW WARNINGS; |
| 535 |
Level Code Message |
| 536 |
Error 1235 This version of MySQL doesn't yet support 'references in JSON Schema' |
| 537 |
Error 3819 Check constraint 't1_chk_3' is violated. |
| 538 |
DROP TABLE t1; |
| 539 |
# |
| 540 |
# Bug#30622327: JSON SCHEMA FUNCTIONS SHOULD CHECK ARGUMENT TYPES |
| 541 |
# AT RESOLVE TIME |
| 542 |
# |
| 543 |
CREATE TABLE t(j JSON, g GEOMETRY); |
| 544 |
PREPARE ps FROM 'SELECT * FROM t WHERE JSON_SCHEMA_VALID(j, g)'; |
| 545 |
ERROR 22032: Invalid data type for JSON data in argument 2 to function json_schema_valid; a JSON string or JSON type is required. |
| 546 |
PREPARE ps FROM 'SELECT * FROM t WHERE JSON_SCHEMA_VALID(g, j)'; |
| 547 |
ERROR 22032: Invalid data type for JSON data in argument 1 to function json_schema_valid; a JSON string or JSON type is required. |
| 548 |
DROP TABLE t; |