← Back to team overview

dhis2-devs-core team mailing list archive

Validation

 

Hi everyone

I have added a new endpoint for validation of objects, you can now POST or
PUT json/xml to /api/schemas/type to get the object validated.

As an example, say you have '{"name": "a"}' and send it to
/api/schemas/dataElement, you will get back:
[
   {
      "message" : "Required property missing.",
      "property" : "type"
   },
   {
      "property" : "name",
      "message" : "Allowed range for length [2, 230], length is 1"
   },
   {
      "message" : "Required property missing.",
      "property" : "aggregationOperator"
   },
   {
      "message" : "Required property missing.",
      "property" : "domainType"
   },
   {
      "property" : "shortName",
      "message" : "Required property missing."
   }
]

This should be considered WIP, but I could use your help for testing this
out.

This is all backed by new features in schema/property introspector, and
also the new SchemaValidator (which you can use internally)

I have added two new annotations to help with validation:
@Property : This is for setting the type of property, the current list of
valid types are:
IDENTIFIER,
TEXT,
NUMBER,
INTEGER,
BOOLEAN,
EMAIL,
PASSWORD,
URL,
DATE,
PHONENUMBER,
GEOLOCATION,
COLOR,
COMPLEX,
COLLECTION,

So if you set @Property(PropertyType.URL) on a field, it will be validated
as a URL.

@PropertyRange(min=x, max=y) : This annotations if for setting min/max
values for a property, it works a bit different depending on the type:
PropertyType.NUMBER,
PropertyType.INTEGER: min/max value of number

PropertyType.TEXT: min/max length of text

PropertyType.COLLECTION: min/max size of collection (useful to require at
least one member, just set min=1, and maximum will be default)

I have annotated lots of classes already (based on validationRules.js), but
feel free to continue adding validation annotations where you see it
missing.