Skip to content

Commit

Permalink
feat: add _isFilled as overrideable mixed method to control require…
Browse files Browse the repository at this point in the history
…d behavior (#459)

BREAKING CHANGE: required no longer shows up twice in describe() output for array and strings, which also no longer override required
  • Loading branch information
vonagam authored and jquense committed Mar 14, 2019
1 parent 27d4bc2 commit 5b01f18
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
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 @@ -305,8 +303,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\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 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 @@ -594,7 +594,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

0 comments on commit 5b01f18

Please sign in to comment.