0

When I try to add a document to my couchdb I get the following error:

{case_clause, {[{<<"error">>, <<"invalid value">>}, {<<"reason">>, [<<"you need to give the ticket a ticketnumber">>]}]}}}

My validate_doc_update method looks like this:

function (newDoc, oldDoc, usrCtx) {
    var error = { reason: [], error: '' }
    switch (newDoc.type) {
        case 'application':
            if (!newDoc.name) {
                error.error = "invalid value"
                error.reason.push("you need to give the application a name")
            }
            break;
        case 'client':
            if (!newDoc.name) {
                error.error = "invalid value"
                error.reason.push("you need to give the client a name")
            }
            break;
        case 'department':
            if (!newDoc.name) {
                error.error = "invalid value"
                error.reason.push("you need to give the department a name")
            }
            break;
        case 'release':
            if (!newDoc.name) {
                error.error = "invalid value"
                error.reason.push("you need to give the release a name")
            }
            break;
        case 'ticket':
            if (!newDoc.ticketnumber) {
                error.error = "invalid value"
                error.reason.push("you need to give the ticket a ticketnumber")
            }
            if (!newDoc.description) {
                error.error = "invalid value"
                error.reason.push("you need to give the ticket a description")
            }
            if (newDoc.priority && !/\d*[,|.]\d*/.test(newDoc.priority)) {
                error.error = "invalid value"
                error.reason.push("the priority is invalid")
            }
            if (!newDoc.minutesperweek) {
                error.error = "invalid value"
                error.reason.push("you need to give the ticket a impact in minutes per week")
            } else if (!/\d*[,|.]?\d*/.test(newDoc.minutesperweek)) {
                error.error = "invalid value"
                error.reason.push("the impact in minutes per week is invalid")
            }
            if (!newDoc.ordervolume) {
                error.error = "invalid value"
                error.reason.push("you need to give the ticket a impact in orders per week")
            } else if (!/\d*/.test(newDoc.ordervolume)) {
                error.error = "invalid value"
                error.reason.push("the impact in orders per week is invalid")
            }
            break;
        case 'worker':
            if (!newDoc.firstname) {
                error.error = "invalid value"
                error.reason.push("you need to give the worker a firstname")
            }
            if (!newDoc.lastname) {
                error.error = "invalid value"
                error.reason.push("you need to give the worker a lastname")
            }
            if (!newDoc.emailaddress) {
                error.error = "invalid value"
                error.reason.push("\r\nyou need to give the worker an emailaddress")
            } else if (!/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(newDoc.emailaddress)) {
                error.error = "invalid value"
                error.reason.push("the emailaddress is invalid")
            }
            if (!newDoc.phonenumber) {
                error.error = "invalid value"
                error.reason.push("you need to give the worker a phonenumber")
            } else if (!/\d/g.test(newDoc.phonenumber)) {
                error.error = "invalid value"
                error.reason.push("the phonenumber is invalid")
            }
            break;
    }
    if (error.error != '') {
        throw { 'error': error.error, 'reason': error.reason }
    }
}

I would expect, that I get as result this:

{ 'error': 'Invalid value', 'reason': 'you need to give the ticket a ticketnumber' }

But instead I get the error that I wrote in the beginning. What is my problem?

I use cradle and node.js, if it matters.

Knerd
  • 1,892
  • 3
  • 28
  • 55

1 Answers1

2

validate_doc_update functions are only expected to throw forbidden or unauthorized errors (for 403 and 401 HTTP responses respectively). Try:

throw({forbidden: { 'error': error.error, 'reason': error.reason }});
Kxepal
  • 4,659
  • 1
  • 19
  • 16
  • According to [this](https://github.com/flatiron/cradle#creating-validation) it should work. What is the alternative that I have? – Knerd Nov 10 '14 at 15:17
  • 1
    This example is invalid and leads to the same behaviour. You can check it by yourself. – Kxepal Nov 10 '14 at 19:15
  • Ok, but still, how can I handle validation on couch site then? – Knerd Nov 10 '14 at 19:53
  • 1
    Same as you'd planned. With given fix CouchDB will respond you HTTP 403 Forbidden and include the following JSON in response body: `{"error":"forbidden","reason":{"error":"invalid value","reason":"you need to give the ticket a ticketnumber"}}` - object structure looks weird, but I think you'll figure out how to improve it (; So on site do validation as you'd planned, just pick the error/reason from "reason" top level field. – Kxepal Nov 10 '14 at 20:58