In Laravel 6, Lighthouse 5, Module, use of custom validation rules
1. At this stage, you need to implement an interface to delete the cache ID in the GraphQL API. Refer to the Shopify example. When the cache ID does not exist, the response fails. as shown in Figure 1
mutation DeleteThemeSessionId($sessionId: String!) {
onlineStoreEditorSessionDelete(sessionId: $sessionId) {
deletedSessionId
userErrors {
field
message
__typename
}
__typename
}
}
{
"sessionId": "H9Mc8FwH5Eu5LR37XAzsehh4"
}
{
"data": {
"onlineStoreEditorSessionDelete": {
"deletedSessionId": null,
"userErrors": [
{
"field": [
"sessionId"
],
"message": "在线商店编辑器访问不存在",
"__typename": "UserError"
}
],
"__typename": "OnlineStoreEditorSessionDeletePayload"
}
},
"extensions": {
"cost": {
"requestedQueryCost": 10,
"actualQueryCost": 10,
"throttleStatus": {
"maximumAvailable": 1000,
"currentlyAvailable": 990,
"restoreRate": 50
}
}
}
}
2. Due to the rule to verify whether this cache ID exists, it will be used in a large number of APIs. The most deciding to customize the validation rule to reuse this verification rule.
3. Because the verification rules are written in the module. Module: make-rule create a new validation rule for the specific module.
PS E:\wwwroot\object> php artisan help module:make-rule
Description:
Create a new validation rule for the specified module.
Usage:
module:make-rule []
Arguments:
name The name of the rule class.
module The name of module will be used.
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--env[=ENV] The environment the command should run under
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
PS E:\wwwroot\object> php artisan module:make-rule ThemeEditor/ThemeEditorCodeExistsRule ThemeStore
Created : E:/wwwroot/object/Modules/ThemeStore/Rules/ThemeEditor/ThemeEditorCodeExistsRule.php
PS E:\wwwroot\object>
4. Edit the rule class to determine whether the cache ID exists. /modules/themestore/rules/themeeditor/themeeditorCodeExistsRule.php
has($value);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The validation error message.';
}
}
5. Edit /themestore/resources/graphql/theme_editor.graphql . Use the @rules directive to refer to a custom validation rule with a fully qualified class name: ThemeEditorCodeExistsRule
extend type Mutation {
...
"删除主题编辑标识"
onlineStoreThemeEditorCodeDelete(themeEditorCode: String! @rules(apply: ["Modules\\ThemeStore\\Rules\\ThemeEditor\\ThemeEditorCodeExistsRule"])): ThemeEditorCodeDeletePayload @field(resolver: "Modules\\ThemeStore\\Resolver\\ThemeEditor\\DeleteThemeEditorCodeResolver")
}
type ThemeEditorCodeDeletePayload
{
deletedThemeEditorCode: String
}
6. A new cache ID is generated: vrybn7nqiewmbh2g6sxb8ae4aobik4jqow2p, first test the verification failure.
mutation {
onlineStoreThemeEditorCodeDelete(themeEditorCode: "vRYbn7NQiEWMbH2G6sXB8AE4aoBIk4JQOw2p0") {
deletedThemeEditorCode
}
}
{
"errors": [
{
"message": "Validation failed for the field [onlineStoreThemeEditorCodeDelete].",
"extensions": {
"validation": {
"themeEditorCode": [
"The validation error message."
]
},
"category": "validation"
},
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"onlineStoreThemeEditorCodeDelete"
],
"trace": [
...
]
}
],
"data": {
"onlineStoreThemeEditorCodeDelete": null
}
}
7. The /modules/themestore/resources/lang/en/validation.php language file under the new module.
[
'theme_editor_code' => [
'theme_editor_code_exists_rule' => 'The selected :attribute is invalid.',
],
],
'attributes' => [],
];
8. Edit the rules class, /modules/themestore/rules/themeeditor/themeeditorCodeExistsRule.php. Returns an error message from the translation file, calling the helper function trans from the message method.
has($value);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return trans('theme_store::validation.custom.theme_editor_code.theme_editor_code_exists_rule');
}
}
9. Test the verification failure again. The Selected Theme Editor Code is invalid. as shown in Figure 2
mutation {
onlineStoreThemeEditorCodeDelete(themeEditorCode: "vRYbn7NQiEWMbH2G6sXB8AE4aoBIk4JQOw2p0") {
deletedThemeEditorCode
}
}
{
"errors": [
{
"message": "Validation failed for the field [onlineStoreThemeEditorCodeDelete].",
"extensions": {
"validation": {
"themeEditorCode": [
"The selected theme editor code is invalid."
]
},
"category": "validation"
},
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"onlineStoreThemeEditorCodeDelete"
],
"trace": [
...
]
}
],
"data": {
"onlineStoreThemeEditorCodeDelete": null
}
}
10. The /modules/themestore/resources/lang/zh_cn/validation.php language file under the new module.
[
'theme_editor_code' => [
'theme_editor_code_exists_rule' => '所选的主题编辑标识无效。',
],
],
'attributes' => [],
];
11. When the current language area is set to: zh_cn, the response to the test failure, and the response information is Chinese. in line with expectations. as shown in Figure 3
{
"errors": [
{
"message": "Validation failed for the field [onlineStoreThemeEditorCodeDelete].",
"extensions": {
"validation": {
"themeEditorCode": [
"所选的主题编辑标识无效。"
]
},
"category": "validation"
},
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"onlineStoreThemeEditorCodeDelete"
],
"trace": [
...
]
}
],
"data": {
"onlineStoreThemeEditorCodeDelete": null
}
}
12. You can copy /modules/themestore/resources/lang/en/validation.php as /modules/themestore/resources/lang/en_us/validation.php. /en_us/validation.php has a higher priority than /en/validation.php when the locale is set to en_us . If it cannot be found in /en_us/validation.php , it will look from /en/validation.php . Test failure response.
[],
'attributes' => [],
];
{
"errors": [
{
"message": "Validation failed for the field [onlineStoreThemeEditorCodeDelete].",
"extensions": {
"validation": {
"themeEditorCode": [
"The selected theme editor code is invalid."
]
},
"category": "validation"
},
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"onlineStoreThemeEditorCodeDelete"
],
"trace": ...
}
],
"data": {
"onlineStoreThemeEditorCodeDelete": null
}
}
13. Note: The name of Theme_Store is derived from /themestore/providers/themestoreServiceProvider.php
private $moduleNameLower = 'theme_store';
14. The test verification is in line with expectations. as shown in Figure 4
mutation {
onlineStoreThemeEditorCodeDelete(themeEditorCode: "vRYbn7NQiEWMbH2G6sXB8AE4aoBIk4JQOw2p") {
deletedThemeEditorCode
}
}
{
"data": {
"onlineStoreThemeEditorCodeDelete": {
"deletedThemeEditorCode": "vRYbn7NQiEWMbH2G6sXB8AE4aoBIk4JQOw2p"
}
}
}



