insertqueries. As part of these two operations, a
ManagedObject<T>will ensure that its properties have valid values. For example, a
Personobject might ensure that its name starts with a capital letter and that its phone number has only numeric values. If one or more validation fails, the update or insert operation will fail and the data is not sent to the database. A validation failure will throw a
QueryExceptionthat returns an HTTP response with error messaging to help the client correct their request.
Validatemetadata to properties of a table definition. Here's an example of a validation that ensures a tweet is less than 140 characters:
Validateclass. Here is an example:
stateproperty must be one of the four listed strings. If a value other than one of those four strings is used, the error message returned to the HTTP client would be:
Validateand its named constructors for possible options.
Validateannotations on properties declared in a managed object subclass (transient properties) have no effect.
Validateto provide custom validation behavior. For example, if there were a
Validate.validate()and call the superclass' primary constructor when instantiated. Here's an example of that phone number validator:
valueis doesn't meet the validation criteria, this method adds an error string to the
ValidationContextit is passed. Error messages should be brief and indicate the successful criteria that failed. Information about the property being validated will automatically be added to the error message, so you do not need to include that information. If the context has no errors at the end of validation, the validation succeeds; otherwise, it fails.
ValidationContextalso has information about the property being validated, and whether the validation is running for an object being inserted or an object being updated.
Validatemetadata. In this case, all of the validations for a property must pass. The order in which multiple validations are performed is undefined and subject to change. Here's an example of validations that ensure a property's value is 10 characters long and only contains 10 alphabetic capital letters:
updatemethod is invoked. A validator can be restricted to only run on
updateby passing values for its optional constructor arguments
Query.values, validations will not be run on that property.
Validate.present()requires that the associated property must have a value. A property with this validator must be provided each time the object is inserted or updated. For example, the following declaration requires that
Validate.absent(). This validation prevents a property from being set. This is useful when a value should be included during insertion, but can't be updated. Here's an example:
canOnlyBeSetOncedoes not have a value. Because this validator is not run on insert operations, there is no restriction when the object is first inserted.
nullfor the property
Column.isNullableproperty. Consider the following declaration:
namemust not be null and must be greater than 10 characters long. The behavior of inserting or updating this property is shown in the following table.
ManagedObject<T>instances to be partially updated, which also allows for partial PUTs. Partial PUTs can be prevented by adding
Validate.present()metadata to all properties.
Validate, you may override
ManagedObject<T>.validate(). This method is useful when a validation involves more than one property. Here's an example:
superimplementation must be invoked to run validations managed by annotations. You must return the
ValidationContextcreated by the superclass' implementation.
Query<T>.values. Values set via
Query<T>.valueMapare not validated and is useful for inserting data without validation. Here's an example of skipping validation:
ManagedObject<T>subclasses may override
willInsertto make changes prior to being updated or inserted. For example, a managed object may have updated and created dates that can be guaranteed to be set when inserted or updated:
willInsertare run before any validation occurs. Like validations,
willInsertare skipped when using