gpt4 book ai didi

Validator failing for not defined param(未定义参数的验证器失败)

转载 作者:bug小助手 更新时间:2023-10-22 15:46:39 29 4
gpt4 key购买 nike



I am using Validator to validate the http request with a defined rule. I have added one rule for GET requests that checks the value of the mode in the input parameter.mode is not a required input parameter.

我正在使用Validator使用定义的规则验证http请求。我为GET请求添加了一条规则,用于检查输入参数中模式的值。模式不是必需的输入参数。


 getGameModes: {
mode: `in:${GAME_MODES.COMPETITIVE},${GAME_MODES.FRIENDLY},${GAME_MODES.TUTORIAL}`
}

Validator works properly for following request, throw errors if not satisfy the rule definition or return success.

Validator对以下请求正常工作,如果不满足规则定义则抛出错误或返回成功。


http://localhost:3000/getGameModes?mode=COMPETITIVE
http://localhost:3000/getGameModes?mode=COMPETITIVE&someOther=test
http://localhost:3000/getGameModes?mode=NOT_EXISTS_MODE
http://localhost:3000/getGameModes

But the validator fails and throw the error for the below request where the mode parameter does not exist in the request but we have other params in the request (someOther)

但是验证器失败,并为以下请求抛出错误,其中模式参数不在请求中,但我们在请求中有其他参数(someOther)


http://localhost:3000/getGameModes?someOther=test

Error

错误


"message": "The selected mode is invalid."

Can anyone tell me how to fix this? I want to apply the rule only when the request parameter contains the mode parameter in the request.

有人能告诉我怎么解决这个问题吗?我只想在请求参数包含请求中的模式参数时应用该规则。


更多回答

Looking at Laravel's doc it has validator called sometimes which you can use like mode: `sometimes | in:${GAME_MODES.COMPETITIVE}, ..other modes`

查看Laravel的文档,它有时会调用验证器,您可以使用类似模式的验证器:“有时| in:${GAME_MODES.COMPATIVE},..其他模式`

@CodeThing, still getting same error. rule - ``` mode: sometimes|in:${GAME_MODES.COMPETITIVE},${GAME_MODES.FRIENDLY},${GAME_MODES.TUTORIAL}```

@CodeThing,仍然得到相同的错误。规则-“``模式:有时|在:${GAME_MODES.COMPETIVE},${GAME_MODES.FRIENDLY},${GAME_MODES.TUTORIAL}```

优秀答案推荐

You can try adding custom validation if the sometimes rule doesn't work for you. In the doc they have example of using custom rule.

如果有时规则不适用,您可以尝试添加自定义验证。在文档中,他们有使用自定义规则的示例。


So you can try something like this.

所以你可以试试这样的东西。


const getGameModes = {
mode: 'gamemode' // Instead of **in** we use custom rule here
};

// Callback to handle custom rule
function validateGameMode(name, value, params) {
const validGameModes = [GAME_MODES.COMPETITIVE, GAME_MODES.FRIENDLY, GAME_MODES.TUTORIAL];
return (value !== '') ? validGameModes.includes(value) : true;
}

const v = Validator.make(data, getGameModes);
// custom rule definition
v.extend('gamemode', validateGameMode, ':attr is not a valid game mode');

更多回答

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com