Skip to content

Commit

Permalink
Feature: strict number validation (#65)
Browse files Browse the repository at this point in the history
* feat: strict number
  • Loading branch information
vitalics authored Sep 10, 2024
1 parent 0f787a7 commit 9a62d94
Show file tree
Hide file tree
Showing 20 changed files with 539 additions and 138 deletions.
30 changes: 30 additions & 0 deletions .changeset/chilled-seahorses-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
"ajv-ts": minor
---

Make [strict numbers](#strict-numbers)

### Strict numbers

We make validation for number `type`, `format`, `minValue` and `maxValue` fields. That means we handle it in our side so you get an error for invalid values.

Examples:

```ts
s.number().format('float').int() // error in type!
s.int().const(3.4) // error in type!
s.number().int().format('float') // error in format!
s.number().int().format('double') // error in format!

// ranges are also check for possibility

s.number().min(5).max(3) // error in range!
s.number().min(3).max(5).const(10) // error in constant - out of range!
```

## 🏡 Chore/Infra

- add [type-fest](https://www.npmjs.com/package/type-fest) library for correct type checking
- add [tsx](https://www.npmjs.com/package/tsx) package
- add minified files for cjs and esm modules in `dist` folder
- remove `bun-types` dependency
2 changes: 1 addition & 1 deletion .changeset/fair-dolls-lick.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"ajv-ts": patch
---

fix # 61
fix #61
2 changes: 1 addition & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
node_version: [18, 20, 22, latest]
pnpm_version: [9.4.0]
pnpm_version: [9.9.0]
steps:
- name: Clone repository
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
matrix:
node_version: [20]
pnpm_version: [9.4.0]
pnpm_version: [9.9.0]
steps:
- name: Clone repository
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tests
# Files
bun.lockb
yarn.lock
tsup.config.ts
tsup.config.*ts
tsconfig.json
.eslintrc.json
CONTRIBUTING.md
Expand Down
77 changes: 76 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
- [String](#string)
- [Typescript features](#typescript-features)
- [Numbers](#numbers)
- [Types](#types)
- [Number](#number)
- [Int](#int)
- [Formats](#formats)
- [int32](#int32)
- [int64](#int64)
- [float](#float)
- [double](#double)
- [Typescript features](#typescript-features-1)
- [BigInts](#bigints)
- [NaNs](#nans)
- [Dates](#dates)
Expand All @@ -40,7 +49,7 @@
- [`.element`](#element)
- [`.nonempty`](#nonempty)
- [`.min`/`.max`/`.length`/`.minLength`/`.maxLength`](#minmaxlengthminlengthmaxlength)
- [Typescript features](#typescript-features-1)
- [Typescript features](#typescript-features-2)
- [`.unique`](#unique)
- [`.contains`/`.minContains`](#containsmincontains)
- [Tuples](#tuples)
Expand Down Expand Up @@ -276,6 +285,72 @@ s.number().nonpositive(); // <= 0
s.number().multipleOf(5); // Evenly divisible by 5. Alias .step(5)
```

### Types

#### Number

Number - any number type

```ts
s.number()
// same as
s.number().number()
```

#### Int

Only integers values.

Note: we check in runtime non-integer format (`float`, `double`) and give an error.

```ts
s.number().int()
// or
s.number().integer()
// or
s.int()
```

### Formats

Defines in [ajv-formats](https://ajv.js.org/packages/ajv-formats.html#formats) package

#### int32

Signed 32 bits integer according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)

#### int64

Signed 64 bits according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)

#### float

float: float according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)

#### double

double: double according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)

### Typescript features

> from >= 0.8
We make validation for number `type`, `format`, `minValue` and `maxValue` fields. That means we handle it in our side so you get an error for invalid values.

Examples:

```ts
s.number().format('float').int() // error in type!
s.int().const(3.4) // error in type!
s.number().int().format('float') // error in format!
s.number().int().format('double') // error in format!

// ranges are also check for possibility

s.number().min(5).max(3) // error in range!
s.number().min(3).max(5).const(10) // error in constant!
```

## BigInts

Not supported
Expand Down
1 change: 0 additions & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ currently being supported with security updates.
| ------- | ------------------ |
| < 0.x | :white_check_mark: |


## Reporting a Vulnerability

Use this section to tell people how to report a vulnerability.
Expand Down
25 changes: 25 additions & 0 deletions UPCOMING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,33 @@

## ✅ New Features

- [strict number](#strict-numbers)

### Strict numbers

We make validation for number `type`, `format`, `minValue` and `maxValue` fields. That means we handle it in our side so you get an error for invalid values.

Examples:

```ts
s.number().format('float').int() // error in type!
s.int().const(3.4) // error in type!
s.number().int().format('float') // error in format!
s.number().int().format('double') // error in format!

// ranges are also check for possibility

s.number().min(5).max(3) // error in range!
s.number().min(3).max(5).const(10) // error in constant - out of range!
```

## 🐛 Bug Fixes

- [#61](https://github.com/vitalics/ajv-ts/issues/61)

## 🏡 Chore/Infra

- add [type-fest](https://www.npmjs.com/package/type-fest) library for correct type checking
- add [tsx](https://www.npmjs.com/package/tsx) package
- add minified files for cjs and esm modules in `dist` folder
- remove `bun-types` dependency
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"packageManager": "pnpm@9.4.0",
"packageManager": "pnpm@9.9.0",
"scripts": {
"build": "tsup",
"build": "tsx ./tsup.config.mts",
"test": "vitest run",
"test:watch": "vitest",
"ci:version": "changeset version",
Expand Down Expand Up @@ -47,7 +47,6 @@
"@typescript-eslint/eslint-plugin": "6.4.0",
"@vitest/ui": "1.6.0",
"benchmark": "2.1.4",
"bun-types": "1.1.18",
"eslint": "8.0.1",
"eslint-plugin-import": "2.25.2",
"eslint-plugin-n": "15.0.0",
Expand All @@ -62,6 +61,7 @@
"dependencies": {
"ajv": "8.16.0",
"ajv-errors": "3.0.0",
"ajv-formats": "3.0.1"
"ajv-formats": "3.0.1",
"type-fest": "4.26.0"
}
}
}
Loading

0 comments on commit 9a62d94

Please sign in to comment.