sinclairzx81 / typebox

Json Schema Type Builder with Static Type Resolution for TypeScript
Other
4.65k stars 150 forks source link

Add trim option to Type.String() #829

Closed Alyxsqrd closed 3 months ago

Alyxsqrd commented 3 months ago

Hello, I noticed that the maxLength and minLength options in StringOptions don't check against trimmed strings. This seems like a problem if validating against something like minLength: 10 where test would fail but test with empty spaces at the start/end would succeed. If trim: true, typebox should return the trimmed string as well.

sinclairzx81 commented 3 months ago

@Alyxsqrd Hi,

Thanks for the suggestion, unfortunately, any options that are unknown to the Json Schema specification are deemed out of scope (which would include the trim: true option). Although it might be possible to implement trim functionality using Transform types.

Just keep in mind, it's generally better to ensure senders of data transmit 'pre-trimmed' / 'correct' data rather than having the validator side fix incorrect data (where it's generally best practice to ensure the receiver is strict). This can help mitigate ambiguities determining the exact inputs a receiver can receive.

Will close off this issue for now. Cheers S