Releases: vinejs/vine
Breaking changes and bug fixes
This release contains a few breaking changes along with a handful of new improvements and bug fixes.
Breaking changes
Infer type
The infer type of schema now marks optional
fields as optional
within the TypeScript types. This ensures the property can be missing altogether from the data object/inferred types vs being marked as undefined
explicitly. For example:
// Schema
vine.object({
username: vine.string(),
age: vine.number().optional(),
})
// Old output
{
age: number | undefined;
username: string;
}
// New output
{
age?: number | undefined;
username: string;
}
SUBTYPE symbol
Custom types extending the VineJS BaseLiteralType
now must define the symbols.SUBTYPE
property on the schema. This property can be used by schema transformers to get a more accurate type for the schema node. Here's how the StringSchema
defines the SUBTYPE
property.
For example:
import { symbols, BaseLiteralType } from '@vinejs/vine'
class MySchemaType extends BaseLiteralType<Input, Output, CamelCaseOutput> {
[symbols.SUBTYPE] = 'multipartFile'
}
Bug Fixes
- add tests to ensure field names with special chars are allowed (67f6c52), closes #50 #63 #82
- do not use Error.captureStackTrace when not available (715e761), closes #49
- infer types (#79) (54f5237)
Features
- add subtype property to schema output (818fbf0), closes #64
- add support for conditional validation in arrays, object, records and tuples (890228e), closes #71
- add support for parsing iso8601 dates (fe61951), closes #65
- add support for ULID validation (#58) (02d3a28)
Pull Requests
- Export VineNativeEnum class by @simoneNEMO in #60
- Add support for ULID validation by @simoneNEMO in #58
- docs: fix JSDoc comment on regex function by @Elliot67 in #77
- fix: infer types by @adamcikado in #79
New Contributors
- @simoneNEMO made their first contribution in #60
- @Elliot67 made their first contribution in #77
Full Changelog: v2.1.0...v3.0.0
Add "tryValidate", "toJSON" method and "in" validation rule
tryValidate
The tryValidate
method can be used to perform validation without throwing a validation error. Instead, the errors are returned as the return value of the method, which is a tuple.
const [error, result] = validator.tryValidate({ data: {} })
The try
prefix is inspired from the Java world.
in
The in
validation rule has been added for the VineNumber
schema type and can be used to ensure the value of field is part of the allowed values list.
toJSON
The validator.toJSON
method can be used to get the validator and its refs as JSON.
Commits
- feat: add tryValidate method to Vine (a70ff38)
- feat: add tryValidate method (cebb8e0)
- fix: add "in" rule in default number rules (#54) (39204e4)
- chore: migrate to release-it (0b5e212)
- feat: add in rule for number (#53) (72912af)
- feat: export modifiers (#48) (34e07fc)
- chore: update dependencies (a7e18b7)
- style: reformat codebase (62d450c)
- feat: add validator.toJSON method to get compiled schema and refs (5259933)
What's Changed
- feat: export modifiers by @adamcikado in #48
- feat: add in rule for number (#44) by @FACON-Nicolas in #53
- fix: add "in" rule in default number rules (#44) by @FACON-Nicolas in #54
New Contributors
- @adamcikado made their first contribution in #48
- @FACON-Nicolas made their first contribution in #53
Full Changelog: v2.0.0...v2.1.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
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