Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended object does not parse JSON #1521

Closed
chuhaienko opened this issue Jun 5, 2018 · 3 comments
Closed

Extended object does not parse JSON #1521

chuhaienko opened this issue Jun 5, 2018 · 3 comments
Assignees
Labels
bug Bug or defect
Milestone

Comments

@chuhaienko
Copy link

chuhaienko commented Jun 5, 2018

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] }
 */
@chuhaienko
Copy link
Author

I have dug some info.
In first case (not-extended object) schema creates through root.object()
In second case (extended object) - not

@Marsup Marsup added this to the 13.4.0 milestone Jun 6, 2018
@Marsup Marsup self-assigned this Jun 6, 2018
@Marsup Marsup added the bug Bug or defect label Jun 6, 2018
@Marsup
Copy link
Collaborator

Marsup commented Jun 6, 2018

Duplicate of #1000.

@Marsup Marsup closed this as completed in 8a1eb96 Jun 6, 2018
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

2 participants