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

Discussion for a new feature - Rule name at toJSON #99

Open
ivansansao opened this issue Feb 24, 2025 · 0 comments
Open

Discussion for a new feature - Rule name at toJSON #99

ivansansao opened this issue Feb 24, 2025 · 0 comments

Comments

@ivansansao
Copy link

ivansansao commented Feb 24, 2025

Hi team, how are you? 😊

I'm trying to export the schema to the frontend (React), where I dynamically convert it to Yup. However, I can't find a way to retrieve the rule names for each field (e.g., "string", "trim", "minLength", "maxLength", "email", "optional").

I've tried everything and still can't get the rule names. Instead, toJSON() only provides ref://X identifiers, which makes it difficult to dynamically map the rules.

Here’s an example of my schema:

const createPostValidator = vine.compile(
  vine.object({
    title: vine.string().trim().minLength(6).maxLength(20),
    email: vine.string().email().optional(),
  })
)

console.log(createPostValidator.toJSON())

Current Output:

{
  schema: {
    type: 'root',
    schema: {
      type: 'object',
      properties: [Array],
    }
  },
  refs: {
    'ref://1': { validator: [Function (anonymous)], options: undefined },
    'ref://2': { validator: [Function (anonymous)], options: undefined },
    'ref://3': { validator: [Function (anonymous)], options: { min: 6 } },
    'ref://4': { validator: [Function (anonymous)], options: { max: 20 } },
    'ref://5': { validator: [Function (anonymous)], options: undefined },
    'ref://6': { validator: [Function (anonymous)], options: undefined }
  }
}

❌ Problem

  • The toJSON() output only includes ref://X identifiers instead of rule names.
  • The validator functions inside refs are anonymous, so I can't extract their names.
  • This makes it impossible to dynamically convert VineJS validation to Yup.

💡 Feature Request / Possible Solutions

Would it be possible to:

  1. Include the rule names directly in toJSON(), alongside the ref://X identifiers?
  2. Provide a way to retrieve the rule names programmatically, even if it’s through an internal API?

🔍 Expected Behavior

I would expect toJSON() to provide human-readable rule names like:

refs: {
  'ref://1': { validator: [Function (anonymous)], ruleName: "string", options: undefined },
  'ref://2': { validator: [Function (anonymous)], ruleName: "trim", options: undefined },
  'ref://3': { validator: [Function (anonymous)], ruleName: "minLength", options: { min: 6 } },
  'ref://4': { validator: [Function (anonymous)], ruleName: "maxLength", options: { max: 20 } },
  'ref://5': { validator: [Function (anonymous)], ruleName: "email", options: undefined },
  'ref://6': { validator: [Function (anonymous)], ruleName: "optional", options: undefined }
}

⚠️ Workaround (Don't use this! You may lose it in the next versions of VineJS)

/**
* Looks for the rule name within the function body by checking
* what is in the second parameter of the `report("message", "ruleName", field)` function.
*/
export function getRuleName(ref: string, refs: any): string {
    const fnBody = refs[ref].validator.toString()
    const match = fnBody.match(/report\s*\([^,]+,\s*([^,]+)/)
    if (match) {
        return match[1].trim().replace(/["']/g, '')
    } else {
        if (fnBody.includes('value.trim')) {
            return 'trim'
        }
    }
    return ''
}

Package: "@vinejs/vine": "^3.0.0"

Long life for VineJS!
Thanks in advance!

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

1 participant