Add title, description, and default fields to schema
[MAILPOET-4195]
This commit is contained in:
@@ -33,6 +33,21 @@ abstract class Schema {
|
|||||||
return $this->unsetSchemaProperty('required');
|
return $this->unsetSchemaProperty('required');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return static */
|
||||||
|
public function title(string $title) {
|
||||||
|
return $this->updateSchemaProperty('title', $title);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return static */
|
||||||
|
public function description(string $description) {
|
||||||
|
return $this->updateSchemaProperty('description', $description);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return static */
|
||||||
|
public function default($default) {
|
||||||
|
return $this->updateSchemaProperty('default', $default);
|
||||||
|
}
|
||||||
|
|
||||||
public function toArray(): array {
|
public function toArray(): array {
|
||||||
return $this->schema;
|
return $this->schema;
|
||||||
}
|
}
|
||||||
|
@@ -423,6 +423,8 @@ class ValidatorTest extends MailPoetTest {
|
|||||||
|
|
||||||
public function testComplex(): void {
|
public function testComplex(): void {
|
||||||
$schema = Builder::object()
|
$schema = Builder::object()
|
||||||
|
->title('User')
|
||||||
|
->description('User schema definition')
|
||||||
->properties([
|
->properties([
|
||||||
'id' => Builder::string()->required()->formatUuid(),
|
'id' => Builder::string()->required()->formatUuid(),
|
||||||
'created_at' => Builder::string()->required()->formatDateTime(),
|
'created_at' => Builder::string()->required()->formatDateTime(),
|
||||||
@@ -431,7 +433,7 @@ class ValidatorTest extends MailPoetTest {
|
|||||||
'email' => Builder::string()->required()->formatEmail(),
|
'email' => Builder::string()->required()->formatEmail(),
|
||||||
'ip' => Builder::string()->required()->formatIp(),
|
'ip' => Builder::string()->required()->formatIp(),
|
||||||
'refresh_interval' => Builder::integer()->required()->multipleOf(3600),
|
'refresh_interval' => Builder::integer()->required()->multipleOf(3600),
|
||||||
'subscribed' => Builder::boolean()->required(),
|
'subscribed' => Builder::boolean()->default(false),
|
||||||
|
|
||||||
// nested object
|
// nested object
|
||||||
'profile' => Builder::object([
|
'profile' => Builder::object([
|
||||||
@@ -490,6 +492,8 @@ class ValidatorTest extends MailPoetTest {
|
|||||||
$this->assertSame(
|
$this->assertSame(
|
||||||
[
|
[
|
||||||
'type' => 'object',
|
'type' => 'object',
|
||||||
|
'title' => 'User',
|
||||||
|
'description' => 'User schema definition',
|
||||||
'properties' => [
|
'properties' => [
|
||||||
'id' => [
|
'id' => [
|
||||||
'type' => 'string',
|
'type' => 'string',
|
||||||
@@ -531,7 +535,7 @@ class ValidatorTest extends MailPoetTest {
|
|||||||
],
|
],
|
||||||
'subscribed' => [
|
'subscribed' => [
|
||||||
'type' => 'boolean',
|
'type' => 'boolean',
|
||||||
'required' => true,
|
'default' => false,
|
||||||
],
|
],
|
||||||
'profile' => [
|
'profile' => [
|
||||||
'type' => 'object',
|
'type' => 'object',
|
||||||
@@ -663,7 +667,7 @@ class ValidatorTest extends MailPoetTest {
|
|||||||
|
|
||||||
$this->assertSame(
|
$this->assertSame(
|
||||||
$schema->toString(),
|
$schema->toString(),
|
||||||
'{"type":"object","properties":{"id":{"type":"string","required":true,"format":"uuid"},"created_at":{"type":"string","required":true,"format":"date-time"},"username":{"type":"string","required":true,"minLength":2,"maxLength":30,"pattern":"^[a-z0-9]+$"},"password":{"type":"string","required":true,"minLength":8,"maxLength":1024},"email":{"type":"string","required":true,"format":"email"},"ip":{"type":"string","required":true,"format":"ip"},"refresh_interval":{"type":"integer","required":true,"multipleOf":3600},"subscribed":{"type":"boolean","required":true},"profile":{"type":"object","properties":{"url":{"type":"string","required":true,"format":"uri"},"photo_url":{"type":["string","null"],"required":true},"color":{"type":"string","required":true,"format":"hex-color"},"age":{"type":"integer","required":true},"rating":{"type":"integer","required":true,"minimum":0,"maximum":5},"score":{"type":"number","required":true,"minimum":0.0,"exclusiveMinimum":true,"maximum":100.0},"distance":{"type":"number","required":true}}},"preferences":{"type":"array","items":{"type":"object","properties":{"key":{"type":"string","required":true},"value":{"anyOf":[{"type":"string"},{"type":"integer"},{"type":"number"}],"required":true},"meta":{"type":"string"}}},"uniqueItems":true},"properties":{"type":"object","patternProperties":{"^number_":{"anyOf":[{"type":"integer"},{"type":"number"}]},"^string_":{"type":"string"},"^bool_":{"type":"boolean"}}},"linked_accounts":{"type":"array","items":{"oneOf":[{"type":"object","properties":{"apple_id":{"type":"string","required":true}}},{"type":"object","properties":{"facebook_id":{"type":"string","required":true}}},{"type":"object","properties":{"google_id":{"type":"string","required":true}}}]}},"attributes":{"type":"object","additionalProperties":{"anyOf":[{"type":"string"},{"type":"integer"},{"type":"boolean"},{"type":"number"},{"type":"null"}]}}}}'
|
'{"type":"object","title":"User","description":"User schema definition","properties":{"id":{"type":"string","required":true,"format":"uuid"},"created_at":{"type":"string","required":true,"format":"date-time"},"username":{"type":"string","required":true,"minLength":2,"maxLength":30,"pattern":"^[a-z0-9]+$"},"password":{"type":"string","required":true,"minLength":8,"maxLength":1024},"email":{"type":"string","required":true,"format":"email"},"ip":{"type":"string","required":true,"format":"ip"},"refresh_interval":{"type":"integer","required":true,"multipleOf":3600},"subscribed":{"type":"boolean","default":false},"profile":{"type":"object","properties":{"url":{"type":"string","required":true,"format":"uri"},"photo_url":{"type":["string","null"],"required":true},"color":{"type":"string","required":true,"format":"hex-color"},"age":{"type":"integer","required":true},"rating":{"type":"integer","required":true,"minimum":0,"maximum":5},"score":{"type":"number","required":true,"minimum":0.0,"exclusiveMinimum":true,"maximum":100.0},"distance":{"type":"number","required":true}}},"preferences":{"type":"array","items":{"type":"object","properties":{"key":{"type":"string","required":true},"value":{"anyOf":[{"type":"string"},{"type":"integer"},{"type":"number"}],"required":true},"meta":{"type":"string"}}},"uniqueItems":true},"properties":{"type":"object","patternProperties":{"^number_":{"anyOf":[{"type":"integer"},{"type":"number"}]},"^string_":{"type":"string"},"^bool_":{"type":"boolean"}}},"linked_accounts":{"type":"array","items":{"oneOf":[{"type":"object","properties":{"apple_id":{"type":"string","required":true}}},{"type":"object","properties":{"facebook_id":{"type":"string","required":true}}},{"type":"object","properties":{"google_id":{"type":"string","required":true}}}]}},"attributes":{"type":"object","additionalProperties":{"anyOf":[{"type":"string"},{"type":"integer"},{"type":"boolean"},{"type":"number"},{"type":"null"}]}}}}'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertValidationPassed(
|
$this->assertValidationPassed(
|
||||||
|
@@ -35,18 +35,46 @@ class SchemaTest extends MailPoetUnitTest {
|
|||||||
$this->assertSame('{"type":"test"}', $schema->toString());
|
$this->assertSame('{"type":"test"}', $schema->toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testTitle(): void {
|
||||||
|
$schema = $this->getTestingSchema()->title('Schema title');
|
||||||
|
$this->assertSame(['type' => 'test', 'title' => 'Schema title'], $schema->toArray());
|
||||||
|
$this->assertSame('{"type":"test","title":"Schema title"}', $schema->toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDescription(): void {
|
||||||
|
$schema = $this->getTestingSchema()->description('Schema description');
|
||||||
|
$this->assertSame(['type' => 'test', 'description' => 'Schema description'], $schema->toArray());
|
||||||
|
$this->assertSame('{"type":"test","description":"Schema description"}', $schema->toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDefault(): void {
|
||||||
|
$schema = $this->getTestingSchema()->default('Default value');
|
||||||
|
$this->assertSame(['type' => 'test', 'default' => 'Default value'], $schema->toArray());
|
||||||
|
$this->assertSame('{"type":"test","default":"Default value"}', $schema->toString());
|
||||||
|
|
||||||
|
$schema = $this->getTestingSchema()->default(null);
|
||||||
|
$this->assertSame(['type' => 'test', 'default' => null], $schema->toArray());
|
||||||
|
$this->assertSame('{"type":"test","default":null}', $schema->toString());
|
||||||
|
}
|
||||||
|
|
||||||
public function testMixedProperties(): void {
|
public function testMixedProperties(): void {
|
||||||
$schema = $this->getTestingSchema()
|
$schema = $this->getTestingSchema()
|
||||||
->required()
|
->required()
|
||||||
->nullable();
|
->nullable()
|
||||||
|
->title('Schema title')
|
||||||
|
->description('Schema description')
|
||||||
|
->default('Default value');
|
||||||
|
|
||||||
$this->assertSame([
|
$this->assertSame([
|
||||||
'type' => ['test', 'null'],
|
'type' => ['test', 'null'],
|
||||||
'required' => true,
|
'required' => true,
|
||||||
|
'title' => 'Schema title',
|
||||||
|
'description' => 'Schema description',
|
||||||
|
'default' => 'Default value',
|
||||||
], $schema->toArray());
|
], $schema->toArray());
|
||||||
|
|
||||||
$this->assertSame(
|
$this->assertSame(
|
||||||
'{"type":["test","null"],"required":true}',
|
'{"type":["test","null"],"required":true,"title":"Schema title","description":"Schema description","default":"Default value"}',
|
||||||
$schema->toString()
|
$schema->toString()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -57,6 +85,9 @@ class SchemaTest extends MailPoetUnitTest {
|
|||||||
$this->assertNotSame($schema->nonNullable(), $schema);
|
$this->assertNotSame($schema->nonNullable(), $schema);
|
||||||
$this->assertNotSame($schema->required(), $schema);
|
$this->assertNotSame($schema->required(), $schema);
|
||||||
$this->assertNotSame($schema->optional(), $schema);
|
$this->assertNotSame($schema->optional(), $schema);
|
||||||
|
$this->assertNotSame($schema->title('Title'), $schema);
|
||||||
|
$this->assertNotSame($schema->description('Description'), $schema);
|
||||||
|
$this->assertNotSame($schema->default(null), $schema);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getTestingSchema(): Schema {
|
private function getTestingSchema(): Schema {
|
||||||
|
Reference in New Issue
Block a user