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

Combine Number AND Enum #44

Closed
Drulac opened this issue Mar 20, 2024 · 2 comments · Fixed by #53 or #54
Closed

Combine Number AND Enum #44

Drulac opened this issue Mar 20, 2024 · 2 comments · Fixed by #53 or #54
Assignees
Labels
Type: Enhancement Improving an existing feature

Comments

@Drulac
Copy link

Drulac commented Mar 20, 2024

Package version

1.8.0

Describe the bug

Hello 😁 !

Sorry, Github won't let me use the Feature Request issue type, forced to submit it as Bug Report :-/

TLDR

[✨ New feature] : Requesting Number (to autoconvert HTML form string to number) and Enum (to ensure the number is from a predefined list) types combination, like this :

vine.number().enum([1, 3, 4, 8])

A bit of the request context

If I want to let my user choice between some objects (already in database, to use as relation) I could use an HTML select, and (dynamicly, using a template) pass every object id as value :

<select name="objectId" id="objectId">
    <option value="1">A Choice</option>
    <option value="3">B Choice</option>
    <option value="4">C Choice</option>
    <option value="8">D Choice</option>
</select>

Because in HTML forms, the values are always strings. My form will send "1", "2" or "3" (or whatever if the user alter the form).

If I want to save in DB a correct objectId for the relation, I need to convert the objectId from string to Number, and then check if there is actually an object with this id in my database.

Using  number().range instead of enum isn't possible because some object's id are unavailables.

It give me (using AdonisJS) a controller store method like that :

async store({ request }: HttpContext) {
    const data = request.all()

    //converting string to number
    if (data.objectId) {
      data.objectId = parseInt(data.objectId)
    }

    //getting all corrects objects id from DB
    const availableObjectIds = (await Object.all())
      .map((obj) => obj.serialize().id)
    //should be equal to [1, 3, 4, 8]
    data.availableObjectIds = availableObjectIds

    const payload = await Validator.validate(data)

    return Objet.create(payload)
}

With a Validator like that :

export const Validator = vine.compile(
  vine.object({
    objectId: vine.enum((field) => {
      //taking our ids picked from DB
      //should be equal to [1, 3, 4, 8]
      return field.parent.availableObjectIds
    }),
  })
)

With number and enum combined

I could drop theses lines from the controller :

//converting string to number
if (data.objectId) {
    data.objectId = parseInt(data.objectId)
}
async store({ request }: HttpContext) {
    const data = request.all()

    //getting all corrects objects id from DB
    const availableObjectIds = (await Object.all())
      .map((obj) => obj.serialize().id)
    //should be equal to [1, 3, 4, 8]
    data.availableObjectIds = availableObjectIds

    const payload = await Validator.validate(data)

    return Objet.create(payload)
}

And my validator will simply look like that :

export const Validator = vine.compile(
 vine.object({
   //converting string to number
   objectId: vine.number().enum((field) => {
     //taking our ids picked from DB
     //should be equal to [1, 3, 4, 8]
     return field.parent.availableObjectIds
   }),
 })
)

I'm asking this feature because I think it's better to keep data type validation inside models definitions

Thanks for all your work on the Adonis environnement !

Reproduction repo

No response

@Drulac Drulac changed the title [✨ New feature] : Combine Number AND Enum Combine Number AND Enum Mar 20, 2024
@thetutlage
Copy link
Contributor

thetutlage commented Mar 28, 2024

How about using the number schema with the in validation rule. Something like this

vine.number().in([1, 3, 4, 8])

Do note, the in validation rule does not exists, but it can be added

@Drulac
Copy link
Author

Drulac commented Mar 30, 2024

It could do the job very well 😁 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Improving an existing feature
Projects
None yet
2 participants