11

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.

Federico Gatti
  • 535
  • 1
  • 9
  • 22
  • 1
    Schema Validation on the server in MongoDB is "really new" and the `$jsonSchema` "query operator" itself is really only a couple of months in circulation in the wild. Additions of "new features" to spring-mongo have been traditionally "slow paced", but it is however not an official MongoDB project and therefore a *"community labor of love"*. If you don't see a feature mentioned in the documentation, then it's not there. They might just accept a volunteered pull request – Neil Lunn May 04 '18 at 07:40
  • What is your question? – BrentR Oct 03 '18 at 15:01
  • 1
    @BrentR I want that spring-data-mongo define automatically the JSON Schema starting from the model – Federico Gatti Oct 03 '18 at 15:11

1 Answers1

3

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' }
     }
}
Christoph Strobl
  • 6,491
  • 25
  • 33
  • Hi @Christoph, thank you for your reply! I have a question: why do you have decided to set **@Nullable** to define the NOT required parameters instead of define **@NotNull** parameters like in Hibernate? Also, is there any task in progress to support also the special keywords (like _pattern_ for String)? – Federico Gatti Apr 01 '19 at 10:04
  • Please let's discuss requirements/decisions for deriving the schema in the [issue tracker](https://jira.spring.io/browse/DATAMONGO-1849). – Christoph Strobl Apr 02 '19 at 08:36