Schema Validation

Morphia provides annotations that allow developers to define document validations for a collection to be defined alongside the other mapping data on an entity’s source. Schema validationprovides the capability to perform schema validation during updates and insertions. Validation rules are on a per-collection basis and can be defined via annotations just like indexes are.

Below we have a basic entity definition. Note the new annotation @Validation.

@Validation("{ number : { $gt : 10 } }")
public class DocumentValidation {
    private ObjectId id;
    private String string;
    private int number;
    private Date date;

In this case, only one value is supplied to the annotation. This string value is the query that will be used to match against any new documents or updated documents. Should this query fail to match the new document, validation will fail on the document and it will be rejected. In addition to the required query, there are two other values that can be configured based on your needs: level with a default of STRICT and action with a default of ERROR. A MODERATE validation level does not apply rules to updates on existing invalid documents. An action setting of WARN will merely log any validation violations.