4

有没有办法为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 架构相关联?如果我完全在错误的地方,那么请有人指出这一点

4

1 回答 1

1

我偶然发现了您在 GitHub 上的帖子,试图找到一种将自定义消息添加到“anyOf”、“oneOf”子句的解决方案。这对我有用:见这里

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];
});

你的模式定义是这样的:

{
"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"
}
}
于 2016-11-01T20:40:20.523 回答