Search code examples
node.jsjoi

JOI reports "AssertionError: Invalid schema content"


Here is the error after I added .unique to order schema with platform_order_id:

AssertionError: Invalid schema content: (platform_order_id)

Here is function validateOrder:

function validateOrder(order) {
    const schema = {
    customer_name: Joi.string()
        .required(),
    amount_rmb: Joi.number()
        .precision(2)
        .required(), 
    amount_crypto: Joi.number()
        .precision(2)
        .required(), 
    platform_order_id: Joi.number()
        .required()
        .unique,  //<== causing error
    x_rate: Joi.number()
        .required()
    };
    return Joi.validate(order, schema);
};

The order.platform_order_id is 149345. Tried Number('149345') and the error is the same. Without .unique, the validate works fine. What am I missing here?


Solution

  • .unique() is for Array type, not for Number type.

    and system level uniqueness should be validated after joi validation, I think.