All Of
The allOf keyword can be used to combine multiple subschemas. The provided value must be valid against each of the subschemas.
{
"$id": "example",
"type": "object",
"properties": {
"example": {
"allOf": [
{
"type": "number",
"multipleOf": 5
},
{
"type": "number",
"multipleOf": 3
}
]
}
}
}
Valid values are eg. 15, 30, 45. Invalid values are eg. 1, 2, 3, 4, 5 or any non numeric values.
Generated interface:
public function setExample(float $example): static;
public function getExample(): float;
Possible exception (eg. if a string is provided):
Invalid value for 'example' declined by composition constraint
Requires to match all composition elements but matched 0 elements
- Composition element #1: Failed
* Invalid type for 'example': requires 'float', got 'string'
- Composition element #2: Failed
* Invalid type for 'example': requires 'float', got 'string'
Possible exception (if eg. 5 is provided, which matches only one subschema):
Invalid value for 'example' declined by composition constraint
Requires to match all composition elements but matched 1 element
- Composition element #1: Valid
- Composition element #2: Failed
* Value for 'example' must be a multiple of 3
The thrown exception will be a PHPModelGenerator\Exception\ComposedValue\AllOfException which provides the following methods to get further error details:
// returns a two-dimensional array which contains all validation exceptions grouped by composition elements
public function getCompositionErrorCollection(): array
// get the amount of succeeded composition elements
public function getSucceededCompositionElements(): int
// get the name of the property which failed
public function getPropertyName(): string
// get the value provided to the property
public function getProvidedValue()
// get the JSON pointer to the schema keyword that rejected the value
public function getJsonPointer(): JsonPointer
Hint
When combining multiple nested objects with an allOf composition the property is typed with a single generated class holding all of their properties. Unlike anyOf/oneOf this is a regular nested class rather than a merged property: an allOf of object branches guarantees that every valid value is an object, so it is routed through the ordinary object path — see Composition-implied objects.
Hint
An allOf branch does not need to declare "type": "object" itself to be treated as an
object — the generator also detects object-ness implied by a $ref chain or nested
allOf, and object-constraining keywords used without any type at all. See
Composition-implied objects for the full explanation, including why
an object-describing branch (bare properties/required, no type) does not
trigger the conflicting-types check below the way an object-asserting branch does.
Note
allOf branches can be the boolean literals true or false.
truebranch — treated as an empty schema; any value satisfies it and it adds no constraint. The generator emits a generation-time warning that the branch carries no validation keyword and matches any value.falsebranch — makes the whole composition unsatisfiable; any provided value raises anAllOfExceptionat runtime (the false branch is represented as an always-failing composition element). The generator also emits a warning at generation time. Absent optional properties are still allowed.
An empty {} branch, and a branch carrying only annotation keywords such as example,
behave identically to true at runtime — they impose no constraint either. They are also
warned about, but only where the branch does not inherit a type from the enclosing schema:
a branch that declares no type of its own inherits the outer one, which counts as a
constraint and suppresses the warning. Since a schema root always has "type": "object"
applied to it, an empty branch in a root-level composition does not currently warn, while a
true branch there does (boolean branches are not subject to the inheritance).
Note
When a property is defined in multiple allOf branches with conflicting types (e.g. one branch
requires string and another requires integer), the generator will throw a SchemaException
at generation time. allOf requires all constraints to hold simultaneously, so conflicting types
make the schema unsatisfiable. When branches agree on a type, the generator narrows the property to
the intersection of all declared types across branches. See
Cross-typed compositions for the full explanation and a contrast
with anyOf/oneOf union widening.
Note
For object-level allOf compositions, when a property appears in the required array of
any branch, the generator promotes that property to non-nullable in the generated class. All
allOf branches must hold simultaneously, so any branch’s required constraint is effectively
global. See Cross-typed compositions for the full promotion rules.
Note
Properties in object-level allOf branches may carry a "default" value. Because all
branches apply simultaneously, defaults from every branch are combined. When multiple branches
define a default for the same property, those defaults must agree; the generator throws a
SchemaException at generation time if they differ.
See Default values for the full explanation.