Skip to content

Commit

Permalink
Closes #1867
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jul 24, 2019
1 parent 59195cd commit 5386138
Show file tree
Hide file tree
Showing 22 changed files with 243 additions and 248 deletions.
8 changes: 8 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
- [`number.port()`](#numberport)
- [`number.positive()`](#numberpositive)
- [`number.precision(limit)`](#numberprecisionlimit)
- [`number.sign(sign)`](#numbersignsign)
- [`number.unsafe([enabled])`](#numberunsafeenabled)
- [`object` - inherits from `Any`](#object---inherits-from-any)
- [`object.and(...peers, [options])`](#objectandpeers-options)
Expand Down Expand Up @@ -2182,6 +2183,13 @@ const schema = Joi.number().precision(2);

Possible validation errors: [`number.integer`](#numberinteger-1)

#### `number.sign(sign)`

Requires the number to be negative or positive where:
`sign` - one of `'negative'` or `'positive'`.

Possible validation errors: [`number.negative`](#numbernegative-1), [`number.positive`](#numberpositive-1)

#### `number.unsafe([enabled])`

By default, numbers must be within JavaScript's safety range (`Number.MIN_SAFE_INTEGER` & `Number.MAX_SAFE_INTEGER`), and when given a string, should be converted without loss of information. You can allow unsafe numbers at your own risks by calling `number.unsafe()`.
Expand Down
4 changes: 4 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ exports.extend = function (Class, prop, methods) {

const args = method.args;
method = method.method;
if (!method) {
method = () => null; // Set placeholder method for dummy rules
}

method.args = args;
}

Expand Down
45 changes: 25 additions & 20 deletions lib/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,7 @@ const Template = require('./template');
let Schemas;


const internals = {
flagDefaults: {
cast: false,
func: false,
insensitive: false,
once: true,
only: false,
sparse: false,
strip: false,
timestamp: false,
truncate: false,
unknown: false,
unsafe: false
}
};
const internals = {};


exports.describe = function (schema) {
Expand All @@ -41,7 +27,7 @@ exports.describe = function (schema) {

for (const flag in schema._flags) {
if (flag[0] !== '_' &&
schema._flags[flag] !== internals.flagDefaults[flag]) {
schema._flags[flag] !== internals.flagDefaults(desc.type, flag)) {

desc.flags[flag] = internals.describe(schema._flags[flag]);
}
Expand Down Expand Up @@ -151,13 +137,14 @@ exports.describe = function (schema) {
continue;
}

if (!items.length) {
if (!items.length &&
inner !== 'keys') {
continue;
}

let normalized = [];
for (const item of items) {
normalized.push(internals.describe(item, { inner: true }));
normalized.push(internals.describe(item));
}

if (inner === 'keys') {
Expand Down Expand Up @@ -193,6 +180,10 @@ internals.describe = function (item, options = {}) {
return Hoek.clone(item);
}

if (item instanceof Date) {
return item.toISOString();
}

if (item instanceof RegExp) {
if (options.assign === 'regex') {
return item.toString();
Expand Down Expand Up @@ -222,10 +213,10 @@ internals.describe = function (item, options = {}) {
continue;
}

normalized[key] = internals.describe(value, { inner: options.inner, assign: key });
normalized[key] = internals.describe(value, { assign: key });
}

return options.inner ? normalized : { value: normalized };
return normalized;
};


Expand Down Expand Up @@ -485,3 +476,17 @@ internals.validate = function (joi, desc) {

joi.assert(desc, Schemas.description);
};


internals.flagDefaults = function (type, flag) {

if (type === 'boolean' &&
flag === 'insensitive') {

return true;
}

if (['insensitive', 'only', 'single', 'sparse', 'strip', 'truncate', 'unknown', 'unsafe'].includes(flag)) {
return false;
}
};
5 changes: 5 additions & 0 deletions lib/modify.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ exports.Ids = internals.Ids = class {

concat(source) {

if (source._type !== 'any') {
this._type = source._type;
this._unsupported = source._unsupported;
}

for (const key of source._map.keys()) {
this._map.set(key, source._map.get(key));
}
Expand Down
1 change: 0 additions & 1 deletion lib/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ exports.description = Joi.object({
id: Joi.string(),
insensitive: Joi.boolean(),
label: Joi.string(),
once: Joi.boolean(),
only: true,
presence: Joi.string().valid('optional', 'required', 'forbidden'),
single: Joi.boolean(),
Expand Down
12 changes: 4 additions & 8 deletions lib/types/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ internals.Alternatives = class extends Any {

const obj = this.clone();
for (const schema of schemas) {
obj._inners.matches.push({ schema: Cast.schema(obj._root, schema) });
obj._inners.matches.push({ schema: obj._cast(schema) });
}

return obj._rebuild();
Expand Down Expand Up @@ -212,12 +212,12 @@ internals.Alternatives = class extends Any {
const item = {
ref: !isSchema ? Cast.ref(match) : null,
peek: isSchema ? match : null,
then: settings.then !== undefined ? Cast.schema(obj._root, settings.then) : undefined,
otherwise: settings.otherwise !== undefined ? Cast.schema(obj._root, settings.otherwise) : undefined
then: settings.then !== undefined ? obj._cast(settings.then) : undefined,
otherwise: settings.otherwise !== undefined ? obj._cast(settings.otherwise) : undefined
};

if (settings.is !== undefined) {
item.is = Cast.schema(obj._root, settings.is);
item.is = obj._cast(settings.is);
if (settings.is === null ||
!(Ref.isRef(settings.is) || Common.isSchema(settings.is))) {

Expand Down Expand Up @@ -264,10 +264,6 @@ internals.Alternatives = class extends Any {
}
}

if (options.otherwise) {
obj._inners.matches.push(normalize(condition, { is: new Any(), otherwise: options.otherwise }));
}

return obj._rebuild();
}

Expand Down
28 changes: 23 additions & 5 deletions lib/types/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ module.exports = internals.Any = class {
const obj = this.clone();

if (schema !== undefined) {
schema = Cast.schema(obj._root, schema);
schema = obj._cast(schema);
obj._refs.register(schema);
}

Expand Down Expand Up @@ -341,11 +341,11 @@ module.exports = internals.Any = class {

const item = {
is: settings.is,
then: settings.then && this.concat(Cast.schema(this._root, settings.then))
then: settings.then && this.concat(this._cast(settings.then))
};

if (settings.otherwise) {
item.otherwise = this.concat(Cast.schema(this._root, settings.otherwise));
item.otherwise = this.concat(this._cast(settings.otherwise));
}

return item;
Expand Down Expand Up @@ -489,6 +489,10 @@ module.exports = internals.Any = class {
for (const key in source._inners) {
const inners = source._inners[key];
if (!inners) {
if (!obj._inners[key]) {
obj._inners[key] = inners;
}

continue;
}

Expand Down Expand Up @@ -526,6 +530,10 @@ module.exports = internals.Any = class {
}
}

if (typeof obj._rebuild === 'function') {
obj._rebuild();
}

return obj;
}

Expand Down Expand Up @@ -644,6 +652,11 @@ module.exports = internals.Any = class {

// Internals

_cast(schema, options) {

return Cast.schema(this._root, schema, options);
}

_default(flag, value, options = {}) {

Common.assertOptions(options, 'literal');
Expand Down Expand Up @@ -757,7 +770,7 @@ module.exports = internals.Any = class {
rule: name,
alias: name,
resolve: [],
...options, // args, refs, multi, convert, ...rule-specific
...options, // args, refs, multi, convert, priority, ...rule-specific
_options: options // The original options
};

Expand Down Expand Up @@ -827,7 +840,12 @@ module.exports = internals.Any = class {
test.warn = true;
}

obj._tests.push(test);
if (options.priority) {
obj._tests.unshift(test);
}
else {
obj._tests.push(test);
}

return obj;
}
Expand Down
Loading

0 comments on commit 5386138

Please sign in to comment.