Skip to content

Commit a08c2c4

Browse files
Merge pull request #1171 from jasperteo/nanoid
fix: update nanoid to be more accurate
2 parents 7731a0f + 8b0b896 commit a08c2c4

7 files changed

Lines changed: 45 additions & 27 deletions

File tree

library/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes to the library will be documented in this file.
1010
- Add `entries` and `notEntries` validation action to validate number of object entries (pull request #1156)
1111
- Add support for bigints to `multipleOf` validation action (pull request #1164)
1212
- Change implementation of `variant` and `variantAsync` schema to improve performance by aborting validation of discriminators early (pull request #1110)
13+
- Change name of `NanoIDAction` and `NanoIDIssue` interface to `NanoIdAction` and `NanoIdIssue` (pull request #1171)
1314

1415
## v1.0.0 (March 18, 2025)
1516

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { describe, expectTypeOf, test } from 'vitest';
22
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts';
3-
import { nanoid, type NanoIDAction, type NanoIDIssue } from './nanoid.ts';
3+
import { nanoid, type NanoIdAction, type NanoIdIssue } from './nanoid.ts';
44

55
describe('nanoid', () => {
66
describe('should return action object', () => {
77
test('with undefined message', () => {
8-
type Action = NanoIDAction<string, undefined>;
8+
type Action = NanoIdAction<string, undefined>;
99
expectTypeOf(nanoid<string>()).toEqualTypeOf<Action>();
1010
expectTypeOf(
1111
nanoid<string, undefined>(undefined)
@@ -14,19 +14,19 @@ describe('nanoid', () => {
1414

1515
test('with string message', () => {
1616
expectTypeOf(nanoid<string, 'message'>('message')).toEqualTypeOf<
17-
NanoIDAction<string, 'message'>
17+
NanoIdAction<string, 'message'>
1818
>();
1919
});
2020

2121
test('with function message', () => {
2222
expectTypeOf(nanoid<string, () => string>(() => 'message')).toEqualTypeOf<
23-
NanoIDAction<string, () => string>
23+
NanoIdAction<string, () => string>
2424
>();
2525
});
2626
});
2727

2828
describe('should infer correct types', () => {
29-
type Action = NanoIDAction<string, undefined>;
29+
type Action = NanoIdAction<string, undefined>;
3030

3131
test('of input', () => {
3232
expectTypeOf<InferInput<Action>>().toEqualTypeOf<string>();
@@ -37,7 +37,7 @@ describe('nanoid', () => {
3737
});
3838

3939
test('of issue', () => {
40-
expectTypeOf<InferIssue<Action>>().toEqualTypeOf<NanoIDIssue<string>>();
40+
expectTypeOf<InferIssue<Action>>().toEqualTypeOf<NanoIdIssue<string>>();
4141
});
4242
});
4343
});

library/src/actions/nanoid/nanoid.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { describe, expect, test } from 'vitest';
22
import { NANO_ID_REGEX } from '../../regex.ts';
33
import type { StringIssue } from '../../schemas/index.ts';
44
import { expectActionIssue, expectNoActionIssue } from '../../vitest/index.ts';
5-
import { nanoid, type NanoIDAction, type NanoIDIssue } from './nanoid.ts';
5+
import { nanoid, type NanoIdAction, type NanoIdIssue } from './nanoid.ts';
66

77
describe('nanoid', () => {
88
describe('should return action object', () => {
9-
const baseAction: Omit<NanoIDAction<string, never>, 'message'> = {
9+
const baseAction: Omit<NanoIdAction<string, never>, 'message'> = {
1010
kind: 'validation',
1111
type: 'nanoid',
1212
reference: nanoid,
@@ -17,7 +17,7 @@ describe('nanoid', () => {
1717
};
1818

1919
test('with undefined message', () => {
20-
const action: NanoIDAction<string, undefined> = {
20+
const action: NanoIdAction<string, undefined> = {
2121
...baseAction,
2222
message: undefined,
2323
};
@@ -29,15 +29,15 @@ describe('nanoid', () => {
2929
expect(nanoid('message')).toStrictEqual({
3030
...baseAction,
3131
message: 'message',
32-
} satisfies NanoIDAction<string, string>);
32+
} satisfies NanoIdAction<string, string>);
3333
});
3434

3535
test('with function message', () => {
3636
const message = () => 'message';
3737
expect(nanoid(message)).toStrictEqual({
3838
...baseAction,
3939
message,
40-
} satisfies NanoIDAction<string, typeof message>);
40+
} satisfies NanoIdAction<string, typeof message>);
4141
});
4242
});
4343

@@ -91,7 +91,7 @@ describe('nanoid', () => {
9191

9292
describe('should return dataset with issues', () => {
9393
const action = nanoid('message');
94-
const baseIssue: Omit<NanoIDIssue<string>, 'input' | 'received'> = {
94+
const baseIssue: Omit<NanoIdIssue<string>, 'input' | 'received'> = {
9595
kind: 'validation',
9696
type: 'nanoid',
9797
expected: null,

library/src/actions/nanoid/nanoid.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { _addIssue } from '../../utils/index.ts';
99
/**
1010
* Nano ID issue interface.
1111
*/
12-
export interface NanoIDIssue<TInput extends string> extends BaseIssue<TInput> {
12+
export interface NanoIdIssue<TInput extends string> extends BaseIssue<TInput> {
1313
/**
1414
* The issue kind.
1515
*/
@@ -32,13 +32,20 @@ export interface NanoIDIssue<TInput extends string> extends BaseIssue<TInput> {
3232
readonly requirement: RegExp;
3333
}
3434

35+
/**
36+
* Nano ID issue type.
37+
*
38+
* @deprecated Use `NanoIdIssue` instead.
39+
*/
40+
export type NanoIDIssue<TInput extends string> = NanoIdIssue<TInput>;
41+
3542
/**
3643
* Nano ID action interface.
3744
*/
38-
export interface NanoIDAction<
45+
export interface NanoIdAction<
3946
TInput extends string,
40-
TMessage extends ErrorMessage<NanoIDIssue<TInput>> | undefined,
41-
> extends BaseValidation<TInput, TInput, NanoIDIssue<TInput>> {
47+
TMessage extends ErrorMessage<NanoIdIssue<TInput>> | undefined,
48+
> extends BaseValidation<TInput, TInput, NanoIdIssue<TInput>> {
4249
/**
4350
* The action type.
4451
*/
@@ -61,12 +68,22 @@ export interface NanoIDAction<
6168
readonly message: TMessage;
6269
}
6370

71+
/**
72+
* Nano ID action type.
73+
*
74+
* @deprecated Use `NanoIdAction` instead.
75+
*/
76+
export type NanoIDAction<
77+
TInput extends string,
78+
TMessage extends ErrorMessage<NanoIdIssue<TInput>> | undefined,
79+
> = NanoIdAction<TInput, TMessage>;
80+
6481
/**
6582
* Creates a [Nano ID](https://github.com/ai/nanoid) validation action.
6683
*
6784
* @returns A Nano ID action.
6885
*/
69-
export function nanoid<TInput extends string>(): NanoIDAction<
86+
export function nanoid<TInput extends string>(): NanoIdAction<
7087
TInput,
7188
undefined
7289
>;
@@ -80,13 +97,13 @@ export function nanoid<TInput extends string>(): NanoIDAction<
8097
*/
8198
export function nanoid<
8299
TInput extends string,
83-
const TMessage extends ErrorMessage<NanoIDIssue<TInput>> | undefined,
84-
>(message: TMessage): NanoIDAction<TInput, TMessage>;
100+
const TMessage extends ErrorMessage<NanoIdIssue<TInput>> | undefined,
101+
>(message: TMessage): NanoIdAction<TInput, TMessage>;
85102

86103
// @__NO_SIDE_EFFECTS__
87104
export function nanoid(
88-
message?: ErrorMessage<NanoIDIssue<string>>
89-
): NanoIDAction<string, ErrorMessage<NanoIDIssue<string>> | undefined> {
105+
message?: ErrorMessage<NanoIdIssue<string>>
106+
): NanoIdAction<string, ErrorMessage<NanoIdIssue<string>> | undefined> {
90107
return {
91108
kind: 'validation',
92109
type: 'nanoid',

packages/to-json-schema/src/convertAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ type Action =
8585
number,
8686
v.ErrorMessage<v.MultipleOfIssue<number, number>> | undefined
8787
>
88-
| v.NanoIDAction<string, v.ErrorMessage<v.NanoIDIssue<string>> | undefined>
88+
| v.NanoIdAction<string, v.ErrorMessage<v.NanoIdIssue<string>> | undefined>
8989
| v.NonEmptyAction<
9090
v.LengthInput,
9191
v.ErrorMessage<v.NonEmptyIssue<v.LengthInput>> | undefined

website/src/routes/api/(actions)/nanoid/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ The following examples show how `nanoid` can be used.
4747
Schema to validate a Nano ID.
4848

4949
```ts
50-
const NanoIDSchema = v.pipe(
50+
const NanoIdSchema = v.pipe(
5151
v.string(),
5252
v.nanoid('The Nano ID is badly formatted.'),
5353
v.length(21, 'The Nano ID must be 21 characters long.')

website/src/routes/api/(actions)/nanoid/properties.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export const properties: Record<string, PropertyProps> = {
1717
generics: [
1818
{
1919
type: 'custom',
20-
name: 'NanoIDIssue',
21-
href: '../NanoIDIssue/',
20+
name: 'NanoIdIssue',
21+
href: '../NanoIdIssue/',
2222
generics: [
2323
{
2424
type: 'custom',
@@ -41,8 +41,8 @@ export const properties: Record<string, PropertyProps> = {
4141
Action: {
4242
type: {
4343
type: 'custom',
44-
name: 'NanoIDAction',
45-
href: '../NanoIDAction/',
44+
name: 'NanoIdAction',
45+
href: '../NanoIdAction/',
4646
generics: [
4747
{
4848
type: 'custom',

0 commit comments

Comments
 (0)