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

Cannot get schema name when using modifier #64

Closed
james-atlasopen opened this issue Jul 15, 2024 · 6 comments
Closed

Cannot get schema name when using modifier #64

james-atlasopen opened this issue Jul 15, 2024 · 6 comments
Assignees
Labels
Type: Question Needs clarification

Comments

@james-atlasopen
Copy link

james-atlasopen commented Jul 15, 2024

Package version

2.1.0

Describe the bug

When using a modifier on a schema, e.g., .optional(), you can no longer get the schema name.

Example:

const symbol = Symbol.for("schema_name");

const schema = vine.string();
const withOpt = vine.string().optional();

console.log(schema[symbol]); // "vine.string"
console.log(withOpt[symbol]); // undefined

Reproduction repo

No response

@thetutlage
Copy link
Contributor

Hey, can you share the purpose for which you are reading the schema_name?

@thetutlage thetutlage self-assigned this Jul 15, 2024
@james-atlasopen
Copy link
Author

james-atlasopen commented Jul 15, 2024

@thetutlage The purpose is to dynamically populate a form with fields depending on the type.
i.e., name is a string, so use a text input, age is a number, so use a number input.

The goal is that we can reuse the Vine schema for this information (essentially, the Vine schema is the "source of truth"), instead of creating another object with the mappings.

@thetutlage
Copy link
Contributor

The schema_name property is for internal usage via unionOfTypes schema type and using it for other purposes may not provide the desired results.

So, for your use-case, you can use the JSON representation of the schema.

const validator = vine.compile(
  vine.object({
     
  })
)

console.log(validator.toJSON())

@thetutlage thetutlage added the Type: Question Needs clarification label Jul 15, 2024
@james-atlasopen
Copy link
Author

james-atlasopen commented Jul 15, 2024

Ah, I see.

The toJSON() representation doesn't appear to give the type as "string", "number", "boolean", etc. Only "literal".
image

Is there a way to get this type information? Ultimately we just want to know the primitive type of a Vine schema object/property.

@thetutlage
Copy link
Contributor

Yeah. It will be nice to encode that info. Lemme do that

@james-atlasopen
Copy link
Author

May I make a request that it be exposed in both the compiled toJSON() representation, and the schema.getProperties() object?

Thank you for looking at this, it's really only a minor thing in the grand scheme of things, but it does mean less technical maintenance for us, so it is greatly appreciated.

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

No branches or pull requests

2 participants