5

Is there a way to set a custom message for json schema (tv4) for when it fails at a oneOf field?

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

[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, e.g.

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");. How do I associate this error with this specific json schema? And if I'm completely in the wrong place, then somebody also point that out please

Jeremy
  • 1,717
  • 5
  • 30
  • 49
  • Aside from the fact that that feature is missing from the specification, the [error reporter](https://github.com/geraintluff/tv4/blob/master/source/error-reporter.js) seems to simply grab the message from a [simple dictionary](https://github.com/geraintluff/tv4/blob/master/source/api.js#L136) (defaulting to [English messages](https://github.com/geraintluff/tv4/blob/master/source/api.js#L42)). –  Jun 22 '16 at 19:30

1 Answers1

1

I stumbled upon your post from GitHub, trying to find a solution for adding custom message to "anyOf", "oneOf" clauses. 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"
}
}