How to insert a ValueBuilder with JSON Schema having a required array field?
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 7.3k
- Forks
- 637
- PR merge metrics
- No merged PRs in 30d
Description
Hello Objection community! 😃
Environment
{
"objection": "^2.2.4",
}
Context
I tried to insert an array of UUIDs using objection in a column defined as uuid[]. However, I had the following error message:
malformed array literal
Example of insert:
MyModel.query().insertAndFetch({
imageIdsArray: ['344e5433-97ea-4abb-be31-7ca7d8da7683'],
})
Workaround
Then by doing some research, I found the following solution when inserting:
val(...).asArray().castTo('uuid[]')
Example of insert:
MyModel.query().insertAndFetch({
imageIdsArray: val(['344e5433-97ea-4abb-be31-7ca7d8da7683']).asArray().castTo('uuid[]'),
})
However, I kept having an issue if my model's field is mandatory in the JSON schema.
static get jsonSchema(): {} {
return {
type: 'object',
required: ['imageIdsArray'],
properties: {
imageIdsArray: {
type: ['array'],
items: { type: 'string', format: 'uuid' },
},
},
};
}
Here is the error message:
imageIdsArray: is a required property
The solution I found is to remove the column as required, and then it worked perfectly.
static get jsonSchema(): {} {
return {
type: 'object',
required: [], // If required doesn't have imageIdsArray, it works!
properties: {
imageIdsArray: {
type: ['array'],
items: { type: 'string', format: 'uuid' },
},
},
};
}
Questions
- Did I miss something that I wasn't aware of?
- Is there a way to do it differently that would allow me to have the column as required?
Thanks in advance for your help! I hope I formatted well the issue 🙏
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue using the shown MyModel.query().insertAndFetch examples, val().asArray().castTo('uuid[]'), and the jsonSchema with imageIdsArray in required. Read the validation and insert handling for ValueBuilder expressions, then establish whether required fields can accept this expression while preserving the UUID array behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, postgresql
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100