Releases: vinejs/vine
Improved error reporting for fields inside arrays and infer schema input types
This release contains a couple of minor breaking changes. So let's first talk about them.
Improved error reporting for fields inside arrays ( Breaking )
In the previous versions of VineJS, the error reporting for fields inside arrays could have been better.
Given the following schema and data
const schema = vine.object({
categories: vine.array(vine.number()),
})
const data = {
categories: [1, 'foo', 'bar', 11],
}
The errors reported up until 2.0 were
{
field: 'categories.*',
index: 1,
message: 'The 1 field must be a number',
rule: 'number',
},
{
field: 'categories.*',
index: 2,
message: 'The 2 field must be a number',
rule: 'number',
}
If you notice, the field name inside arrays is defined as categories.*
and not the actual index of the item inside the array. Now, you may think that I can replace the *
with the index
property value and get a nested path to the item index within the array.
Well, the replacement of *
might work in this situation. But it will not work when there are errors inside nested arrays or the field that failed the validation is a grandchild of an array. Because the index
property only exists when the field is an immediate child of an array.
But anyway, after this release, you do not have to perform any manual substitutions. The field names are nested paths with the correct index. The following is an example of errors with @vinejs/vine@2
.
{
field: 'categories.1',
index: 1,
message: 'The 1 field must be a number',
rule: 'number',
},
{
field: 'categories.2',
index: 2,
message: 'The 2 field must be a number',
rule: 'number',
}
Infer Schema Input value ( Breaking )
After this release, you can infer the input values a Schema type accepts. Let's consider the following example.
import { InferInput } from '@vinejs/vine/types'
const schema = vine.object({
is_admin: vine.boolean()
})
InferInput<typeof Schema>
{
is_admin: boolean | string | number
}
If you notice, the is_admin
property accepts a boolean | string | number
. VineJS is built for parsing form inputs submitted over HTTP. Therefore, it receives all inputs as string
values and performs normalization before performing any sort of validation.
Because of this change, the BaseSchema
classes accept another generic value for the InputTypes
. So, if you use the BaseSchema anywhere in your apps, make sure to pass the Input type as the first generic argument.
Also, please consult this commit for a better understanding of the change. df27df8
Define error messages for specific array index or a wildcard ( New feature )
Now, you will be able to define custom error messages for specific array indexes with a wildcard fallback for rest of the indexes. For example:
{
"contacts.0.email.required": "The primary email address is required",
"contacts.*.email.required": "The email address is required",
}
Commits
- style: remove unused type 9dd733c
- feat: add support for inferring schema input types df27df8
- feat: improve error reporting for fields inside arrays 3d59dad
- chore: update dependencies 8ff246f
What's Changed
- refactor: dynamic import node:dns by @Julien-R44 in #24
- Add Valibot to benchmarks by @nakrovati in #33
- Fix lolo32 PR by @nakrovati in #39
- Fix typo in Symbol(schema_nme) by @Izurii in #36
- docs: update benchmarks by @nakrovati in #40
- feat: implement requiredIf rules by @thetutlage in #42
New Contributors
- @nakrovati made their first contribution in #33
- @Izurii made their first contribution in #36
Full Changelog: v1.7.0...v2.0.0
Improved error reporting for fields inside arrays and infer schema input types
This release contains a couple of minor breaking changes. So let's first talk about them.
Improved error reporting for fields inside arrays ( Breaking )
In the previous versions of VineJS, the error reporting for fields inside arrays could have been better.
Given the following schema and data
const schema = vine.object({
categories: vine.array(vine.number()),
})
const data = {
categories: [1, 'foo', 'bar', 11],
}
The errors reported up until 2.0 were
{
field: 'categories.*',
index: 1,
message: 'The 1 field must be a number',
rule: 'number',
},
{
field: 'categories.*',
index: 2,
message: 'The 2 field must be a number',
rule: 'number',
}
If you notice, the field name inside arrays is defined as categories.*
and not the actual index of the item inside the array. Now, you may think that I can replace the *
with the index
property value and get a nested path to the item index within the array.
Well, the replacement of *
might work in this situation. But it will not work when there are errors inside nested arrays or the field that failed the validation is a grandchild of an array. Because the index
property only exists when the field is an immediate child of an array.
But anyway, after this release, you do not have to perform any manual substitutions. The field names are nested paths with the correct index. The following is an example of errors with @vinejs/vine@2
.
{
field: 'categories.1',
index: 1,
message: 'The 1 field must be a number',
rule: 'number',
},
{
field: 'categories.2',
index: 2,
message: 'The 2 field must be a number',
rule: 'number',
}
Infer Schema Input value ( Breaking )
After this release, you can infer the input values a Schema type accepts. Let's consider the following example.
import { InferInput } from '@vinejs/vine/types'
const schema = vine.object({
is_admin: vine.boolean()
})
InferInput<typeof Schema>
{
is_admin: boolean | string | number
}
If you notice, the is_admin
property accepts a boolean | string | number
. VineJS is built for parsing form inputs submitted over HTTP. Therefore, it receives all inputs as string
values and performs normalization before performing any sort of validation.
Because of this change, the BaseSchema
classes accept another generic value for the InputTypes
. So, if you use the BaseSchema anywhere in your apps, make sure to pass the Input type as the first generic argument.
Also, please consult this commit for a better understanding of the change. df27df8
Define error messages for specific array index or a wildcard ( New feature )
Now, you will be able to define custom error messages for specific array indexes with a wildcard fallback for rest of the indexes. For example:
{
"contacts.0.email.required": "The primary email address is required",
"contacts.*.email.required": "The email address is required",
}
Commits
- style: remove unused type 9dd733c
- feat: add support for inferring schema input types df27df8
- feat: improve error reporting for fields inside arrays 3d59dad
- chore: update dependencies 8ff246f
What's Changed
- refactor: dynamic import node:dns by @Julien-R44 in #24
- Add Valibot to benchmarks by @nakrovati in #33
- Fix lolo32 PR by @nakrovati in #39
- Fix typo in Symbol(schema_nme) by @Izurii in #36
- docs: update benchmarks by @nakrovati in #40
- feat: implement requiredIf rules by @thetutlage in #42
New Contributors
- @nakrovati made their first contribution in #33
- @Izurii made their first contribution in #36
Full Changelog: v1.7.0...v2.0.0-0
Add requiredIf rules
Please check docs to learn how requiredIf
rules work. And check this PR to understand the difference between vine.union
and requiredIf
rules.
Commits
- feat: implement requiredIf rules (#42) 893d378
- chore: update dependencies 81beff7
- docs: update benchmarks (#40) 21ac492
- fix: typo on 'Symbol.for('schema_nme') (#36) d2a03a3
- Merge pull request #39 from nakrovati/fix-lolo32-pr ef50170
- fix: add joi & ajv to devDeps 200bb39
- Merge branch 'develop' into fix-lolo32-pr 63c49e2
- Merge pull request #33 from nakrovati/develop f94d274
- chore(benchmarks): add valibot 02ff0a5
- fix(benchmark): use namespace to import yup d6f5589
- chore(benchmarks): add ajv and joi benchmark libraries ffb2a4e
What's Changed
- Add Valibot to benchmarks by @nakrovati in #33
- Fix lolo32 PR by @nakrovati in #39
- Fix typo in Symbol(schema_nme) by @Izurii in #36
- docs: update benchmarks by @nakrovati in #40
- feat: implement requiredIf rules by @thetutlage in #42
New Contributors
- @nakrovati made their first contribution in #33
- @Izurii made their first contribution in #36
Full Changelog: v1.7.1...v1.8.0
Bug fix and performance improvements
- fix: unix timetamp validation with x format bcebea5
- style: format source code 9dd9d85
- chore: update dependencies 6e412b2
- refactor: performance optimizations 3e35b83
- chore: update dependencies 4c88fa1
- refactor: dynamic import node:dns 92a48c8
What's Changed
- refactor: dynamic import node:dns by @Julien-R44 in #24
Full Changelog: v1.7.0...v1.7.1
Support for validating dates
This release adds support for validating dates in VineJS. You may check the documentation here. https://vinejs.dev/docs/types/date
The vine.date
schema type accepts a string value formatted as a date and returns an instance of the JavaScript Date object. The reason we accept a string is because the data submitted over an HTTP request will always represent date/datetime as a string.
Once you have a date, you may validate it further by comparing it against a fixed value or compare it against values from other fields. You may refer the documentation to view all the available validation rules.
Commits
- refactor: changes to vine validator options normalization e85356b
- chore: update list of files to publish e07cb69
- docs(README): remove snyk badge 1b5c497
- docs: update github workflow badge url b39b00c
- chore: pin typescript to 5.2 02c2945
- feat: add weekday and weekend rules 223bb93
- feat: add first set of date validation rules c893f10
- feat: add support for comparing nested values in sameAs and notSameAs rules 628b4c7
- chore: update dependencies and generate types using tsc ce6c52c
- chore: update dependencies cf08e2a
- feat: Serialize messages and fields when converting toJSON 72d098d
- refactor(SimpleMessagesProperty): make fields property optional (#18) 16bd6e8
- Merge pull request #16 from vinejs/snyk-upgrade-ee4daf504d32e111676c4eb19cecf239 5d2a97a
- feat: upgrade camelcase from 7.0.1 to 8.0.0 f98e099
Bundling with tsup
Use validator.js specific imports
Export VineValidator class
- refactor: export VineValidator class cfaeeff
- style: format source code 74ca7e0
- chore: update dependencies 9b7bc07
Full Changelog: v1.5.1...v1.5.2
Fix: Make schema classes Macroable to be extensible
Add API to make validation metadata type-safe
In VineJS, you can pass runtime metadata to the validation pipeline, which you can access from the validation rules, union predicates, etc. The metadata API was not type-safe until now. However, this release allows you to define the static metadata types and a validation function to validate them at runtime.
Note: The metadata API is kept very simple because only a few schemas might need runtime metadata with a few properties to be functional.
One example is the unique
validation rule. You might want the unique validation rule to check all the database rows except the one for the currently logged-in user. In that case, you will pass the currently logged-in userId to the statically compiled validation schema using metadata as follows.
const updateUserValidator = vine.compile(
vine.object({
email: vine.string().email().unique((field) => {
console.log(field.meta.userId)
}),
})
)
await updateUserValidator.validate(data, {
meta: { userId: request.auth.user.id }
})
However, there is no way to know that updateUserValidator
needs the currently logged-in user id to be functional.
From @vinejs/vine@1.5.0
, you can use the withMetaData
method to define static types for the metadata a validator accepts. The schema will look as follows.
const updateUserValidator = vine
.withMetaData<{ userId: number }>()
.compile(
vine.object({
email: vine.string().email().unique((field) => {
console.log(field.meta.userId)
}),
})
)
You can pass a callback to withMetaData
to validate the metadata at runtime if needed.
vine
.withMetaData<{ userId: number }>((meta) => {
// validate id and throw an error
})
Commits
- feat: add support for defining static metadata types and validator function 09c4097
- chore: use @adonisjs/tooling presets for tooling config a02908d
- chore: upgrade japa to v3 4181ee4
- chore: update dependencies f24ebb8
- chore: add labels to exempt from stale and lock bot 92697c4
- docs: fix contributing link fcad2fb
Full Changelog: v1.4.1...v1.5.0