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: defaults in ordered array are not filled #2548

Merged
merged 10 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
36 changes: 36 additions & 0 deletions lib/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,13 @@ module.exports = Any.extend({

if (ordereds.length) {
internals.fillOrderedErrors(schema, errors, ordereds, value, state, prefs);

if (errors.length) {
hueniverse marked this conversation as resolved.
Show resolved Hide resolved
return errors;
}

internals.fillDefault(ordereds, value, state, prefs);
iifawzi marked this conversation as resolved.
Show resolved Hide resolved
return value;
}

return errors.length ? errors : value;
Expand Down Expand Up @@ -677,6 +684,35 @@ internals.fillOrderedErrors = function (schema, errors, ordereds, value, state,
};


internals.fillDefault = function (ordereds, value, state, prefs) {

const defaultValues = [];
let trailingUndefined = true;

for (let i = ordereds.length - 1; i >= 0; --i) {

const ancestors = [value, ...state.ancestors];
const ordered = ordereds[i];
const res = ordered.$_validate(undefined, state.localize(state.path, ancestors, ordered), prefs);
const orderedValue = res.value;

if (trailingUndefined) {
if (orderedValue === undefined) {
continue;
}

trailingUndefined = false;
}

defaultValues.unshift(orderedValue);
}

if (defaultValues.length) {
value.push(...defaultValues);
}
};


internals.fastSplice = function (arr, i) {

let pos = i;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
"@sideway/pinpoint": "^2.0.0"
},
"devDependencies": {
"typescript": "4.0.x",
"@hapi/bourne": "2.x.x",
"@hapi/code": "8.x.x",
"@hapi/joi-legacy-test": "npm:@hapi/[email protected]",
"@hapi/lab": "24.x.x",
"@hapi/joi-legacy-test": "npm:@hapi/[email protected].x"
"typescript": "4.0.x"
},
"scripts": {
"prepublishOnly": "cd browser && npm install && npm run build",
Expand Down
69 changes: 69 additions & 0 deletions test/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,75 @@ describe('array', () => {
]);
});

it('can fill the default values into the value array', () => {

const schema = Joi.array().ordered(Joi.string().required(), Joi.number().default(0), Joi.number().default(6)).required();

Helper.validate(schema, [
[['a'], true, ['a', 0, 6]]
]);
});

it('ignore trailing undefined', () => {

const schema = Joi.array().ordered(Joi.string().required(), Joi.string(), Joi.number().default(5), Joi.string(), Joi.string());

Helper.validate(schema, [
[['a'], true, ['a', undefined, 5]]
]);
});

it('generate sparse array', () => {

const schema = Joi.array().ordered(Joi.string(), Joi.number().default(5), Joi.string(), Joi.string().default('f'));

Helper.validate(schema, [
[[], true, [undefined, 5, undefined, 'f']]
]);
});

it('fills defaults correctly when nested items contain references', () => {

const schema = Joi.object({
info: Joi.object(),
values: Joi.array().ordered(
Joi.string().required(),
Joi.string().default(Joi.ref('...info.firstname')),
Joi.string(),
Joi.string().default(Joi.ref('...info.lastname')),
Joi.string()
).required()
});

Helper.validate(schema, [
[{ info: { firstname: 'f', lastname: 'e' }, values: ['h'] }, true, { info: { firstname: 'f', lastname: 'e' }, values: ['h', 'f', undefined, 'e'] }],
[{ info: { firstname: 'f', lastname: 'e' }, values: ['h', 'test'] }, true, { info: { firstname: 'f', lastname: 'e' }, values: ['h', 'test', undefined, 'e'] }]
]);
});

it('fills defaults correctly when nested items contain references', () => {

const schema = Joi.object({
info: Joi.object().required(),
values: Joi.array().ordered(
Joi.string().required(),
Joi.array().ordered(
Joi.string(),
Joi.string().default('normal value'),
Joi.string(),
Joi.number().default(Joi.x('{number("202099")}'))
),
Joi.string(),
Joi.string().default(Joi.ref('...info.firstname')),
Joi.string()
).required()
});

Helper.validate(schema, [
[{ info: { firstname: 'f' }, values: ['h', []] }, true, { info: { firstname: 'f' }, values: ['h', [undefined, 'normal value', undefined, 202099], undefined, 'f'] }]
]);
});

iifawzi marked this conversation as resolved.
Show resolved Hide resolved
it('can strip matching items', () => {

const schema = Joi.array().items(Joi.string(), Joi.any().strip());
Expand Down