[英]JSON Schema custom message for oneOf
Is there a way to set a custom message for json schema (tv4) for when it fails at a oneOf
field? 有没有办法为json架构(tv4)设置一个自定义消息,以便它在oneOf
字段失败?
I saw that there was an issue opened for custom messages about a year ago here and here but is there a way to make this work for something like this? 我看到大约一年前在这里和这里打开自定义消息的问题,但有没有办法让这个工作为这样的东西?
{
"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"
}
}
as opposed to just seeing Data does not match any schemas from \\"oneOf\\"
, you could see Code does not match schema
而不是只看到Data does not match any schemas from \\"oneOf\\"
,您可能会看到Code does not match schema
[EDIT] [编辑]
I am reading through the tv4 code as best as I can and I am seeing a lot about custom error messages and codes, eg 我尽可能地通过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];
}
}
},
I can add my own error with it's own status code (via this function) by one simple line tv4.defineError('MY_CUSTOM_CODE_ERROR', 999999, "Hello World, you have a custom code error");
我可以通过一个简单的行tv4.defineError('MY_CUSTOM_CODE_ERROR', 999999, "Hello World, you have a custom code error");
添加我自己的错误及其自己的状态代码(通过此函数tv4.defineError('MY_CUSTOM_CODE_ERROR', 999999, "Hello World, you have a custom code error");
. 。 How do I associate this error with this specific json schema? 如何将此错误与此特定json架构相关联? And if I'm completely in the wrong place, then somebody also point that out please 如果我完全在错误的地方,那么有人也会指出这一点
I stumbled upon your post from GitHub, trying to find a solution for adding custom message to "anyOf", "oneOf" clauses. 我偶然发现了GitHub的帖子,试图找到一个解决方案,为“anyOf”,“oneOf”条款添加自定义消息。 Here is what worked for me: See here 这对我有用:见这里
tv4.setErrorReporter(function (error, data, schema) {
// Last component of schemaPath, which *most* of the time is the keyword!
var lsP = error.schemaPath.split('/').splice(-1);
return schema.messages && schema.messages[lsP];
});
and your schema definition goes like this: 你的架构定义是这样的:
{
"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"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.