vinejs / vine

VineJS is a form data validation library for Node.js
https://vinejs.dev
MIT License
1.05k stars 21 forks source link

Cannot get schema name when using modifier #64

Open james-atlasopen opened 1 month ago

james-atlasopen commented 1 month ago

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 commented 1 month ago

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

james-atlasopen commented 1 month ago

@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 commented 1 month ago

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())
james-atlasopen commented 1 month ago

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 commented 1 month ago

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

james-atlasopen commented 1 month ago

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.