首页 > web前端 > js教程 > 正文

Ajv-ts got . What&#s new?

DDD
发布: 2024-09-18 21:55:03
原创
461 人浏览过

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
登录后复制
  • 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"] }
登录后复制

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
登录后复制
  • 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
登录后复制

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

以上是Ajv-ts got . What&#s new?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!