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

trim string option causes optional fields to be returned with undefined values #100

Closed
nicktalb opened this issue Sep 25, 2023 · 2 comments

Comments

@nicktalb
Copy link

nicktalb commented Sep 25, 2023

See the following test cases:

(1) Without trim option - optional fields are correctly not returned as they are not defined in the input.

// Returns { ok: true, result: {} } ✅
st.use(st.partial(st.record({ name: st.string({ trim: false }), other: st.string() })), {})

(2) With trim option - all optional fields incorrectly returned with undefined values.

// Returns { ok: true, result: { name: undefined, other: undefined } } ❌
st.use(st.partial(st.record({ name: st.string({ trim: true }), other: st.string() })), {})

Many libraries will treat the two objects differently which can cause downstream issues.

@hoeck
Copy link
Owner

hoeck commented Sep 26, 2023

Thanks for reporting this 👍 that indeed is a problem and not the intended behavior.

@hoeck-cs
Copy link

Damn, trim and minLength / maxLength are also broken in combination.

Trimming should be done first and min/max length checking should be done on the trimmed string, not on the untrimmed string:

const s = st.string({trim: true, minLength: 1, maxLength: 3})

s(' ') === '' // true, should raise an error instead because '' is not long enough
s('abc ') // raises an error but should not because 'abc' is still within maxLength

Gotta fix that too 😅

@hoeck hoeck closed this as completed in 1744f62 Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants