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
JSON
schema does usingallOf
,anyOf
,dependencies
etc.? - If not, what's the best way to achieve conditional validation in Avro?
- Alternatively, is there a tool to convert a
JSON
schema toAVSC
(I already have aJSON
schema defined with the conditional validation I need)?
For example:
- If
field1
is present, thenfield2
should be present too. field3
should always be present.- If
field2
is present, thenfield4
which is anenum
of[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.