Skip to content

Commit

Permalink
feat: expose oneOf and notOneOf values on description (#885)
Browse files Browse the repository at this point in the history
* [tests] Add test

* Expose blacklist and whitelist values

* Add tests

* whitelist -> oneOf, blacklist -> notOneOf
  • Loading branch information
brianle1301 authored May 11, 2020
1 parent b2af326 commit 08dad5f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
21 changes: 18 additions & 3 deletions src/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ class RefSet {
this.list = new Set();
this.refs = new Map();
}
get size() {
return this.list.size + this.refs.size;
}
describe() {
const description = [];

for (const item of this.list) description.push(item);
for (const [, ref] of this.refs) description.push(ref.describe());

return description;
}
toArray() {
return toArray(this.list).concat(toArray(this.refs.values()));
}
Expand Down Expand Up @@ -528,8 +539,7 @@ const proto = (SchemaType.prototype = {

describe() {
const next = this.clone();

return {
const description = {
type: next._type,
meta: next._meta,
label: next._label,
Expand All @@ -538,7 +548,12 @@ const proto = (SchemaType.prototype = {
.filter(
(n, idx, list) => list.findIndex(c => c.name === n.name) === idx,
),
};
}

if (next._whitelist.size) description.oneOf = next._whitelist.describe();
if (next._blacklist.size) description.notOneOf = next._blacklist.describe();

return description;
},

defined(message = locale.defined) {
Expand Down
9 changes: 8 additions & 1 deletion test/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,9 @@ describe('Mixed Types ', () => {
bar: string()
.max(2)
.meta({ input: 'foo' })
.label('str!'),
.label('str!')
.oneOf(['a', 'b'])
.notOneOf([ref('foo')]),
}).describe();

desc.should.eql({
Expand Down Expand Up @@ -926,6 +928,11 @@ describe('Mixed Types ', () => {
meta: {
input: 'foo',
},
oneOf: ['a', 'b'],
notOneOf: [{
type: 'ref',
key: 'foo'
}]
},
},
});
Expand Down

0 comments on commit 08dad5f

Please sign in to comment.