MySQL Connector/J 8.0 Developer Guide

10.2 Schema Validation

For Connector/J 8.0.21 and later, when working with MySQL Server 8.0.19 and later: Schema validation can be configured for a Collection, so that documents in the Collection are validated against a schema before they can be inserted or updated. This is done by specifying a JSON Schema during Collection creation or modification; schema validation is then performed by the server at a document creation or update, and an error is returned if the document does not validate against the assigned schema. For more information on JSON schema validation in MySQL, see JSON Schema Validation Functions. This section describes how to configure schema validation for a Collection with Connector/J.

To configure schema validation during the creation of a Collection, pass to the createCollection() method a CreateCollectionOptions object, which has these fields:

This is an example of how to configure schema validation at the creation of a Collection:

Collection coll = this.schema.createCollection(collName,
    new CreateCollectionOptions()
        .setReuseExisting(false)
        .setValidation(new Validation()
           .setLevel(ValidationLevel.STRICT)
           .setSchema(
               "{\"id\": \"http://json-schema.org/geo\","
               + "\"$schema\": \"http://json-schema.org/draft-06/schema#\","
               + "       \"description\": \"A geographical coordinate\","
               + "       \"type\": \"object\","
               + "       \"properties\": {"
               + "          \"latitude\": {"
               + "             \"type\": \"number\""
               + "          },"
               + "          \"longitude\": {"
               + "             \"type\": \"number\""
               + "          }"
               + "       },"
               + "       \"required\": [\"latitude\", \"longitude\"]"
               + "  }"
)));

The set fields are accessible by the corresponding getter methods.

To modify the schema validation configuration for a Collection, use the modifyCollection() method and pass to it a ModifyCollectionOptions object, which has the same fields as the CreateCollectionOptions object except for the reuse field, which does not exist for a ModifyCollectionOptions object. For the Validation object of a ModifyCollectionOptions object, users can set either its level or schema, or both. Here is an example of using the modifyCollection() to change the schema validation configuration:

schema.modifyCollection(collName,
    new ModifyCollectionOptions()
        .setValidation(new Validation()
           .setLevel(ValidationLevel.OFF)
           .setSchema(
               "{\"id\": \"http://json-schema.org/geo\","
               + "\"$schema\": \"http://json-schema.org/draft-06/schema#\","
               + "       \"description\": \"NEW geographical coordinate\","
               + "       \"type\": \"object\","
               + "       \"properties\": {"
               + "          \"latitude\": {"
               + "             \"type\": \"number\""
               + "          },"
               + "          \"longitude\": {"
               + "             \"type\": \"number\""
               + "          }"
               + "       },"
               + "       \"required\": [\"latitude\", \"longitude\"]"
               + "  }"
)));

If the Collection contains documents that do not validate against the new JSON schema supplied through ModifyCollectionOptions, the server will reject the schema modification with the error ERROR 5180 (HY000) Document is not valid according to the schema assigned to collection.

Note

createCollection() and modifyCollection() are overloaded: they can be called without passing to them the CreateCollectionOptions or the ModifyCollectionOptions, respectively, in which case schema validation will not be applied to the Collection.