shapeshift icon indicating copy to clipboard operation
shapeshift copied to clipboard

bug: UnionValidator#setValidationEnabled does not make its members respect that

Open Qjuh opened this issue 2 years ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Description of the bug

Found when disabling validation in @discordjs/builders on EmbedBuilder#setDescription() it would still throw a CombinedError with ExpectedValidationError in it.

Steps To Reproduce

import {s} from "@sapphire/shapeshift";

s.string
.lengthGreaterThanOrEqual(10)
.nullish
.setValidationEnabled(false).parse("test");

Expected behavior

It should not throw any Error.

Screenshots

No response

Additional context

No response

Qjuh avatar Aug 18 '23 09:08 Qjuh

Thing is, although the API is called setValidationEnabled, the getter method (getValidationEnabled()) is only called in tests, not a single time in the source code, and instead the library uses the getter below it (get shouldRunConstraints()):

https://github.com/sapphiredev/shapeshift/blob/afb87bf81f288603eeae8d30a0d29fa32705d34a/src/validators/BaseValidator.ts#L116-L122

During validation, this.handle() is always called, even when get shouldRunConstraints() returns false:

https://github.com/sapphiredev/shapeshift/blob/afb87bf81f288603eeae8d30a0d29fa32705d34a/src/validators/BaseValidator.ts#L91-L99

In fact, if this.handle() wasn't called at all if it returned false, the effects would not only apply to the current validator, but also the children, since their parse() method wouldn't be called either.

We should change the name or the behaviour IMO. cc: @vladfrangu @favna

kyranet avatar Aug 19 '23 21:08 kyranet

I'd say we should change the behaviour.

favna avatar Aug 19 '23 21:08 favna