Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added JSON schema validation option to templates (#27)
* Added JSON schema validation option to templates * Fixed code style and change made to wrong file
- Loading branch information
1 parent
6a5fc83
commit 14619cc
Showing
11 changed files
with
250 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Validating parameters with JSON schema | ||
|
||
As templates become more and more complex, the required parameters needed to render an email can become large and complex too. | ||
It's easy to omit a parameter, or forget what parameters are needed to be submitted for any given template. A mistake here, can result in | ||
your template language throwing an error, and a 500 response. | ||
|
||
In order to minimise this, you can optionally define a JSON schema which runtime parameters will be validated against. | ||
|
||
So you've got the hello world working, and you want to try a real template. | ||
|
||
Simply define your JSON schema next to your template's meta file, e.g. `hello-world/hello-world.schema.json`: | ||
|
||
```json | ||
{ | ||
"$schema": "http://json-schema.org/draft-06/schema#", | ||
"properties": { | ||
"email": { | ||
"type": "string", | ||
"format": "email" | ||
}, | ||
"required": ["email"] | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-06/schema#", | ||
"properties": { | ||
"email": { | ||
"type": "string", | ||
"format": "email" | ||
}, | ||
"required": ["email"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Outstack/Enveloper/Resolution/ParametersFailedSchemaValidation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Outstack\Enveloper\Resolution; | ||
|
||
class ParametersFailedSchemaValidation extends \RuntimeException | ||
{ | ||
private $errors; | ||
|
||
public function __construct(array $validationErrors) | ||
{ | ||
parent::__construct("Parameters failed schema validation"); | ||
$this->errors = $validationErrors; | ||
} | ||
|
||
public function getErrors(): array | ||
{ | ||
return $this->errors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tests/data/templates/message-with-attachments/message-with-attachments.schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-06/schema#", | ||
"properties": { | ||
"email": { | ||
"type": "string", | ||
"format": "email" | ||
}, | ||
"attachments": { | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"properties": { | ||
"contents": { | ||
"type": "string", | ||
"minLength": 1 | ||
}, | ||
"filename": { | ||
"type": "string", | ||
"minLength": 1 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |