vine

VineJS is a form data validation library for Node.js

MIT License

Downloads
190.8K
Stars
1.1K

Bot releases are hidden (Show)

vine - Add "tryValidate", "toJSON" method and "in" validation rule Latest Release

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.

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

New Contributors

Full Changelog: https://github.com/vinejs/vine/compare/v2.0.0...v2.1.0

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. https://github.com/vinejs/vine/commit/df27df8314e019f6cf22619e46dfbbebe2a41737

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

New Contributors

Full Changelog: https://github.com/vinejs/vine/compare/v1.7.0...v2.0.0

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. https://github.com/vinejs/vine/commit/df27df8314e019f6cf22619e46dfbbebe2a41737

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

New Contributors

Full Changelog: https://github.com/vinejs/vine/compare/v1.7.0...v2.0.0-0

vine - Add requiredIf rules

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.

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

New Contributors

Full Changelog: https://github.com/vinejs/vine/compare/v1.7.1...v1.8.0

vine - Bug fix and performance improvements

Published by thetutlage 9 months ago

  • 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

Full Changelog: https://github.com/vinejs/vine/compare/v1.7.0...v1.7.1

vine - Support for validating dates

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.

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

https://github.com/vinejs/vine/compare/v1.6.0...v1.7.0

vine - Bundling with tsup

Published by Julien-R44 about 1 year ago

  • chore: add tsup bundling (#14) 627ee41

https://github.com/vinejs/vine/compare/v1.5.3...v1.6.0

vine - Use validator.js specific imports

Published by Julien-R44 about 1 year ago

  • refactor: use validator.js specific imports (#13) 459f3e5

https://github.com/vinejs/vine/compare/v1.5.2...v1.5.3

vine - Export VineValidator class

Published by thetutlage over 1 year ago

  • refactor: export VineValidator class cfaeeff
  • style: format source code 74ca7e0
  • chore: update dependencies 9b7bc07

Full Changelog: https://github.com/vinejs/vine/compare/v1.5.1...v1.5.2

vine - Fix: Make schema classes Macroable to be extensible

Published by thetutlage over 1 year ago

  • ci: fix failing tests 2f5258c
  • ci: remove test.yml workflow file 89efc20
  • test: fix failing tests d04800c
  • test: add test for extending Vine class a417418
  • fix: make schema classes Macroable 41bd3d5

Full Changelog: https://github.com/vinejs/vine/compare/v1.5.0...v1.5.1

vine - Add API to make validation metadata type-safe

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
 })

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: https://github.com/vinejs/vine/compare/v1.4.1...v1.5.0

vine - Export testing factories

Published by thetutlage over 1 year ago

  • fix: export testing factories ffe8279

Full Changelog: https://github.com/vinejs/vine/compare/v1.4.0...v1.4.1

vine - Add strict mode to number schema type

Published by thetutlage over 1 year ago

  • feat: add strict mode to number type 84c3890

Full Changelog: https://github.com/vinejs/vine/compare/v1.3.0...v1.4.0

vine - Implement additional rules

Published by thetutlage over 1 year ago

  • feat: implement normalizeUrl rule c3a893f
  • refactor: remove he dependency and encode rule b16ea5d
  • feat: implement escape and encode rules 7d6ba6d
  • feat: implement toUpperCase, toLowerCase and toCamelCase rules 0c9be26
  • feat: implement ascii, iban, jwt, and coordinates rules 0ae33d7
  • feat: implement uuid rule 2372fae
  • feat: implement postal code rule eff6f61
  • feat: implement passport rule 53f402a
  • feat: implement creditCard rule 9cd8fdf
  • chore: update dependencies dd68dfe
  • refactor: export SimpleMessagesProvider and SimpleErrorReporter 8eaf6d2
  • docs: update benchmarks 8532cfa
  • feat: define otherwise error reporter for unions 45646f2
  • chore(benchmarks): union conditions (#1) 869d787

What's Changed

New Contributors

Full Changelog: https://github.com/vinejs/vine/compare/v1.2.0...v1.3.0

vine - Adding new rules

Published by thetutlage over 1 year ago

  • feat: implement ipAddress rule df41d23
  • feat: implement in and notIn rules ed9fa5c
  • feat: implement sameAs and notSameAs rules 5817f2b
  • feat: implement startsWith and endsWith rules c6fb726
  • feat: implement trim and normalizeEmail rules 63dcdef
  • feat: implement confirmed rule cf27cb5
  • feat: add size based string rules 935a751
  • test: use datasets be04d1c
  • feat: implement string rules bee7a9e
  • refactor: rename ctx references with field ea63dd2
  • chore: update dependencies e44de42

Full Changelog: https://github.com/vinejs/vine/compare/v1.1.1...v1.2.0

vine - Exporting additionally classes

Published by thetutlage over 1 year ago

  • feat: enable defining custom error reporter at all layers 0138b5f
  • feat: export Vine class bc38c04
  • feat: export base classes 33026e3

Full Changelog: https://github.com/vinejs/vine/compare/v1.1.0...v1.1.1

vine - Improvements to the API for defining messages and fields

Published by thetutlage over 1 year ago

  • docs: update benchmarks b874b87
  • feat: export defaults submodule b9cbbeb
  • refactor: cleanup messages and fields API 68a1186
  • chore: update dependencies 22996f5
  • docs: fix npm badge URL 4479bdf
  • chore: add repo details cf9905b

https://github.com/vinejs/vine/compare/v1.0.0...v1.1.0

vine - First working release

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