How to validate certain fields of a nested json object using JOI validation












0















I need help regarding how to validate certain fields of a nested json object using JOI validation. In my example I have an object which contains two sub objects i.e. clientObj and agentObj. I'm only interested in validating the username field which is required but I don't want to validate the remaining fields. If I only mention that field, by deleting all other fields, in my schema and joi.validate() function I get 422 error. Code is given below:



exports.callAuthentication = function (req, res, next) {

let connectSchema = {
clientObj: joi.object().keys({
name: joi.string().min(3).max(38),
email: joi.string().min(3).max(38),
language: joi.string().min(3).max(38),
username: joi.string().min(3).max(38).required(),
mobile_no: joi.string().min(3).max(38),
time_zone: joi.string().min(3).max(38),
system_phone: joi.string().optional().allow('').min(3).max(38),
phone_no_info: joi.any().optional().allow(''),
voicemail_pin: joi.string().min(3).max(38),
display_picture: joi.string().min(3).max(38),
external_extension: joi.string().min(3).max(38)

}),
agentObj: joi.object().keys({
userId: joi.number(),
username: joi.string().min(3).max(38).required(),
name: joi.string().min(3).max(38),
email: joi.string().min(3).max(38),
status: joi.string().min(3).max(38),
role: joi.string().min(3).max(38)
})
};

const data = req.body;

joi.validate(data, connectSchema, (err) => {
if (err) {
// send a 422 error response if validation fails
res.status(422).json({
status: 'error',
message: err.details[0].message
});
} else {
req.body = data;
next();
}
});
}









share|improve this question

























  • What error message does Joi return?

    – Ankh
    Nov 28 '18 at 9:26











  • Error received is 422 Unprocessable Entity.

    – Aamer Rasheed
    Nov 28 '18 at 12:59
















0















I need help regarding how to validate certain fields of a nested json object using JOI validation. In my example I have an object which contains two sub objects i.e. clientObj and agentObj. I'm only interested in validating the username field which is required but I don't want to validate the remaining fields. If I only mention that field, by deleting all other fields, in my schema and joi.validate() function I get 422 error. Code is given below:



exports.callAuthentication = function (req, res, next) {

let connectSchema = {
clientObj: joi.object().keys({
name: joi.string().min(3).max(38),
email: joi.string().min(3).max(38),
language: joi.string().min(3).max(38),
username: joi.string().min(3).max(38).required(),
mobile_no: joi.string().min(3).max(38),
time_zone: joi.string().min(3).max(38),
system_phone: joi.string().optional().allow('').min(3).max(38),
phone_no_info: joi.any().optional().allow(''),
voicemail_pin: joi.string().min(3).max(38),
display_picture: joi.string().min(3).max(38),
external_extension: joi.string().min(3).max(38)

}),
agentObj: joi.object().keys({
userId: joi.number(),
username: joi.string().min(3).max(38).required(),
name: joi.string().min(3).max(38),
email: joi.string().min(3).max(38),
status: joi.string().min(3).max(38),
role: joi.string().min(3).max(38)
})
};

const data = req.body;

joi.validate(data, connectSchema, (err) => {
if (err) {
// send a 422 error response if validation fails
res.status(422).json({
status: 'error',
message: err.details[0].message
});
} else {
req.body = data;
next();
}
});
}









share|improve this question

























  • What error message does Joi return?

    – Ankh
    Nov 28 '18 at 9:26











  • Error received is 422 Unprocessable Entity.

    – Aamer Rasheed
    Nov 28 '18 at 12:59














0












0








0


1






I need help regarding how to validate certain fields of a nested json object using JOI validation. In my example I have an object which contains two sub objects i.e. clientObj and agentObj. I'm only interested in validating the username field which is required but I don't want to validate the remaining fields. If I only mention that field, by deleting all other fields, in my schema and joi.validate() function I get 422 error. Code is given below:



exports.callAuthentication = function (req, res, next) {

let connectSchema = {
clientObj: joi.object().keys({
name: joi.string().min(3).max(38),
email: joi.string().min(3).max(38),
language: joi.string().min(3).max(38),
username: joi.string().min(3).max(38).required(),
mobile_no: joi.string().min(3).max(38),
time_zone: joi.string().min(3).max(38),
system_phone: joi.string().optional().allow('').min(3).max(38),
phone_no_info: joi.any().optional().allow(''),
voicemail_pin: joi.string().min(3).max(38),
display_picture: joi.string().min(3).max(38),
external_extension: joi.string().min(3).max(38)

}),
agentObj: joi.object().keys({
userId: joi.number(),
username: joi.string().min(3).max(38).required(),
name: joi.string().min(3).max(38),
email: joi.string().min(3).max(38),
status: joi.string().min(3).max(38),
role: joi.string().min(3).max(38)
})
};

const data = req.body;

joi.validate(data, connectSchema, (err) => {
if (err) {
// send a 422 error response if validation fails
res.status(422).json({
status: 'error',
message: err.details[0].message
});
} else {
req.body = data;
next();
}
});
}









share|improve this question
















I need help regarding how to validate certain fields of a nested json object using JOI validation. In my example I have an object which contains two sub objects i.e. clientObj and agentObj. I'm only interested in validating the username field which is required but I don't want to validate the remaining fields. If I only mention that field, by deleting all other fields, in my schema and joi.validate() function I get 422 error. Code is given below:



exports.callAuthentication = function (req, res, next) {

let connectSchema = {
clientObj: joi.object().keys({
name: joi.string().min(3).max(38),
email: joi.string().min(3).max(38),
language: joi.string().min(3).max(38),
username: joi.string().min(3).max(38).required(),
mobile_no: joi.string().min(3).max(38),
time_zone: joi.string().min(3).max(38),
system_phone: joi.string().optional().allow('').min(3).max(38),
phone_no_info: joi.any().optional().allow(''),
voicemail_pin: joi.string().min(3).max(38),
display_picture: joi.string().min(3).max(38),
external_extension: joi.string().min(3).max(38)

}),
agentObj: joi.object().keys({
userId: joi.number(),
username: joi.string().min(3).max(38).required(),
name: joi.string().min(3).max(38),
email: joi.string().min(3).max(38),
status: joi.string().min(3).max(38),
role: joi.string().min(3).max(38)
})
};

const data = req.body;

joi.validate(data, connectSchema, (err) => {
if (err) {
// send a 422 error response if validation fails
res.status(422).json({
status: 'error',
message: err.details[0].message
});
} else {
req.body = data;
next();
}
});
}






javascript node.js validation joi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 9:25









Ankh

2,66022225




2,66022225










asked Nov 28 '18 at 6:38









Aamer RasheedAamer Rasheed

3010




3010













  • What error message does Joi return?

    – Ankh
    Nov 28 '18 at 9:26











  • Error received is 422 Unprocessable Entity.

    – Aamer Rasheed
    Nov 28 '18 at 12:59



















  • What error message does Joi return?

    – Ankh
    Nov 28 '18 at 9:26











  • Error received is 422 Unprocessable Entity.

    – Aamer Rasheed
    Nov 28 '18 at 12:59

















What error message does Joi return?

– Ankh
Nov 28 '18 at 9:26





What error message does Joi return?

– Ankh
Nov 28 '18 at 9:26













Error received is 422 Unprocessable Entity.

– Aamer Rasheed
Nov 28 '18 at 12:59





Error received is 422 Unprocessable Entity.

– Aamer Rasheed
Nov 28 '18 at 12:59












1 Answer
1






active

oldest

votes


















1














You can allow unknown keys with { allowUnknown: true }






const data = {
clientObj: {
username: 'username',
otherProp: 'otherProp'
},
agentObj: {
otherProp2: 'otherProp2'
}
};

const schema = Joi.object().keys({
clientObj: Joi.object().keys({
username: Joi.string().required()
})
});

Joi.validate(data, schema, { allowUnknown: true }, (err) => {
console.log(`err with allowUnknown: ${err}`);
});

Joi.validate(data, schema, { allowUnknown: false }, (err) => {
console.log(`err without allowUnknown: ${err}`);
});

<script src="https://cdn.jsdelivr.net/npm/joi-browser@13.4.0/dist/joi-browser.min.js"></script>





doc






share|improve this answer
























  • Solution works. Thanks Gabriel for your valued input.

    – Aamer Rasheed
    Nov 28 '18 at 13:01











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53513536%2fhow-to-validate-certain-fields-of-a-nested-json-object-using-joi-validation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can allow unknown keys with { allowUnknown: true }






const data = {
clientObj: {
username: 'username',
otherProp: 'otherProp'
},
agentObj: {
otherProp2: 'otherProp2'
}
};

const schema = Joi.object().keys({
clientObj: Joi.object().keys({
username: Joi.string().required()
})
});

Joi.validate(data, schema, { allowUnknown: true }, (err) => {
console.log(`err with allowUnknown: ${err}`);
});

Joi.validate(data, schema, { allowUnknown: false }, (err) => {
console.log(`err without allowUnknown: ${err}`);
});

