VineJS is a form data validation library for Node.js
MIT License
Bot releases are hidden (Show)
Published by thetutlage 5 months ago
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.
Full Changelog: https://github.com/vinejs/vine/compare/v2.0.0...v2.1.0
Published by thetutlage 7 months ago
This release contains a couple of minor breaking changes. So let's first talk about them.
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',
}
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. https://github.com/vinejs/vine/commit/df27df8314e019f6cf22619e46dfbbebe2a41737
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",
}
Full Changelog: https://github.com/vinejs/vine/compare/v1.7.0...v2.0.0
Published by thetutlage 7 months ago
This release contains a couple of minor breaking changes. So let's first talk about them.
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',
}
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. https://github.com/vinejs/vine/commit/df27df8314e019f6cf22619e46dfbbebe2a41737
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",
}
Full Changelog: https://github.com/vinejs/vine/compare/v1.7.0...v2.0.0-0
Published by thetutlage 7 months ago
Please check docs to learn how requiredIf
rules work. And check this PR to understand the difference between vine.union
and requiredIf
rules.
Full Changelog: https://github.com/vinejs/vine/compare/v1.7.1...v1.8.0
Published by thetutlage 9 months ago
Full Changelog: https://github.com/vinejs/vine/compare/v1.7.0...v1.7.1
Published by thetutlage 11 months ago
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.
Published by Julien-R44 about 1 year ago
Published by Julien-R44 about 1 year ago
Published by thetutlage over 1 year ago
Full Changelog: https://github.com/vinejs/vine/compare/v1.5.1...v1.5.2
Published by thetutlage over 1 year ago
Full Changelog: https://github.com/vinejs/vine/compare/v1.5.0...v1.5.1
Published by thetutlage over 1 year ago
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/[email protected]
, 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
})
Full Changelog: https://github.com/vinejs/vine/compare/v1.4.1...v1.5.0
Published by thetutlage over 1 year ago
Full Changelog: https://github.com/vinejs/vine/compare/v1.4.0...v1.4.1
Published by thetutlage over 1 year ago
Full Changelog: https://github.com/vinejs/vine/compare/v1.3.0...v1.4.0
Published by thetutlage over 1 year ago
Full Changelog: https://github.com/vinejs/vine/compare/v1.2.0...v1.3.0
Published by thetutlage over 1 year ago
Full Changelog: https://github.com/vinejs/vine/compare/v1.1.1...v1.2.0
Published by thetutlage over 1 year ago
Full Changelog: https://github.com/vinejs/vine/compare/v1.1.0...v1.1.1
Published by thetutlage over 1 year ago
Published by thetutlage over 1 year ago
Hold your horses, the release is not polished and some of the APIs are rough. Wait for the official announcement
Full Changelog: https://github.com/vinejs/vine/commits/v1.0.0