Skip to content

DATAMONGO-1835 - Add support for $jsonSchema to CollectionOptions and Criteria queries. #524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from

Conversation

christophstrobl
Copy link
Member

We now support $jsonSchema for collection creation and within Criteria queries.
Schema properties and requiredProperties are mapped against the domain types fields. enum values use the conversion infrastructure to map values into the MongoDB specific representation.

// -------------
// Query usage
// -------------

MongoJsonSchema schema = MongoJsonSchema.builder()
	.required("address")
	.property(object("address").properties(string("street").matching("^Apple.*"))).build();

List<Person> person = template.find(query(matchingDocumentStructure(schema)), Person.class));
// ---------------------
// Collection validation
// ---------------------

MongoJsonSchema schema = MongoJsonSchema.builder()
	.required("firstname", "lastname")
	.properties(
		string("firstname").possibleValues("luke", "han").maxLength(10), 
		object("address").properties(
			string("postCode").minLength(4).maxLength(5))
		).build();

template.createCollection(Person.class, CollectionOptions.empty()
	.schema(schema)
	.failOnValidationError());

Related to #511 which should be added on top.

christophstrobl and others added 9 commits January 9, 2018 14:44
… map and write schema to collection)

We now can create a $jsonSchema that can be used as a validator when creating collections. Required fields and properties get mapped according to the @field annotation on domain objects.
Currently the schema is written directly to the db. So there’s still a lot of work to do:

* Increase test coverage
* Add support for MongoJsonSchema as validator to CollectionOptions.
* Run enum values through the conversion infrastructure.
* Support reading back $jsonSchema from Database using the listCollections command.
* Add support for usage of $jsonSchema in Criteria API
* Update Javadoc & reference documentation.
Add JSON schema to collection creation using reactive API. Refactor MongoJsonSchema to interface with default implementations for JsonSchemaObject and Document-based schemas. Make fields final and methods static where possible.

Add minItems/maxItems/additionalItems properties to ArrayJsonSchemaProperty. Add missing overrides to NullJsonSchemaProperty.
Slightly rename methods for item/property counts. Add generics, Javadoc, minor tweaks.
mp911de pushed a commit that referenced this pull request Jan 12, 2018
mp911de pushed a commit that referenced this pull request Jan 12, 2018
We now can create a $jsonSchema that can be used as a validator when creating collections and as predicate for queries. Required fields and properties get mapped according to the @field annotation on domain objects.

MongoJsonSchema schema = MongoJsonSchema.builder().required("firstname", "lastname")
  .properties(string("firstname").possibleValues("luke", "han"),
              object("address").properties(string("postCode").minLength(4).maxLength(5)))
  .build();

resulting in the following schema:

{
  "type": "object",
  "required": [ "firstname", "lastname" ],
  "properties": {
    "firstname": {
      "type": "string", "enum": [ "luke", "han" ],
    },
    "address": {
      "type": "object",
      "properties": {
        "postCode": { "type": "string", "minLength": 4, "maxLength": 5 }
      }
    }
  }
}

Query usage:

MongoJsonSchema schema = MongoJsonSchema.builder()
  .required("address")
  .property(object("address").properties(string("street").matching("^Apple.*"))).build();

List<Person> person = template.find(query(matchingDocumentStructure(schema)), Person.class));

Collection validation:

MongoJsonSchema schema = MongoJsonSchema.builder().required("firstname", "lastname")
  .properties(string("firstname").possibleValues("luke", "han"),
              object("address").properties(string("postCode").minLength(4).maxLength(5)))
  .build();

template.createCollection(Person.class, CollectionOptions.empty()
  .schema(schema)
  .failOnValidationError());

Original pull request: #524.
mp911de added a commit that referenced this pull request Jan 12, 2018
Original pull request: #524.
mp911de added a commit that referenced this pull request Jan 12, 2018
Add JSON schema to collection creation using reactive API. Refactor MongoJsonSchema to interface with default implementations for JsonSchemaObject and Document-based schemas. Make fields final and methods static where possible.

Add minItems/maxItems/additionalItems properties to ArrayJsonSchemaProperty. Add missing overrides to NullJsonSchemaProperty.
Slightly rename methods for item/property counts. Add generics, Javadoc, minor tweaks.

Original pull request: #524.
@mp911de
Copy link
Member

mp911de commented Jan 12, 2018

That's now merged and polished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants