Skip to content

Extended object does not parse JSON #1521

@chuhaienko

Description

@chuhaienko

Context

  • node version: 10.2.1
  • joi version: 13.3.0
  • environment (node, browser): node
  • used with (hapi, standalone, ...): standalone
  • any other relevant information: no

What are you trying to achieve or the steps to reproduce ?

I have tried to add new rule for object but after it many validations become broken.
Example of code to reproduce is below.

const Joi = require('joi');

const schema = Joi.object({
	position: Joi.object({
		x: Joi.number(),
		y: Joi.number()
	}).required()
});

const data = {
	position: JSON.stringify({
		x: 100,
		y: 200
	})
};

console.log(Joi.validate(data, schema));
/*
There I got result as expected. There will be error if data.position.x = 'hello'
*/

/*
{ error: null,
  value: { position: { x: 100, y: 200 } },
  then: [Function: then],
  catch: [Function: catch] }
 */
const origJoi = require('joi');

const Joi = origJoi.extend((joi) => {
	return {
		name:  'object',
		base:  joi.object(),
		rules: [{
			name: 'newRule',
			validate (params, value, state, options) {
				return value;
			}
		}]
	};
});

const schema = Joi.object({
	position: Joi.object({
		x: Joi.number(),
		y: Joi.number()
	}).required()
});

const data = {
	position: JSON.stringify({
		x: 'hello',
		y: 200
	})
};

console.log(Joi.validate(data, schema));
/*
There is unexpected result. There is no error on data.position.x and data.position is not parsed
*/

/*
{ error: null,
  value: { position: '{"x":"hello","y":200}' },
  then: [Function: then],
  catch: [Function: catch] }
 */

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions