I'm planning to use Avro's Java API to build out a schema (preferably by using a tool which converts POJOs to AVSC).
- Does Avro schema allow conditional fields like
JSONschema does usingallOf,anyOf,dependenciesetc.? - If not, what's the best way to achieve conditional validation in Avro?
- Alternatively, is there a tool to convert a
JSONschema toAVSC(I already have aJSONschema defined with the conditional validation I need)?
For example:
- If
field1is present, thenfield2should be present too. field3should always be present.- If
field2is present, thenfield4which is anenumof[a,b,c]can only have the value[a,b].
You can represent optional fields in Avro by making the field type a union between the actual type and null, e.g.:
There is no way to make the schema for one field dependent on the value of another field.
Simply make the schema as retrictive as possible and then for any rules that you can't capture in the schema, simply apply them as part of a post-construction validation step.
E.g. taking your examples:
Simply make field1 and field2 optional, and apply the check on field2 during validation.
Again, simply apply the check on field4 during validation.
I doubt it. In any case, even if one did exist, given that Avro Schema is less powerful than JSON Schema, it woudn't solve your problem.