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

Ajv-ts got . What&#s new?

Sep 18, 2024 pm 09:55 PM

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

jQuery get element padding/margin jQuery get element padding/margin Mar 01, 2025 am 08:53 AM

jQuery get element padding/margin

jQuery Check if Date is Valid jQuery Check if Date is Valid Mar 01, 2025 am 08:51 AM

jQuery Check if Date is Valid

10 jQuery Accordions Tabs 10 jQuery Accordions Tabs Mar 01, 2025 am 01:34 AM

10 jQuery Accordions Tabs

10 Worth Checking Out jQuery Plugins 10 Worth Checking Out jQuery Plugins Mar 01, 2025 am 01:29 AM

10 Worth Checking Out jQuery Plugins

HTTP Debugging with Node and http-console HTTP Debugging with Node and http-console Mar 01, 2025 am 01:37 AM

HTTP Debugging with Node and http-console

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

jquery add scrollbar to div jquery add scrollbar to div Mar 01, 2025 am 01:30 AM

jquery add scrollbar to div

See all articles