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

Adding labels to values in issue paths #807

Open
jamiebuilds opened this issue Aug 29, 2024 · 2 comments
Open

Adding labels to values in issue paths #807

jamiebuilds opened this issue Aug 29, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request question Further information is requested

Comments

@jamiebuilds
Copy link

When validating really large datasets it would be nice if there were a way to describe the path for specific issues.

For example, if you have an issue with a path like this:

components:
  71:
    licenses: Invalid type: Expected Array but received undefined

The index 71 there is difficult to track down. But if I could write something like:

Note: This is just a rough draft of an idea, I could see this being a method or an action, see other api ideas below

// function label(label: (input: unknown) => string): LabelAction

let ComponentSchema = v.pipe(
  v.object({
    id: v.string(),
    licenses: v.array(LicenseSchema),
  }),
  v.label(input => {
    // Not sure if this should 
    if (typeof input === "object" && input != null && typeof input.id == "string") {
      return `Component(${label.id})`
    } else {
      return `Component`
    }
  })
)

The issue path could then contain a label which is describing the input at the current path index.

	{
	  kind: 'schema',
    message: 'Invalid type: Expected Array but received undefined',
	  ...
	  path: [
	    { type: 'object', origin: 'value', key: 'components', ... },
	    {
        type: 'array',
        origin: 'value',
        input: [Array],
        key: 71,
        value: [Object]
+       label: "Component(npm-package-id)"
      },
	    { type: 'object', origin: 'value', key: 'licenses', ... }
	  ],
	  ...
	}

That would allow you to print the issue with enough information to identify the invalid data more quickly:

components:
  71: Component(npm-package-id)
    licenses: Invalid type: Expected Array but received undefined

Alternative APIs

Similar to partialCheck:

// function label(pathList: PathKeys, label: (input: Selection) => string): LabelAction
let ComponentSchema = v.pipe(
  v.object({
    id: v.string(),
    licenses: v.array(LicenseSchema),
  }),
  v.label([["id"]], input => {
    return `Component(${label.id})`
  }),
)

As a method:

// function label(schema: Schema, label: (input: unknown) => string): SchemaWithLabel

let ComponentSchema = v.label(
  v.object({
    id: v.string(),
    licenses: v.array(LicenseSchema),
  }),
  input => {
    // Not sure if this should 
    if (typeof input === "object" && input != null && typeof input.id == "string") {
      return `Component(${label.id})`
    } else {
      return `Component`
    }
  })
)

Something more opinionated:

I'm not sure this is a good idea, but as an option maybe:

// function label(name: string, primaryKey: string[], schema: ObjectSchema): SchemaWithLabel

let ComponentSchema = v.label("Component", ["id"], v.object({
  id: v.string(),
  licenses: v.array(LicenseSchema),
}))
@fabian-hiller
Copy link
Owner

Thanks for creating this PR. In simple words, you want to be able to label path items to generate a stack-trace like output with a better overview through these labels?

@fabian-hiller fabian-hiller self-assigned this Aug 30, 2024
@fabian-hiller fabian-hiller added enhancement New feature or request question Further information is requested labels Aug 30, 2024
@sihu
Copy link

sihu commented Nov 22, 2024

I agree with the fact, that more context would make it much easier to debug an issue (especially if you have a lot of schemas and a field has a common name used in more than one schema). Then it would make it very helpful, if the ValiError would have some context. I would prefer not defining a label for each schema. I could imagine that it would be enough to just show the user the whole context of the schema, meaning in the given example:

licenses: Invalid type: Expected Array but received undefined

in

object({
  id: v.string(),
  licenses: v.array(LicenseSchema),
})

In theory it should be possible to track that from the stacktrace of the error where you can follow the code and see the schema used. But in practice you often have a compilation step and it's much harder to debug in a compiled chunk. That's also why I would appreciate the idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants