Skip to content

Commit

Permalink
feat: add function test names to email and url (#292)
Browse files Browse the repository at this point in the history
* Add function test names to email and url

* Set default name to 'matches' when none is provided to the matches function

Co-Authored-By: Jason Quense <[email protected]>

* Add comma
  • Loading branch information
aytee17 authored and jquense committed May 16, 2019
1 parent 2540d7b commit 7e94395
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function 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 Down Expand Up @@ -73,14 +73,20 @@ inherits(StringSchema, MixedSchema, {
matches(regex, options) {
let excludeEmptyString = false;
let message;
let name;

if (options) {
if (options.message || options.hasOwnProperty('excludeEmptyString')) {
({ excludeEmptyString, message } = options);
if (
options.message ||
options.hasOwnProperty('excludeEmptyString') ||
options.name
) {
({ excludeEmptyString, message, name } = options);
} else message = options;
}

return this.test({
name: name || 'matches',
message: message || locale.matches,
params: { regex },
test: value =>
Expand All @@ -92,13 +98,15 @@ inherits(StringSchema, MixedSchema, {

email(message = locale.email) {
return this.matches(rEmail, {
name: 'email',
message,
excludeEmptyString: true,
});
},

url(message = locale.url) {
return this.matches(rUrl, {
name: 'url',
message,
excludeEmptyString: true,
});
Expand Down

0 comments on commit 7e94395

Please sign in to comment.