<script src="https://cdn.jsdelivr.net/npm/joi-browser@13.4.0/dist/joi-browser.min.js"></script>





doc






share|improve this answer
























  • Solution works. Thanks Gabriel for your valued input.

    – Aamer Rasheed
    Nov 28 '18 at 13:01
















1














You can allow unknown keys with { allowUnknown: true }






const data = {
clientObj: {
username: 'username',
otherProp: 'otherProp'
},
agentObj: {
otherProp2: 'otherProp2'
}
};

const schema = Joi.object().keys({
clientObj: Joi.object().keys({
username: Joi.string().required()
})
});

Joi.validate(data, schema, { allowUnknown: true }, (err) => {
console.log(`err with allowUnknown: ${err}`);
});

Joi.validate(data, schema, { allowUnknown: false }, (err) => {
console.log(`err without allowUnknown: ${err}`);
});

<script src="https://cdn.jsdelivr.net/npm/joi-browser@13.4.0/dist/joi-browser.min.js"></script>





doc






share|improve this answer
























  • Solution works. Thanks Gabriel for your valued input.

    – Aamer Rasheed
    Nov 28 '18 at 13:01














1












1








1







You can allow unknown keys with { allowUnknown: true }






const data = {
clientObj: {
username: 'username',
otherProp: 'otherProp'
},
agentObj: {
otherProp2: 'otherProp2'
}
};

const schema = Joi.object().keys({
clientObj: Joi.object().keys({
username: Joi.string().required()
})
});

Joi.validate(data, schema, { allowUnknown: true }, (err) => {
console.log(`err with allowUnknown: ${err}`);
});

Joi.validate(data, schema, { allowUnknown: false }, (err) => {
console.log(`err without allowUnknown: ${err}`);
});

<script src="https://cdn.jsdelivr.net/npm/joi-browser@13.4.0/dist/joi-browser.min.js"></script>





doc






share|improve this answer













You can allow unknown keys with { allowUnknown: true }






const data = {
clientObj: {
username: 'username',
otherProp: 'otherProp'
},
agentObj: {
otherProp2: 'otherProp2'
}
};

const schema = Joi.object().keys({
clientObj: Joi.object().keys({
username: Joi.string().required()
})
});

Joi.validate(data, schema, { allowUnknown: true }, (err) => {
console.log(`err with allowUnknown: ${err}`);
});

Joi.validate(data, schema, { allowUnknown: false }, (err) => {
console.log(`err without allowUnknown: ${err}`);
});

<script src="https://cdn.jsdelivr.net/npm/joi-browser@13.4.0/dist/joi-browser.min.js"></script>





doc






const data = {
clientObj: {
username: 'username',
otherProp: 'otherProp'
},
agentObj: {
otherProp2: 'otherProp2'
}
};

const schema = Joi.object().keys({
clientObj: Joi.object().keys({
username: Joi.string().required()
})
});

Joi.validate(data, schema, { allowUnknown: true }, (err) => {
console.log(`err with allowUnknown: ${err}`);
});

Joi.validate(data, schema, { allowUnknown: false }, (err) => {
console.log(`err without allowUnknown: ${err}`);
});

<script src="https://cdn.jsdelivr.net/npm/joi-browser@13.4.0/dist/joi-browser.min.js"></script>





const data = {
clientObj: {
username: 'username',
otherProp: 'otherProp'
},
agentObj: {
otherProp2: 'otherProp2'
}
};

const schema = Joi.object().keys({
clientObj: Joi.object().keys({
username: Joi.string().required()
})
});

Joi.validate(data, schema, { allowUnknown: true }, (err) => {
console.log(`err with allowUnknown: ${err}`);
});

Joi.validate(data, schema, { allowUnknown: false }, (err) => {
console.log(`err without allowUnknown: ${err}`);
});

<script src="https://cdn.jsdelivr.net/npm/joi-browser@13.4.0/dist/joi-browser.min.js"></script>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 10:54









Gabriel BleuGabriel Bleu

3,8661723




3,8661723













  • Solution works. Thanks Gabriel for your valued input.

    – Aamer Rasheed
    Nov 28 '18 at 13:01



















  • Solution works. Thanks Gabriel for your valued input.

    – Aamer Rasheed
    Nov 28 '18 at 13:01

















Solution works. Thanks Gabriel for your valued input.

– Aamer Rasheed
Nov 28 '18 at 13:01





Solution works. Thanks Gabriel for your valued input.

– Aamer Rasheed
Nov 28 '18 at 13:01




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53513536%2fhow-to-validate-certain-fields-of-a-nested-json-object-using-joi-validation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)