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

Feat: add array index in path #309

Open
intech opened this issue Sep 2, 2022 · 4 comments
Open

Feat: add array index in path #309

intech opened this issue Sep 2, 2022 · 4 comments

Comments

@intech
Copy link
Contributor

intech commented Sep 2, 2022

When validating an array of objects, when we use custom in nested rules, we cannot get the array element we are validating.

const itemPath = path + "[]";
const rule = this.getRuleFromSchema(schema.items);
// eslint-disable-next-line quotes
const innerSource = `arr[i] = ${context.async ? "await " : ""}context.fn[%%INDEX%%](arr[i], (parentField ? parentField : "") + "[" + i + "]", parent, errors, context)`;
src.push(this.compileRule(rule, context, itemPath, innerSource, "arr[i]"));

An example of such a scheme:

  const validator = {
    some: {
      type: 'array',
      optional: true,
      items: {
        type: 'object',
        props: {
          start: {
            type: 'string',
            max: 8,
            nullable: true,
            custom: (
              value: string | null,
              _schema: unknown,
              _path: unknown,
              _parent: unknown,
              _context: unknown
            ) => check(value),
          },
          end: {
            type: 'string',
            max: 8,
            nullable: true,
            custom: (
              value: string | null,
              _schema: unknown,
              _path: unknown,
              _parent: unknown,
              _context: unknown
            ) => check(value),
          },
        },
      },
    },
  }

The solution might be to change

const itemPath = path + "[]";`

on the

const itemPath = `${path}[${i}]`;
@fernandodevelon
Copy link

not sure if this would be a correct solution.
@intech have you tested it ?
does it work ?
i also have the problem that in custom arrays there is no index reference, and this is a problem for particular validations.

@intech
Copy link
Contributor Author

intech commented Jul 12, 2023

@fernandodevelon

I didn't check, but I found out by tracing the code. I don’t remember how I decided in my case, but I did something differently. And the current problem is actually. I just forgot about it.

@fernandodevelon
Copy link

fernandodevelon commented Jul 12, 2023

I did a test with the full example,
activating debugging
and tracing the custom call shows that itemPath does not have the references

itemPath = 'multiarray[][]'

this is the compiled code, where it calls the custom, no reference is passed to index i

image

@fernandodevelon
Copy link

I’ve been thinking about this issue, and after looking at the code, it’s not easily implementable unless the for loop saves the dynamic path in context.meta, for example.

However, it’s also true that each element of the array must be validated individually, otherwise the validation must be implemented at the highest level.

Therefore, I think we can close this issue.

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

2 participants