Types and constraints
Schema describes a value, with the JSON Schema 2020-12 vocabulary of OpenAPI 3.1. Write the 3.1 model; for 3.0 the
library converts what differs (see Conversions).
Types
One shortcut per type; the argument is the objectId (property or component name):
use Ark4ne\OpenApiWriter\Objects\Schema;
Schema::string('name');
Schema::integer('age');
Schema::number('price');
Schema::boolean('active');
Schema::array('tags');
Schema::object('address');
Schema::null();
Schema::create('anything'); // no type
type() sets several types at once, and addType() appends one. Both take the SchemaType enum or its string value:
Schema::create('id')->type('string', 'integer');
Nullable
nullable() works on every schema, including references. Do not add a null type by hand: the library writes the
form each version expects.
Schema::string('subtitle')->nullable();
{ "type": ["string", "null"] }
{ "type": "string", "nullable": true }
Formats and strings
Schema::string('email')->format('email');
Schema::string('code')->minLength(3)->maxLength(8)->pattern('^[A-Z]+$');
Schema::string('published_at')->format('date-time');
Schema::integer('id')->format('int64');
Binary content uses contentMediaType() and contentEncoding():
Schema::string('photo')->contentMediaType('image/png'); // a file
Schema::string('thumbnail')->contentEncoding('base64'); // base64 text
Enumerations and constants
Schema::string('status')
->enum('draft', 'published', 'archived')
->default('draft');
Schema::string('kind')->const('card');
const() is written enum: [card] for OpenAPI 3.0. enum is not deduplicated.
Numbers
Schema::integer('quantity')->minimum(1)->maximum(100);
Schema::number('price')->exclusiveMinimum(0)->multipleOf(0.01);
exclusiveMinimum() and exclusiveMaximum() take the bound itself, as in 3.1. For 3.0 the library writes
minimum: 0, exclusiveMinimum: true.
Arrays
Schema::array('tags')
->items(Schema::string())
->minItems(1)
->uniqueItems();
{
"type": "array",
"items": { "type": "string" },
"minItems": 1,
"uniqueItems": true
}
Also: maxItems(), prefixItems() for tuples, contains() with minContains() / maxContains(), and
unevaluatedItems(). items() accepts a boolean: items(false) forbids any item.
Objects
Properties are schemas keyed by their objectId; required() lists the required names:
Schema::object('User')
->properties(
Schema::integer('id')->format('int64')->readOnly(),
Schema::string('email')->format('email'),
Schema::string('password')->writeOnly(),
Schema::object('metadata')
->additionalProperties(Schema::string()),
)
->required('id', 'email');
required() without names: it writes required: [], which OpenAPI 3.0 rejects.| Method | Purpose |
|---|---|
additionalProperties(SchemaContract|bool|null) | Schema of the other properties; false forbids them |
patternProperties(SchemaContract ...) | Schemas keyed by a regular expression (the objectId) |
propertyNames(SchemaContract|bool|null) | Schema of the property names |
minProperties(?int), maxProperties(?int) | |
dependentRequired(array) | ['credit_card' => ['billing_address']] |
dependentSchemas(SchemaContract ...) | Schema applied when the property (objectId) is present |
unevaluatedProperties(SchemaContract|bool|null) |
Metadata
Schema::string('nickname')
->title('Nickname')
->description('Shown instead of the name')
->example('ada')
->deprecated();
examples() on a schema takes plain values (a JSON Schema list), unlike the examples() of MediaType and
Parameter, which take Example objects. For 3.0, the first value is written as example.
Schema::number('price')->examples(9.99, 20);
Also: readOnly(), writeOnly(), externalDocs(), xml(Xml) and comment() ($comment).
Keywords from OpenAPI 3.1
These keywords come from JSON Schema 2020-12 and have no 3.0 form: prefixItems, contains, minContains,
maxContains, unevaluatedItems, patternProperties, propertyNames, unevaluatedProperties,
dependentRequired, dependentSchemas and $comment. Written for 3.0, they throw an UnsupportedFeatureException
in strict mode, or are dropped in lenient mode; see
Strict and lenient.
Boolean schemas
Wherever JSON Schema accepts a schema, true (anything) and false (nothing) are accepted where the method takes a
bool: items(), additionalProperties(), contains(), not(), if(), then(), else()… For 3.0 they are
written {} and {"not": {}}.