Skip to content

Commit

Permalink
Full types manifest support. For #1867
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jul 23, 2019
1 parent 4ebeffd commit 45861ac
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 32 deletions.
48 changes: 16 additions & 32 deletions lib/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ let Schemas;


const internals = {
nonInners: ['allow', 'flags', 'invalid', 'keys', 'preferences', 'rules', 'type'],
flagDefaults: {
cast: false,
func: false,
Expand Down Expand Up @@ -337,7 +336,22 @@ internals.Builder = class {

const inners = {};
for (const key in desc) {
if (internals.nonInners.includes(key)) {
if (['allow', 'flags', 'invalid', 'keys', 'preferences', 'rules', 'type'].includes(key)) {
continue;
}

if (['items', 'ordered'].includes(key)) {
inners[key] = desc[key].map((item) => this.parse(item));
continue;
}

if (['falsy', 'truthy'].includes(key)) {
inners[key] = desc[key].map((item) => this.build(item));
continue;
}

if (key === 'link') {
inners.link = this.build(desc.link);
continue;
}

Expand Down Expand Up @@ -373,36 +387,6 @@ internals.Builder = class {
schema = schema._build(inners);
}

/*
// Inners
for (const inner in schema._inners) {
if (items instanceof Map) {
if (items.size) {
description[inner] = [...items.entries()];
}
continue;
}
if (Common.isValues(items)) {
if (items.length) {
description[inner] = items.describe();
}
continue;
}
let normalized = [];
for (const item of items) {
normalized.push(internals.describe(item));
}
description[inner] = inner === 'link' ? normalized[0] : normalized;
}
*/

return schema;
}

Expand Down
15 changes: 15 additions & 0 deletions lib/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ internals.Array = class extends Any {
}
}

_build(desc) {

let obj = this; // eslint-disable-line consistent-this

if (desc.items) {
obj = obj.items(...desc.items);
}

if (desc.ordered) {
obj = obj.ordered(...desc.ordered);
}

return obj;
}

// Rules

has(schema) {
Expand Down
15 changes: 15 additions & 0 deletions lib/types/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ internals.Boolean = class extends Any {
}
}

_build(desc) {

let obj = this; // eslint-disable-line consistent-this

if (desc.truthy) {
obj = obj.truthy(...desc.truthy);
}

if (desc.falsy) {
obj = obj.falsy(...desc.falsy);
}

return obj;
}

// Rules

truthy(...values) {
Expand Down
5 changes: 5 additions & 0 deletions lib/types/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ internals.Link = class extends Any {
return schema._validate(value, state, prefs);
}

_build(desc) {

return this.ref(desc.link);
}

// Rules

ref(ref) {
Expand Down
7 changes: 7 additions & 0 deletions lib/types/symbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ internals.Symbol = class extends Any {
}
}

_build(desc) {

let obj = this; // eslint-disable-line consistent-this
obj = obj.map(desc.map);
return obj;
}

// Rules

map(iterable) {
Expand Down
41 changes: 41 additions & 0 deletions test/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,32 @@ describe('Manifest', () => {
]);
});

it('builds arrays', () => {

internals.test([
Joi.array().min(1).items(Joi.number()),
Joi.array().has(Joi.string()),
Joi.array().ordered(Joi.number(), Joi.boolean(), Joi.binary())
]);
});

it('builds booleans', () => {

internals.test([
Joi.boolean().truthy('x'),
Joi.boolean().falsy(Joi.ref('$x')),
Joi.boolean().truthy(3).falsy(4)
]);
});

it('builds links', () => {

internals.test([
Joi.link('....'),
Joi.link(Joi.ref('xxx....', { separator: 'x' }))
]);
});

it('builds objects', () => {

internals.test([
Expand All @@ -374,6 +400,21 @@ describe('Manifest', () => {
Joi.string().replace(/x/, 'X')
]);
});

it('builds strings', () => {

internals.test([
Joi.string().min(1).max(10).pattern(/\d*/),
Joi.string().replace(/x/, 'X')
]);
});

it('builds symbols', () => {

internals.test([
Joi.symbol().map([['a', Symbol('a')]])
]);
});
});
});

Expand Down

0 comments on commit 45861ac

Please sign in to comment.