Invoices and Receipts in One Endpoint Without Mixing Schemas
An interface that accepts both invoices and receipts at the same time can easily result in a large object with “all fields optional.” Such an object can accommo
Invoices and receipts often arrive through the same interface, but mixing their fields into one loose object makes the data hard to check. Developers who handle both document types will find a clearer pattern here.
The main problem is a single object with "all fields optional." It accepts almost anything, so it becomes hard to say exactly what was received. This guide explains how to keep two separate contracts and use an explicit type field to choose between them. It also covers error messages and what to do when the document type is unclear.
Should invoices and receipts share one endpoint? The short answer
Yes, but only if each document type is validated against its own contract, selected by an explicit kind field.
| Your situation | What to do | Why |
|---|---|---|
| Invoices and receipts share one interface | Keep two separate contracts and select one with a required kind field | A single object with all fields optional accepts anything and makes the input hard to specify |
| Branches have only a few optional fields | Add a required kind plus const to each branch | Overlapping optional fields can make valid inputs fail under oneOf |
| A record contains fields from both types | Reject it and show which fields conflict and where they came from | Mixed structures should not pass as valid |
| The classifier cannot tell the type apart | Keep the document in an unknown state for review | Forcing a branch can produce a false "success" |
| Documents will be translated | Pass the document type along with the delivery list | The same field can need different terminology across document types |
Why one large optional object causes problems
A catch-all object can hold any combination of fields, but that flexibility hides what the input actually is. When every field is optional, you cannot tell from the schema whether a record is an invoice, a receipt, or something else.
Separate contracts make the expected structure explicit. Each type gets its own rules, and a required type field tells the validator which rules to apply.
How oneOf works with typed branches
oneOf needs exactly one match
oneOf must match exactly one branch. If a record matches two branches at once, that also counts as a failure.
Overlapping optional fields cause false failures
When a branch has only a few optional fields, those fields may overlap heavily with the other branch. Valid inputs can then fail instead of passing.
A required kind plus const removes the overlap
Adding a required kind with a const value to each branch separates the branches in advance, so a record can match only the branch its type points to.
An executable diversion protocol
from jsonschema import Draft202012Validator
def branch(kind, identity):
return {
"type": "object", "additionalProperties": False,
"required": ["kind", identity],
"properties": {"kind": {"const": kind},
identity: {"type": "string", "minLength": 1}}
}
schema = {"oneOf": [branch("invoice", "invoice_number"),
branch("receipt", "receipt_number")]}
v = Draft202012Validator(schema)
valid = lambda x: not list(v.iter_errors(x))
assert valid({"kind": "invoice", "invoice_number": "I-1"})
assert valid({"kind": "receipt", "receipt_number": "R-1"})
assert not valid({"kind": "invoice", "receipt_number": "R-1"})
assert not valid({"kind": "receipt", "receipt_number": ""})
assert not valid({"kind": "invoice", "invoice_number": "I-1",
"receipt_number": "R-1"})
assert not valid({"kind": "unknown"})
This is the smallest record constructed for the demonstration. It is not a complete field model of a real invoice. The code shows branch selection and the rejection of extra fields. Tax rates, headings, and amounts must be defined separately.
If your business allows additional fields, specify their namespaces explicitly. Do not quietly remove all constraints to make validation pass.
Error messages should be specific
A generic "mismatch" message does not help users fix their input. Give feedback that matches the problem:
| Input feature | Likely cause | Suggested feedback |
|---|---|---|
kind is missing | The system cannot select a contract | Please provide the document type |
| Type and number fields conflict | Two structures are mixed | Show the conflicting fields and where they came from |
| Numbered field is an empty string | The key exists but has no value | Check the ID range |
| Unknown type | The current contracts do not cover it | Send it to a separate queue for classification |
When the document type is uncertain
If the classifier cannot distinguish an invoice from a receipt, do not pick a branch at random. Keep the document in an unknown state so it can be reviewed. Forcing a choice can produce a "success" result that is wrong.
Three separate checks
Keep these three levels distinct:
- Classification confidence: how sure the system is about the document type.
- Structural validation: whether the record matches the chosen contract.
- Business authenticity: whether the document is genuine.
Structural validation cannot prove that the classification was correct. It also cannot verify that the document is authentic.
Keep the document type during translation
The same field may need different terms in different documents. When you handle terminology and bilingual review, send the document type with the delivery list. This helps the reviewer understand the context of the numbers and amounts. Do not let a generic JSON object hide these distinctions.
You can start with OmniDoc (product entry point) when you need terminology support and a bilingual review workflow.
📊 At a glance
| Approach | What it accepts | Main risk |
|---|---|---|
| One object with all fields optional | Almost anything | Hard to tell what was received |
Two contracts with a required kind | Only records that match the selected type | Needs a clear type field from the start |
oneOf with overlapping optional fields | Valid inputs may fail | Overlap makes exactly-one matching unreliable |
🎯 Which approach fits you?
If you handle both invoices and receipts through one interface
Use two separate contracts and require a kind field to select between them.
If your branches share many optional fields
Add kind with const to each branch so that oneOf can match exactly one.
If the classifier is not sure about a document
Keep it in an unknown state for review instead of forcing it into a branch.
If you translate these documents
Send the document type with the delivery list so the reviewer knows what the numbers mean.
❓ FAQ
1. Why not use one object with all fields optional?
It accepts almost anything, so it is difficult to specify exactly what was received.
2. What does oneOf require?
It must match exactly one branch. Matching two branches counts as a failure.
3. How does a required kind field help?
Combined with const, it separates the branches in advance so that a record matches only the branch for its type.
4. What should happen when the document type is unclear?
Keep the document in an unknown state. Do not pick a branch that could produce a false success.
5. Does structural validation prove a document is genuine?
No. It checks the record's structure and cannot verify the classification or the document's authenticity.
📝 Summary
Keep invoices and receipts in separate contracts, select between them with an explicit kind field, and reject records that mix structures. Treat uncertain classifications as unknown rather than forcing them. For the schema rules behind this approach, see JSON Schema Combination Rules.
Try it now: upload a document and get it translated with the layout intact


