You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 :
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 :
asyncstore({ request }: HttpContext){constdata=request.all()//converting string to numberif(data.objectId){data.objectId=parseInt(data.objectId)}//getting all corrects objects id from DBconstavailableObjectIds=(awaitObject.all()).map((obj)=>obj.serialize().id)//should be equal to [1, 3, 4, 8]data.availableObjectIds=availableObjectIdsconstpayload=awaitValidator.validate(data)returnObjet.create(payload)}
With a Validator like that :
exportconstValidator=vine.compile(vine.object({objectId: vine.enum((field)=>{//taking our ids picked from DB//should be equal to [1, 3, 4, 8]returnfield.parent.availableObjectIds}),}))
With number and enum combined
I could drop theses lines from the controller :
//converting string to numberif(data.objectId){data.objectId=parseInt(data.objectId)}
asyncstore({ request }: HttpContext){constdata=request.all()//getting all corrects objects id from DBconstavailableObjectIds=(awaitObject.all()).map((obj)=>obj.serialize().id)//should be equal to [1, 3, 4, 8]data.availableObjectIds=availableObjectIdsconstpayload=awaitValidator.validate(data)returnObjet.create(payload)}
And my validator will simply look like that :
exportconstValidator=vine.compile(vine.object({//converting string to numberobjectId: vine.number().enum((field)=>{//taking our ids picked from DB//should be equal to [1, 3, 4, 8]returnfield.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
The text was updated successfully, but these errors were encountered:
Drulac
changed the title
[✨ New feature] : Combine Number AND Enum
Combine Number AND Enum
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 asBug 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 :
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 :
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 theobjectId
from string toNumber
, and then check if there is actually an object with thisid
in my database.Using
number().range
instead ofenum
isn't possible because some object's id are unavailables.It give me (using AdonisJS) a controller store method like that :
With a
Validator
like that :With number and enum combined
I could drop theses lines from the controller :
And my validator will simply look like that :
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
The text was updated successfully, but these errors were encountered: