有没有办法为json模式(tv4)设置自定义消息,以便在oneOf
字段失败时?
我看到大约一年前这里和这里有一个自定义消息的问题,但是有没有办法让这个工作适合这样的事情?
{
"id": "code",
"description": "Schema for request.body - pin for logging into the bank",
"oneOf": [
{
"type": "string",
"pattern": "^.*\\S.*$"
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"pattern": "^(encrypted|not_encrypted)$"
},
"value": {
"type": "string",
"pattern": "^.*\\S.*$"
}
}
}
],
"messages": {
"oneOf": "Code does not match schema"
}
}
与仅仅看到相反Data does not match any schemas from \"oneOf\"
,你可以看到Code does not match schema
[编辑]
我正在尽我所能阅读 tv4 代码,并且我看到了很多关于自定义错误消息和代码的信息,例如
defineError: function (codeName, codeNumber, defaultMessage) {
if (typeof codeName !== 'string' || !/^[A-Z]+(_[A-Z]+)*$/.test(codeName)) {
throw new Error('Code name must be a string in UPPER_CASE_WITH_UNDERSCORES');
}
if (typeof codeNumber !== 'number' || codeNumber%1 !== 0 || codeNumber < 10000) {
throw new Error('Code number must be an integer > 10000');
}
if (typeof ErrorCodes[codeName] !== 'undefined') {
throw new Error('Error already defined: ' + codeName + ' as ' + ErrorCodes[codeName]);
}
if (typeof ErrorCodeLookup[codeNumber] !== 'undefined') {
throw new Error('Error code already used: ' + ErrorCodeLookup[codeNumber] + ' as ' + codeNumber);
}
ErrorCodes[codeName] = codeNumber;
ErrorCodeLookup[codeNumber] = codeName;
ErrorMessagesDefault[codeName] = ErrorMessagesDefault[codeNumber] = defaultMessage;
for (var langCode in languages) {
var language = languages[langCode];
if (language[codeName]) {
language[codeNumber] = language[codeNumber] || language[codeName];
}
}
},
我可以用它自己的状态代码(通过这个函数)通过一个简单的行添加我自己的错误tv4.defineError('MY_CUSTOM_CODE_ERROR', 999999, "Hello World, you have a custom code error");
。如何将此错误与此特定 json 架构相关联?如果我完全在错误的地方,那么请有人指出这一点