Skip to content

Commit

Permalink
fix: present checks for array and strings
Browse files Browse the repository at this point in the history
fixes #913
  • Loading branch information
jquense committed May 27, 2020
1 parent 82035eb commit ecd8ebe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
12 changes: 7 additions & 5 deletions src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function ArraySchema(type) {
this.innerType = undefined;

this.withMutation(() => {
this.transform(function(values) {
this.transform(function (values) {
if (typeof values === 'string')
try {
values = JSON.parse(values);
Expand Down Expand Up @@ -76,7 +76,7 @@ inherits(ArraySchema, MixedSchema, {
return MixedSchema.prototype._validate
.call(this, _value, options)
.catch(propagateErrors(endEarly, errors))
.then(value => {
.then((value) => {
if (!recursive || !innerType || !this._typeCheck(value)) {
if (errors.length) throw errors[0];
return value;
Expand Down Expand Up @@ -114,7 +114,9 @@ inherits(ArraySchema, MixedSchema, {
},

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

of(schema) {
Expand Down Expand Up @@ -169,9 +171,9 @@ inherits(ArraySchema, MixedSchema, {
},

compact(rejector) {
let reject = !rejector ? v => !!v : (v, i, a) => !rejector(v, i, a);
let reject = !rejector ? (v) => !!v : (v, i, a) => !rejector(v, i, a);

return this.transform(values =>
return this.transform((values) =>
values != null ? values.filter(reject) : values,
);
},
Expand Down
22 changes: 12 additions & 10 deletions src/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ 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;

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

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

MixedSchema.call(this, { type: 'string' });

this.withMutation(() => {
this.transform(function(value) {
this.transform(function (value) {
if (this.isType(value)) return value;
return value != null && value.toString ? value.toString() : value;
});
Expand All @@ -31,7 +31,9 @@ inherits(StringSchema, MixedSchema, {
},

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

length(length, message = locale.length) {
Expand Down Expand Up @@ -87,7 +89,7 @@ inherits(StringSchema, MixedSchema, {
name: name || 'matches',
message: message || locale.matches,
params: { regex },
test: value =>
test: (value) =>
isAbsent(value) ||
(value === '' && excludeEmptyString) ||
value.search(regex) !== -1,
Expand All @@ -112,36 +114,36 @@ inherits(StringSchema, MixedSchema, {

//-- transforms --
ensure() {
return this.default('').transform(val => (val === null ? '' : val));
return this.default('').transform((val) => (val === null ? '' : val));
},

trim(message = locale.trim) {
return this.transform(val => (val != null ? val.trim() : val)).test({
return this.transform((val) => (val != null ? val.trim() : val)).test({
message,
name: 'trim',
test: isTrimmed,
});
},

lowercase(message = locale.lowercase) {
return this.transform(value =>
return this.transform((value) =>
!isAbsent(value) ? value.toLowerCase() : value,
).test({
message,
name: 'string_case',
exclusive: true,
test: value => isAbsent(value) || value === value.toLowerCase(),
test: (value) => isAbsent(value) || value === value.toLowerCase(),
});
},

uppercase(message = locale.uppercase) {
return this.transform(value =>
return this.transform((value) =>
!isAbsent(value) ? value.toUpperCase() : value,
).test({
message,
name: 'string_case',
exclusive: true,
test: value => isAbsent(value) || value === value.toUpperCase(),
test: (value) => isAbsent(value) || value === value.toUpperCase(),
});
},
});

1 comment on commit ecd8ebe

@PierreJeanjacquot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thanks

Please sign in to comment.