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

feat: add function test names to email and url #292

Merged
merged 3 commits into from
May 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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 @@ -76,14 +76,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 @@ -95,13 +101,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