I want to define a Collection in Mongo using Spring-boot with a JSON Schema validator option (https://docs.mongodb.com/manual/core/schema-validation/#json-schema), I don't want a JSR-303 Bean validation (this is not a valid answer Spring data mongoDb not null annotation like Spring data Jpa), but define, at the moment of the creation of the Collection, an option that is showed in the JSON using CollectionInfos().
Example, if I define an Account model class likes:
public class Account {
@Id
private String id;
private String name;
private String surname;
@NotNull
private String username;
}
I want that the collection has, using db.getCollectionInfos(), a json likes:
[
{
"name" : "account",
"type" : "collection",
"options" : {
"validator" : {
"$jsonSchema" : {
"bsonType" : "object",
"required" : [
"username"
]
}
}
},
"info" : {
"readOnly" : false,
"uuid" : UUID("979cdc4b-d6f3-4aef-bc89-3eee812773a5")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "databaseName.account"
}
}
]
The procedure could be similar to spring.jpa.hibernate.ddl-auto = create, because it defines rules at the schema level, and not similar to Bean validator, that defines rules at the Application level.
MongoDB uses a flexible schema model, which means that documents in a collection do not need to have the same fields or data types by default. Once you've established an application schema, you can use schema validation to ensure there are no unintended schema changes or improper data types.
If you want to "add a validation rule" then there are methods which depend on the current state of the collection. In either case, there actually is no "add to" function, but the action instead is to "replace" all the validation rules with new ones to specify. Read on for the rules of how this works.
The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.
JsonSchema is supported by Spring Data MongoDB as of version 2.1
. Please see the Reference Documentation for details.
You can use a builder like below to define your schema.
MongoJsonSchema schema = MongoJsonSchema.builder()
.required("username")
.properties(JsonSchemaProperty.string("username"))
.build();
template.createCollection("account", CollectionOptions.empty().schema(schema));
At the time of writing json schema creation from a domain type is not support. However you may want to join the discussion DATAMONGO-1849 and/or give the snapshots of PR#733 a try.
The suggestion would turn the DomainType
into a MongoJsonSchema
by calling something like MongoJsonSchema schema = schemaCreator.createSchemaFor(DomainType.class);
public class DomainType {
private final String requiredCtorArg;
private final @Nullable String nullableCtorArg;
private String optionalArg;
public DomainType(String requiredCtorArg, @Nullable String nullableCtorArg) {
this.requiredCtorArg = requiredCtorArg;
this.nullableCtorArg = nullableCtorArg;
}
// gettter / setter omitted
}
{
'type' : 'object',
'required' : ['requiredCtorArg'],
'properties' : {
'requiredCtorArg' : { 'type' : 'string' },
'nullableCtorArg' : { 'type' : 'string' },
'optionalArg' : { 'type' : 'string' }
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With