Rechercher dans le manuel MySQL
12.17.7 JSON Schema Validation Functions
Beginning with MySQL 8.0.17, MySQL supports validation of JSON
documents against JSON schemas conforming to
Draft
4 of the JSON Schema specification. This can be done using
either of the functions detailed in this section, both of which
take two orguments, a JSON schema, and a JSON document which is
validated against the schema.
JSON_SCHEMA_VALID()
returns true if
the document is validates against the schema, and false if it is
not;
JSON_SCHEMA_VALIDATION_REPORT()
provides a report in JSON format on the validation.
Both functions handle null or invalid input as follows:
If at least one of the arguments is
NULL
, the function returnsNULL
.If at least one of the arguments is not valid JSON, the function raises an error (
ER_INVALID_TYPE_FOR_JSON
)In addition, if the schema is not a valid JSON object, the function returns
ER_INVALID_JSON_TYPE
.
MySQL supports the required
attribute in JSON
schemas to enforce the inclusion of required properties (see the
examples in the function descriptions).
MySQL does not support external resources in JSON schemas; using
the $ref
keyword causes
JSON_SCHEMA_VALID()
to fail with
ER_NOT_SUPPORTED_YET
.
MySQL supports regular expression patterns in JSON schema, which
supports but silently ignores invalid patterns (see the
description of JSON_SCHEMA_VALID()
for an
example).
These functions are described in detail in the following list:
JSON_SCHEMA_VALID(
schema
,document
)Validates a JSON
document
against a JSONschema
. Bothschema
anddocument
are required. The schema must be a valid JSON object; the document must be a valid JSON document. Provided that these conditions are met: If the document validates against the schema, the function returns true (1); otherwise, it returns false (0).In this example, we set a user variable
@schema
to the value of a a JSON schema for geographical coordinates, and another one@document
to the value of a JSON document containing one such coordinate. We then verify that@document
validates according to@schema
by using them as the arguments toJSON_SCHEMA_VALID()
:- '> "id": "http://json-schema.org/geo",
- '> "$schema": "http://json-schema.org/draft-04/schema#",
- '> "description": "A geographical coordinate",
- '> "type": "object",
- '> "properties": {
- '> "latitude": {
- '> "type": "number",
- '> "minimum": -90,
- '> "maximum": 90
- '> },
- '> "longitude": {
- '> "type": "number",
- '> "minimum": -180,
- '> "maximum": 180
- '> }
- '> },
- '> "required": ["latitude", "longitude"]
- '>}';
- Query OK, 0 rows affected (0.01 sec)
- '> "latitude": 63.444697,
- '> "longitude": 10.445118
- '>}';
- Query OK, 0 rows affected (0.00 sec)
- mysql> SELECT JSON_SCHEMA_VALID(@schema, @document);
- +---------------------------------------+
- | JSON_SCHEMA_VALID(@schema, @document) |
- +---------------------------------------+
- | 1 |
- +---------------------------------------+
- 1 row in set (0.00 sec)
Since
@schema
contains therequired
attribute, we can set@document
to a value that is otherwise valid but does not contain the required properties, then test it against@schema
, like this:- Query OK, 0 rows affected (0.00 sec)
- +---------------------------------------+
- +---------------------------------------+
- | 0 |
- +---------------------------------------+
If we now set the value of
@schema
to the same JSON schema but without therequired
attribute,@document
validates because it is a valid JSON object, even though it contains no properties, as shown here:- '> "id": "http://json-schema.org/geo",
- '> "$schema": "http://json-schema.org/draft-04/schema#",
- '> "description": "A geographical coordinate",
- '> "type": "object",
- '> "properties": {
- '> "latitude": {
- '> "type": "number",
- '> "minimum": -90,
- '> "maximum": 90
- '> },
- '> "longitude": {
- '> "type": "number",
- '> "minimum": -180,
- '> "maximum": 180
- '> }
- '> }
- '>}';
- Query OK, 0 rows affected (0.00 sec)
- mysql> SELECT JSON_SCHEMA_VALID(@schema, @document);
- +---------------------------------------+
- | JSON_SCHEMA_VALID(@schema, @document) |
- +---------------------------------------+
- | 1 |
- +---------------------------------------+
- 1 row in set (0.00 sec)
JSON Schema has support for specifying regular expression patterns for strings, but the implementation used by MySQL silently ignores invalid patterns. This means that
JSON_SCHEMA_VALID()
can return true even when a regular expression pattern is invalid, as shown here:- +---------------------------------------------------------------+
- | JSON_SCHEMA_VALID('{"type":"string","pattern":"("}', '"abc"') |
- +---------------------------------------------------------------+
- | 1 |
- +---------------------------------------------------------------+
JSON_SCHEMA_VALIDATION_REPORT(
schema
,document
)Validates a JSON
document
against a JSONschema
. Bothschema
anddocument
are required. As with JSON_VALID_SCHEMA(), the schema must be a valid JSON object, and the document must be a valid JSON document. Provided that these conditions are met, the function returns a report, as a JSON document, on the outcome of the validation. If the JSON document is considered valid according to the JSON Schema, the function returns a JSON object with one propertyvalid
having the value "true". If the JSON document fails validation, the function returns a JSON object which includes the properties listed here:valid
: Always "false" for a failed schema validationreason
: A human-readable string containing the reason for the failureschema-location
: A JSON pointer URI fragment identifier indicating where in the JSON schema the validation failed (see Note following this list)document-location
: A JSON pointer URI fragment identifier indicating where in the JSON document the validation failed (see Note following this list)schema-failed-keyword
: A string containing the name of the keyword or property in the JSON schema that was violated
NoteJSON pointer URI fragment identifiers are defined in RFC 6901 - JavaScript Object Notation (JSON) Pointer. (These are not the same as the JSON path notation used by
JSON_EXTRACT()
and other MySQL JSON functions.) In this notation,#
represents the entire document, and#/myprop
represents the portion of the document included in the top-level property namedmyprop
. See the specification just cited and the examples shown later in this section for more information.In this example, we set a user variable
@schema
to the value of a a JSON schema for geographical coordinates, and another one@document
to the value of a JSON document containing one such coordinate. We then verify that@document
validates according to@schema
by using them as the arguments toJSON_SCHEMA_VALIDATION_REORT()
:- '> "id": "http://json-schema.org/geo",
- '> "$schema": "http://json-schema.org/draft-04/schema#",
- '> "description": "A geographical coordinate",
- '> "type": "object",
- '> "properties": {
- '> "latitude": {
- '> "type": "number",
- '> "minimum": -90,
- '> "maximum": 90
- '> },
- '> "longitude": {
- '> "type": "number",
- '> "minimum": -180,
- '> "maximum": 180
- '> }
- '> },
- '> "required": ["latitude", "longitude"]
- '>}';
- Query OK, 0 rows affected (0.01 sec)
- '> "latitude": 63.444697,
- '> "longitude": 10.445118
- '>}';
- Query OK, 0 rows affected (0.00 sec)
- mysql> SELECT JSON_SCHEMA_VALIDATION_REPORT(@schema, @document);
- +---------------------------------------------------+
- | JSON_SCHEMA_VALIDATION_REPORT(@schema, @document) |
- +---------------------------------------------------+
- | {"valid": true} |
- +---------------------------------------------------+
- 1 row in set (0.00 sec)
Now we set
@document
such that it specifies an illegal value for one of its properties, like this:- '> "latitude": 63.444697,
- '> "longitude": 310.445118
- '> }';
Validation of
@document
now fails when tested withJSON_SCHEMA_VALIDATION_REPORT()
. The output from the function call contains detailed information about the failure (with the function wrapped byJSON_PRETTY()
to provide better formatting), as shown here:- *************************** 1. row ***************************
- "reason": "The JSON document location '#/longitude' failed requirement 'maximum' at JSON Schema location '#/properties/longitude'",
- "schema-location": "#/properties/longitude",
- "document-location": "#/longitude",
- "schema-failed-keyword": "maximum"
- }
Since
@schema
contains therequired
attribute, we can set@document
to a value that is otherwise valid but does not contain the required properties, then test it against@schema
. The output ofJSON_SCHEMA_VALIDATION_REPORT()
shows that validation fails due to lack of a required element, like this:- Query OK, 0 rows affected (0.00 sec)
- *************************** 1. row ***************************
- "reason": "The JSON document location '#' failed requirement 'required' at JSON Schema location '#'",
- "schema-location": "#",
- "document-location": "#",
- "schema-failed-keyword": "required"
- }
If we now set the value of
@schema
to the same JSON schema but without therequired
attribute,@document
validates because it is a valid JSON object, even though it contains no properties, as shown here:- '> "id": "http://json-schema.org/geo",
- '> "$schema": "http://json-schema.org/draft-04/schema#",
- '> "description": "A geographical coordinate",
- '> "type": "object",
- '> "properties": {
- '> "latitude": {
- '> "type": "number",
- '> "minimum": -90,
- '> "maximum": 90
- '> },
- '> "longitude": {
- '> "type": "number",
- '> "minimum": -180,
- '> "maximum": 180
- '> }
- '> }
- '>}';
- Query OK, 0 rows affected (0.00 sec)
- mysql> SELECT JSON_SCHEMA_VALIDATION_REPORT(@schema, @document);
- +---------------------------------------------------+
- | JSON_SCHEMA_VALIDATION_REPORT(@schema, @document) |
- +---------------------------------------------------+
- | {"valid": true} |
- +---------------------------------------------------+
- 1 row in set (0.00 sec)
Deutsche Übersetzung
Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.
Vielen Dank im Voraus.
Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-json-validation-functions.html
Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.
Referenzen
Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.