Skip to content

Commit

Permalink
feat: more strictly coerce strings, exclude arrays and plain objects
Browse files Browse the repository at this point in the history
BREAKING CHANGE: plain objects and arrays are no long cast to strings automatically

to recreate the old behavior:
```js
string().transform((_, input) => input != null && input.toString ? input.toString() : value);
```
  • Loading branch information
jquense committed Nov 19, 2020
1 parent 31bbfc3 commit 963d2e8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
17 changes: 12 additions & 5 deletions src/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ let rEmail = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\u
// eslint-disable-next-line
let rUrl = /^((https?|ftp):)?\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
// eslint-disable-next-line
let rUUID = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i
let rUUID = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;

let isTrimmed = (value) => isAbsent(value) || value === value.trim();

let objStringTag = {}.toString();

export default function StringSchema() {
if (!(this instanceof StringSchema)) return new StringSchema();

Expand All @@ -20,7 +22,14 @@ export default function StringSchema() {
this.withMutation(() => {
this.transform(function (value) {
if (this.isType(value)) return value;
return value != null && value.toString ? value.toString() : value;
if (Array.isArray(value)) return value;

const strValue =
value != null && value.toString ? value.toString() : value;

if (strValue === objStringTag) return value;

return strValue;
});
});
}
Expand All @@ -33,9 +42,7 @@ inherits(StringSchema, MixedSchema, {
},

_isPresent(value) {
return (
MixedSchema.prototype._isPresent.call(this, value) && value.length > 0
);
return MixedSchema.prototype._isPresent.call(this, value) && !!value.length;
},

length(length, message = locale.length) {
Expand Down
10 changes: 8 additions & 2 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ describe('String types', () => {
valid: [
[5, '5'],
['3', '3'],
//[new String('foo'), 'foo'],
// [new String('foo'), 'foo'],
['', ''],
[true, 'true'],
[false, 'false'],
[0, '0'],
[null, null, schema.nullable()],
[
{
toString: () => 'hey',
},
'hey',
],
],
invalid: [null],
invalid: [null, {}, []],
});

describe('ensure', () => {
Expand Down

0 comments on commit 963d2e8

Please sign in to comment.