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.
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.
// 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
// 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"] }
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.
It's also worth highlighting some of the significant changes introduced in the previous Release 0.8, which brought more rigorous number validation:
s.number().format("float").int(); // Error: incompatible format and type s.number().int().format("double"); // Error: 'double' format not allowed for integers
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
These enhancements helped prevent logical errors during schema definition and ensured more consistent data validation.
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!
Link to the project: https://github.com/vitalics/ajv-ts
The above is the detailed content of Ajv-ts got . Whats new?. For more information, please follow other related articles on the PHP Chinese website!