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

fix: concat of required() for array/string #459

Merged
merged 1 commit into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
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
18 changes: 5 additions & 13 deletions src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import isSchema from './util/isSchema';
import makePath from './util/makePath';
import printValue from './util/printValue';
import MixedSchema from './mixed';
import { mixed, array as locale } from './locale';
import { array as locale } from './locale';
import runValidations, { propagateErrors } from './util/runValidations';

let hasLength = value => !isAbsent(value) && value.length > 0;

export default ArraySchema;

function ArraySchema(type) {
Expand Down Expand Up @@ -110,6 +108,10 @@ inherits(ArraySchema, MixedSchema, {
});
},

_isFilled(value) {
return value.length > 0;
},

of(schema) {
var next = this.clone();

Expand All @@ -125,16 +127,6 @@ inherits(ArraySchema, MixedSchema, {
return next;
},

required(message = mixed.required) {
var next = MixedSchema.prototype.required.call(this, message);

return next.test({
message,
name: 'required',
test: hasLength,
});
},

min(min, message) {
message = message || locale.min;

Expand Down
15 changes: 12 additions & 3 deletions src/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import printValue from './util/printValue';
import Ref from './Reference';
import { getIn } from './util/reach';

let notEmpty = value => !isAbsent(value);

class RefSet {
constructor() {
this.list = new Set();
Expand Down Expand Up @@ -302,8 +300,19 @@ const proto = (SchemaType.prototype = {
return next;
},

_isFilled(_) {
return true;
},

required(message = locale.required) {
return this.test({ message, name: 'required', test: notEmpty });
return this.test({
message,
name: 'required',
exclusive: true,
test(value) {
return !isAbsent(value) && this.schema._isFilled(value);
},
});
},

notRequired() {
Expand Down
9 changes: 3 additions & 6 deletions src/string.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import inherits from './util/inherits';
import MixedSchema from './mixed';
import { mixed, string as locale } from './locale';
import { string as locale } from './locale';
import isAbsent from './util/isAbsent';

// eslint-disable-next-line
let rEmail = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([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])))$/i;
// eslint-disable-next-line
let rUrl = /^((https?|ftp):)?\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF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?$/i;

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

export default function StringSchema() {
Expand All @@ -31,10 +30,8 @@ inherits(StringSchema, MixedSchema, {
return typeof value === 'string';
},

required(message = mixed.required) {
var next = MixedSchema.prototype.required.call(this, message);

return next.test({ message, name: 'required', test: hasLength });
_isFilled(value) {
return value.length > 0;
},

length(length, message = locale.length) {
Expand Down
2 changes: 1 addition & 1 deletion test/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ describe('Mixed Types ', () => {
});

it('should have the correct number of tests', () => {
reach(next, 'str').tests.length.should.equal(3); // presence, alt presence, and trim
reach(next, 'str').tests.length.should.equal(2);
});

it('should have the tests in the correct order', () => {
Expand Down