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

[1] "[0]" must not be a sparse array item #19

Open
albertpeiro opened this issue Sep 8, 2022 · 3 comments
Open

[1] "[0]" must not be a sparse array item #19

albertpeiro opened this issue Sep 8, 2022 · 3 comments
Assignees

Comments

@albertpeiro
Copy link

albertpeiro commented Sep 8, 2022

I installed:

"joi": "^17.6.0",
"joi-tz": "^4.1.1",

and coded:

import * as BaseJoi from "joi";
import JoiTimezone from "joi-tz";
const Joi = BaseJoi.extend(JoiTimezone);

const querySchema = Joi.object({
  rangeStart: Joi.date().required(),
  rangeEnd: Joi.date().required(),
  rangeTz: Joi.string().required(),
});

Didn't call .timezone() yet but it already seems to break BaseJoi. Is there anything I'm doing wrong?
I've tried downgrading to joi 16, 17.5, 17.4 but didn't help.

Thank you!

This is the error I get at run time.

/Users/project/node_modules/joi/lib/errors.js:193
        return new exports.ValidationError(message, details, original);
               ^
Error [ValidationError]: [
  null
]

[1] "[0]" must not be a sparse array item
    at Object.exports.process (/Users/project/node_modules/joi/lib/errors.js:193:16)
    at Object.internals.entry (/Users/project/node_modules/joi/lib/validator.js:153:26)
    at Object.exports.entry (/Users/project/node_modules/joi/lib/validator.js:27:30)
    at internals.Base.validate (/Users/project/node_modules/joi/lib/base.js:548:26)
    at Object.internals.assert (/Users/project/node_modules/joi/lib/index.js:225:27)
    at Object.assert (/Users/project/joi/lib/index.js:102:19)
    at Object.extend (/Users/project/joi/lib/index.js:156:14)
    at Object.<anonymous> (/Users/project/HomeOverview.ts:5:21)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Module.m._compile (/Users/project/node_modules/ts-node/src/index.ts:1618:23) {
  _original: [ undefined ],
  details: [
    {
      message: '"[0]" must not be a sparse array item',
      path: [Array],
      type: 'array.sparse',
      context: [Object]
    }
  ]
}
@tjdavey tjdavey self-assigned this Sep 12, 2022
@tjdavey
Copy link
Owner

tjdavey commented Sep 12, 2022

@albertpeiro I've been unable to reproduce this in Node 18 or Node 16 using the example you gave and some expansion. This is the code I tested with.

import BaseJoi from "joi";
import JoiTimezone from "joi-tz";
const Joi = BaseJoi.extend(JoiTimezone);

const querySchema = Joi.object({
    rangeStart: Joi.date().required(),
    rangeEnd: Joi.date().required(),
    rangeTz: Joi.string().required(),
});

const result = querySchema.validate({
    rangeStart: '2022-01-01',
    rangeEnd: '2022-01-03',
    rangeTz: 'Asia/Bangkok',
});

console.log(result);

The error you have provided indicates a validation error in a Joi schema containing an array type. There isn't one included in this code. Are you able to provide me with a little more detail in the environment you're seeing this issue in, and maybe a full example file?

@ckrusepmc
Copy link

ckrusepmc commented Nov 7, 2022

I'm getting a similar error on my end. I'm also not using array(). If I remove joi-tz, the error goes away. It might be worth noting that I'm using moment-timezone in my project already.

My schema:

export const LocationValidator = Joi.object({
  name: Joi.string().required(),
  // timezone: Joi.timezone(),
  address: Joi.object({
    line_1: Joi.string(),
    line_2: Joi.string(),
    city: Joi.string(),
    state: Joi.string(),
    postcode: Joi.string(),
  }),
});

Error:

{
	"errorType": "ValidationError",
	"errorMessage": "[\n  null\n]\n\u001b[31m\n[1] \"[0]\" must not be a sparse array item\u001b[0m",
	"_original": [
		null
	],
	"details": [{
		"message": "\"[0]\" must not be a sparse array item",
		"path": [
			0
		],
		"type": "array.sparse",
		"context": {
			"key": 0,
			"path": [
				0
			],
			"pos": 0,
			"label": "[0]"
		}
	}]
}

Using node v16.x

@cursomeancore
Copy link

Any solution for this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants