Home > Web Front-end > JS Tutorial > Ajv-ts got . What&#s new?

Ajv-ts got . What&#s new?

DDD
Release: 2024-09-18 21:55:03
Original
473 people have browsed it

Ajv-ts got . What

The latest Release 0.9 of the Schema Validation Toolkit - ajv-ts brings a suite of enhancements to improve developer productivity and ensure better validation accuracy. Whether you're building complex data models or just handling basic input, these updates help streamline schema definitions with more robust features and examples. Here's an overview of the key updates introduced in this release, along with a recap of the significant changes from Release 0.7.

Schema Examples for Better Clarity

One of the major updates in Release 0.9 is the introduction of schema examples for every data type. This feature, allows developers to include specific examples directly within their schema definitions, offering more transparency and ease of use. You can also use it with any argument length.

  • For Strings:
// Valid examples for a string schema
const myString = s.string().examples(["hello", "world"]);
myString.schema // // {type: 'string', examples: ['hello', 'world']}
s.string().examples("hello", "world"); // OKs
Copy after login
  • For Numbers:
// Valid examples for a number schema
const myNum1 = s.number().examples(1, 2, 3);
myNum1.schema // {type: 'number', examples: [1,2,3]}

// TypeScript error for invalid number types
// @ts-ignore fails ts, but schema still ok
const myNum2 = s.number().examples(["abc", "123"]);
myNum2.schema // {type: "number", examples: ["abc", "123"] }
Copy after login

This change helps validate the correct data types and improves documentation within your code, making it easier for teams to understand what values are expected.

Stricter Number Validation

It's also worth highlighting some of the significant changes introduced in the previous Release 0.8, which brought more rigorous number validation:

  • Format and Type Enforcement: This update introduced stricter rules for number formats. For instance, only specific combinations like integers and floats are allowed, while incorrect formats result in errors:
s.number().format("float").int(); // Error: incompatible format and type
s.number().int().format("double"); // Error: 'double' format not allowed for integers
Copy after login
  • Range Validation: Another feature of Release 0.8 was the introduction of automatic range checking. Developers can specify minimum and maximum values for numbers, and the system will throw an error if the value is out of range.
s.number().min(5).max(3); // Error: max cannot be smaller than min
s.number().min(1).max(10).const(15); // Error: constant is out of specified range
Copy after login

These enhancements helped prevent logical errors during schema definition and ensured more consistent data validation.

Other changes

  • Changeset Management with GitHub
  • Update pnpm version to 9.10.0
  • Meta Object Updates for meta method

Conclusion

The Release 0.9 and Release 0.8 updates significantly improve the developer experience by offering better examples, stricter validation, and enhanced error handling—especially for TypeScript users. These features make schema definition more intuitive, reliable, and easier to maintain in complex projects. Be sure to explore the new functionality to make your validation workflows more efficient!

Happy schema definition!

Link to the project: https://github.com/vitalics/ajv-ts

The above is the detailed content of Ajv-ts got . What&#s new?. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template