joi icon indicating copy to clipboard operation
joi copied to clipboard

custom function not called when schema has valid()

Open stockson opened this issue 2 years ago • 5 comments

Context

  • node version: 14.17.0
  • module version with issue: 17.6.0
  • last module version without issue: N/A
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): node, and in testing with mocha
  • any other relevant information:

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

I've been using custom methods to reformat (stringify) objects after validating them. Works great except when using .custom on a schema with valid() on it. When using them together, the custom function isn't called.

	it("should work with valid and custom", () => {
		let customCalled = false
		const schema = Joi.string().valid("baz")
			.custom( (value, helper) => {
				customCalled = true
				return `foo-${value}`
			})
		const { value, warning, error } = schema.validate( "baz" )
		customCalled.should.equal(true) // actual: false
		expect(error).to.be.undefined
		value.should.equal("foo-baz") // actual: baz
	})

What was the result you got?

No custom method called, no value transformation.

What result did you expect?

I was hoping the custom method would be called after/if validation rules passed

stockson avatar May 18 '22 02:05 stockson

Just realized this applies to any added rules ex min max, and having those override custom might be the intended behavior?

stockson avatar May 18 '22 02:05 stockson

I just took a look in the code and in the docs. The docs example doesn't refer to custom as exclusive, but it isn't used with other validations.

This is because when you call validate, it checks for the validation and return right away. It checks the rules only after everything. I tried applying the custom with the rules, but it breaks too many tests.

https://github.com/sideway/joi/blob/7aa36666863c1dde7e4eb02a8058e00555a99d54/lib/validator.js#L288-L354

Perhaps this is intentional, though I'm not sure. A workaround is format the field without using Joi.

ramos-ph avatar Aug 10 '22 19:08 ramos-ph

Valid (and invalid) is indeed a whitelist, anything you set there goes through without evaluating other rules.

Marsup avatar Aug 11 '22 10:08 Marsup

I needed this, but to my surprise the .custom callback does not get called if i have a .valid inside the schema.

izzat-sq avatar Oct 26 '23 02:10 izzat-sq

Ok turns out, i can use .external as suggested in #2924 https://joi.dev/api/?v=17.9.1#anyexternalmethod-description. It works.

izzat-sq avatar Oct 26 '23 02:10 izzat-sq