hapijs / hapijs/joi

custom function not called when schema has valid()

Open
#2,784 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

Description

#### 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.

```typescript
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.