idora.js-zod 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +109 -2
- package/dist/index.d.ts +89 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +135 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jake Seracino
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,110 @@
|
|
|
1
|
-
#
|
|
1
|
+
# idora.js-zod
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Zod](https://zod.dev) schemas for [Idora](https://github.com/SrKleRAimBot/Idora/tree/main/Idora.JS)
|
|
4
|
+
identifiers such as `usr_01k2f9v8ta7m3x0qbrn4dzeh5c`.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { idoraId } from 'idora.js-zod';
|
|
8
|
+
import * as z from 'zod';
|
|
9
|
+
|
|
10
|
+
const Params = z.object({ userId: idoraId({ prefix: 'usr' }) });
|
|
11
|
+
|
|
12
|
+
Params.parse({ userId: 'usr_01k226e0g0008j4ct4ank7f24s' }); // { userId: Id<'usr'> }
|
|
13
|
+
Params.parse({ userId: 'ord_01k226e0g0008j4ct4ank7f24s' });
|
|
14
|
+
// ZodError: Invalid identifier: expected the prefix 'usr', received 'ord'
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
One line instead of a hand-written regular expression that is almost right: the schema applies
|
|
18
|
+
exactly the rules every Idora package parses by, says precisely what is wrong with a rejected
|
|
19
|
+
identifier, and types what it accepts as a checked `Id<'usr'>` rather than a bare `string`.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install idora.js-zod idora.js zod
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`idora.js` and `zod` (4.1 or later) are peer dependencies — you already have them, and this package
|
|
30
|
+
adds no others. It is an ES module with TypeScript declarations included, and runs wherever Zod and
|
|
31
|
+
`idora.js` do.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Schemas
|
|
36
|
+
|
|
37
|
+
### `idoraId(options?)`
|
|
38
|
+
|
|
39
|
+
A string schema that accepts Idora identifiers. Its output is the input string, unchanged —
|
|
40
|
+
parsing is strict, so a valid identifier never needs rewriting — typed as
|
|
41
|
+
[`Id`](https://github.com/SrKleRAimBot/Idora/tree/main/Idora.JS#a-branded-type-for-checked-identifiers).
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
idoraId(); // any well-formed identifier → Id
|
|
45
|
+
idoraId({ prefix: 'usr' }); // exactly the prefix usr → Id<'usr'>
|
|
46
|
+
idoraId({ entity: Order, registry: ids }); // the prefix Order is registered with → Id
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`registry` is your `Idora` generator, or its `registry`. The prefix is looked up when the schema is
|
|
50
|
+
built, so naming an unregistered entity type throws `EntityPrefixNotRegisteredError` at start-up
|
|
51
|
+
rather than on the first request; an invalid `prefix` throws `InvalidEntityPrefixError` the same
|
|
52
|
+
way.
|
|
53
|
+
|
|
54
|
+
### `parsedIdoraId(options?)`
|
|
55
|
+
|
|
56
|
+
The same checks, as a [codec](https://zod.dev/codecs) that decodes into a parsed `IdoraId` — with
|
|
57
|
+
its `prefix`, `createdAt` and natural order — and encodes one back into its text:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { parsedIdoraId } from 'idora.js-zod';
|
|
61
|
+
|
|
62
|
+
const Query = z.object({ after: parsedIdoraId({ prefix: 'evt' }) });
|
|
63
|
+
|
|
64
|
+
const { after } = Query.parse(request.query);
|
|
65
|
+
after.createdAt; // when the event was minted
|
|
66
|
+
|
|
67
|
+
z.encode(Query, { after }); // { after: 'evt_01k2…' }
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Encoding checks the text on its way out, so an `IdoraId` with the wrong prefix is refused in that
|
|
71
|
+
direction too.
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## Issues
|
|
76
|
+
|
|
77
|
+
A rejected identifier raises one `custom` issue. Its message is short and meant for whoever sent
|
|
78
|
+
the identifier; its `params` say why, for code that handles a `ZodError`:
|
|
79
|
+
|
|
80
|
+
| `params.reason` | Default message |
|
|
81
|
+
| --- | --- |
|
|
82
|
+
| `noSeparator` | Invalid identifier: expected a prefix and a value separated by '_', such as usr_01k2f9v8ta7m3x0qbrn4dzeh5c |
|
|
83
|
+
| `prefix` | Invalid identifier: the prefix before '_' must be 1 to 16 ASCII letters or digits |
|
|
84
|
+
| `value` | Invalid identifier: the value after '_' must be a lowercase ULID or UUIDv7 |
|
|
85
|
+
| `prefixMismatch` | Invalid identifier: expected the prefix 'usr', received 'ord' |
|
|
86
|
+
|
|
87
|
+
`params.expectedPrefix` is the prefix the schema expected, if any. The reasons are those of
|
|
88
|
+
`InvalidIdError` in `idora.js`, and are checked in that order. Anything that is not a string is
|
|
89
|
+
rejected by `z.string()` in the usual way, before any of this.
|
|
90
|
+
|
|
91
|
+
Replace the message with `error`, either fixed or built from the `InvalidIdError`:
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
idoraId({ prefix: 'usr', error: 'That is not a user' });
|
|
95
|
+
idoraId({ entity: User, registry: ids, error: (error) => `Not a user identifier (${error.reason})` });
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## JSON Schema and OpenAPI
|
|
101
|
+
|
|
102
|
+
`idoraId()` is a plain string schema with a check attached, so `z.toJSONSchema` describes it as
|
|
103
|
+
`{ "type": "string" }`. `parsedIdoraId()` has a string as its input and an `IdoraId` as its output,
|
|
104
|
+
so describe it with `{ io: 'input' }`.
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## License
|
|
109
|
+
|
|
110
|
+
MIT — see [LICENSE](https://github.com/SrKleRAimBot/Idora/blob/main/LICENSE).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* idora.js-zod — Zod schemas for Idora identifiers such as `usr_01k2f9v8ta7m3x0qbrn4dzeh5c`.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
import { EntityPrefixRegistry, type EntityType, type Id, Idora, IdoraId, InvalidIdError, type InvalidIdReason } from 'idora.js';
|
|
7
|
+
import * as z from 'zod';
|
|
8
|
+
/** What to say when an identifier is rejected: a fixed message, or one built from the error. */
|
|
9
|
+
export type IdoraIdErrorMessage = string | ((error: InvalidIdError) => string);
|
|
10
|
+
/** Accept identifiers with any prefix, or with exactly the one given. */
|
|
11
|
+
export interface PrefixOptions<P extends string = string> {
|
|
12
|
+
/** The prefix every identifier must carry, compared exactly, case included. Omit to accept any. */
|
|
13
|
+
readonly prefix?: P;
|
|
14
|
+
readonly entity?: undefined;
|
|
15
|
+
readonly registry?: undefined;
|
|
16
|
+
/** Replaces the default issue message. See {@link IdoraIdErrorMessage}. */
|
|
17
|
+
readonly error?: IdoraIdErrorMessage;
|
|
18
|
+
}
|
|
19
|
+
/** Accept identifiers with the prefix a registered entity type was given. */
|
|
20
|
+
export interface EntityOptions {
|
|
21
|
+
/** The entity type whose identifiers are accepted. */
|
|
22
|
+
readonly entity: EntityType;
|
|
23
|
+
/** The generator, or its registry, that `entity` is registered with. */
|
|
24
|
+
readonly registry: Idora | EntityPrefixRegistry;
|
|
25
|
+
readonly prefix?: undefined;
|
|
26
|
+
/** Replaces the default issue message. See {@link IdoraIdErrorMessage}. */
|
|
27
|
+
readonly error?: IdoraIdErrorMessage;
|
|
28
|
+
}
|
|
29
|
+
/** Which identifiers a schema accepts, and what it says about the rest. */
|
|
30
|
+
export type IdoraIdSchemaOptions<P extends string = string> = PrefixOptions<P> | EntityOptions;
|
|
31
|
+
/**
|
|
32
|
+
* The `params` of the issue a rejected identifier raises, so that code handling a `ZodError` can
|
|
33
|
+
* tell why without matching on the message.
|
|
34
|
+
*/
|
|
35
|
+
export interface IdoraIdIssueParams {
|
|
36
|
+
/** Which check the identifier failed — see `InvalidIdError` in `idora.js`. */
|
|
37
|
+
readonly reason: InvalidIdReason;
|
|
38
|
+
/** The prefix the schema expected, when it expected one. */
|
|
39
|
+
readonly expectedPrefix: string | undefined;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A string schema that accepts Idora identifiers, optionally with one particular prefix, and types
|
|
43
|
+
* what it accepts as {@link Id}.
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { idoraId } from 'idora.js-zod';
|
|
47
|
+
* import * as z from 'zod';
|
|
48
|
+
*
|
|
49
|
+
* const Params = z.object({
|
|
50
|
+
* userId: idoraId({ prefix: 'usr' }), // Id<'usr'>
|
|
51
|
+
* orderId: idoraId({ entity: Order, registry: ids }), // the prefix Order was registered with
|
|
52
|
+
* ref: idoraId(), // any well-formed identifier
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* The output is the input string itself — parsing is strict, so a valid identifier is never
|
|
57
|
+
* rewritten — branded as `Id<P>` so that the type checker knows it has been checked. Use
|
|
58
|
+
* {@link parsedIdoraId} to decode into an `IdoraId` instead.
|
|
59
|
+
*
|
|
60
|
+
* A rejected identifier raises one `custom` issue whose message says what is wrong, and whose
|
|
61
|
+
* `params` carry the {@link IdoraIdIssueParams | reason}. Anything that is not a string is rejected
|
|
62
|
+
* by `z.string()` as usual.
|
|
63
|
+
*
|
|
64
|
+
* @throws {EntityPrefixNotRegisteredError} `entity` is not registered with `registry`. Thrown when
|
|
65
|
+
* the schema is built, so the mistake surfaces at start-up rather than on the first request.
|
|
66
|
+
* @throws {InvalidEntityPrefixError} `prefix` is not itself a valid prefix.
|
|
67
|
+
* @throws {TypeError} The options are contradictory, such as an `entity` without a `registry`.
|
|
68
|
+
*/
|
|
69
|
+
export declare function idoraId<P extends string = string>(options?: IdoraIdSchemaOptions<P>): z.ZodType<Id<P>, string>;
|
|
70
|
+
/**
|
|
71
|
+
* A codec that accepts the same identifiers as {@link idoraId} and decodes them into parsed
|
|
72
|
+
* `IdoraId` objects — and, with `z.encode`, turns `IdoraId` objects back into strings.
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* import { parsedIdoraId } from 'idora.js-zod';
|
|
76
|
+
*
|
|
77
|
+
* const Query = z.object({ after: parsedIdoraId({ prefix: 'evt' }) });
|
|
78
|
+
*
|
|
79
|
+
* const { after } = Query.parse(request.query);
|
|
80
|
+
* after.createdAt; // when the event was minted
|
|
81
|
+
* z.encode(Query, { after }); // { after: 'evt_01k2…' }
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* @throws {EntityPrefixNotRegisteredError} `entity` is not registered with `registry`.
|
|
85
|
+
* @throws {InvalidEntityPrefixError} `prefix` is not itself a valid prefix.
|
|
86
|
+
* @throws {TypeError} The options are contradictory, such as an `entity` without a `registry`.
|
|
87
|
+
*/
|
|
88
|
+
export declare function parsedIdoraId<P extends string = string>(options?: IdoraIdSchemaOptions<P>): z.ZodCodec<z.ZodType<Id<P>, string>, z.ZodCustom<IdoraId<P>, IdoraId<P>>>;
|
|
89
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,oBAAoB,EAAE,KAAK,UAAU,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,KAAK,eAAe,EAAE,MAAM,UAAU,CAAC;AAChI,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB,gGAAgG;AAChG,MAAM,MAAM,mBAAmB,GAAG,MAAM,GAAG,CAAC,CAAC,KAAK,EAAE,cAAc,KAAK,MAAM,CAAC,CAAC;AAE/E,yEAAyE;AACzE,MAAM,WAAW,aAAa,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;IACtD,mGAAmG;IACnG,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACpB,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC;IAC5B,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC;IAC9B,2EAA2E;IAC3E,QAAQ,CAAC,KAAK,CAAC,EAAE,mBAAmB,CAAC;CACtC;AAED,6EAA6E;AAC7E,MAAM,WAAW,aAAa;IAC5B,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x0qbrn4dzeh5c`.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
import { EntityPrefixRegistry, Idora, IdoraId, InvalidIdError } from 'idora.js';
|
|
7
|
+
import * as z from 'zod';
|
|
8
|
+
/**
|
|
9
|
+
* A string schema that accepts Idora identifiers, optionally with one particular prefix, and types
|
|
10
|
+
* what it accepts as {@link Id}.
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { idoraId } from 'idora.js-zod';
|
|
14
|
+
* import * as z from 'zod';
|
|
15
|
+
*
|
|
16
|
+
* const Params = z.object({
|
|
17
|
+
* userId: idoraId({ prefix: 'usr' }), // Id<'usr'>
|
|
18
|
+
* orderId: idoraId({ entity: Order, registry: ids }), // the prefix Order was registered with
|
|
19
|
+
* ref: idoraId(), // any well-formed identifier
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* The output is the input string itself — parsing is strict, so a valid identifier is never
|
|
24
|
+
* rewritten — branded as `Id<P>` so that the type checker knows it has been checked. Use
|
|
25
|
+
* {@link parsedIdoraId} to decode into an `IdoraId` instead.
|
|
26
|
+
*
|
|
27
|
+
* A rejected identifier raises one `custom` issue whose message says what is wrong, and whose
|
|
28
|
+
* `params` carry the {@link IdoraIdIssueParams | reason}. Anything that is not a string is rejected
|
|
29
|
+
* by `z.string()` as usual.
|
|
30
|
+
*
|
|
31
|
+
* @throws {EntityPrefixNotRegisteredError} `entity` is not registered with `registry`. Thrown when
|
|
32
|
+
* the schema is built, so the mistake surfaces at start-up rather than on the first request.
|
|
33
|
+
* @throws {InvalidEntityPrefixError} `prefix` is not itself a valid prefix.
|
|
34
|
+
* @throws {TypeError} The options are contradictory, such as an `entity` without a `registry`.
|
|
35
|
+
*/
|
|
36
|
+
export function idoraId(options = {}) {
|
|
37
|
+
const expectation = resolve(options);
|
|
38
|
+
const message = options.error;
|
|
39
|
+
// The output is the very string that came in; only its static type changes, so the cast is the
|
|
40
|
+
// whole of the transformation and the schema stays a plain string schema — which keeps it usable
|
|
41
|
+
// in JSON Schema output, and in both directions of a codec.
|
|
42
|
+
return z.string().check((payload) => {
|
|
43
|
+
if (IdoraId.isValid(payload.value, expectation.prefix)) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const error = expectation.explain(payload.value);
|
|
47
|
+
const params = { reason: error.reason, expectedPrefix: error.expectedPrefix };
|
|
48
|
+
payload.issues.push({
|
|
49
|
+
code: 'custom',
|
|
50
|
+
message: message === undefined ? defaultMessage(error) : typeof message === 'string' ? message : message(error),
|
|
51
|
+
input: payload.value,
|
|
52
|
+
params,
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A codec that accepts the same identifiers as {@link idoraId} and decodes them into parsed
|
|
58
|
+
* `IdoraId` objects — and, with `z.encode`, turns `IdoraId` objects back into strings.
|
|
59
|
+
*
|
|
60
|
+
* ```ts
|
|
61
|
+
* import { parsedIdoraId } from 'idora.js-zod';
|
|
62
|
+
*
|
|
63
|
+
* const Query = z.object({ after: parsedIdoraId({ prefix: 'evt' }) });
|
|
64
|
+
*
|
|
65
|
+
* const { after } = Query.parse(request.query);
|
|
66
|
+
* after.createdAt; // when the event was minted
|
|
67
|
+
* z.encode(Query, { after }); // { after: 'evt_01k2…' }
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @throws {EntityPrefixNotRegisteredError} `entity` is not registered with `registry`.
|
|
71
|
+
* @throws {InvalidEntityPrefixError} `prefix` is not itself a valid prefix.
|
|
72
|
+
* @throws {TypeError} The options are contradictory, such as an `entity` without a `registry`.
|
|
73
|
+
*/
|
|
74
|
+
export function parsedIdoraId(options = {}) {
|
|
75
|
+
const parsed = z.custom((value) => value instanceof IdoraId, 'Invalid input: expected an IdoraId');
|
|
76
|
+
return z.codec(idoraId(options), parsed, {
|
|
77
|
+
// The input schema has already checked the text, prefix included, so this cannot throw.
|
|
78
|
+
decode: (text) => IdoraId.parse(text),
|
|
79
|
+
// And on the way back out, the input schema checks the text again — so encoding an IdoraId with
|
|
80
|
+
// the wrong prefix fails just as decoding one would.
|
|
81
|
+
encode: (id) => id.text,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function resolve(options) {
|
|
85
|
+
if (options === null || typeof options !== 'object') {
|
|
86
|
+
throw new TypeError(`The options for an identifier schema must be an object, but received ${options === null ? 'null' : typeof options}.`);
|
|
87
|
+
}
|
|
88
|
+
const { prefix, entity, registry } = options;
|
|
89
|
+
if (entity === undefined) {
|
|
90
|
+
if (registry !== undefined) {
|
|
91
|
+
throw new TypeError(`An identifier schema was given a registry but no entity. Name the entity type too, as in idoraId({ entity: User, registry: ids }).`);
|
|
92
|
+
}
|
|
93
|
+
if (prefix !== undefined) {
|
|
94
|
+
// Validates the prefix now, rather than on the first identifier the schema sees.
|
|
95
|
+
IdoraId.isValid('', prefix);
|
|
96
|
+
}
|
|
97
|
+
return { prefix: prefix, explain: (text) => failure(() => IdoraId.parse(text, prefix)) };
|
|
98
|
+
}
|
|
99
|
+
if (prefix !== undefined) {
|
|
100
|
+
throw new TypeError(`An identifier schema was given both a prefix and an entity. Give one: the entity's registered prefix is used when you name the entity.`);
|
|
101
|
+
}
|
|
102
|
+
const prefixes = registry instanceof Idora ? registry.registry : registry;
|
|
103
|
+
if (!(prefixes instanceof EntityPrefixRegistry)) {
|
|
104
|
+
throw new TypeError(`An identifier schema was given an entity but no registry to look its prefix up in. Pass the generator too, as in idoraId({ entity: User, registry: ids }).`);
|
|
105
|
+
}
|
|
106
|
+
const entityType = entity;
|
|
107
|
+
return { prefix: prefixes.getPrefix(entityType), explain: (text) => failure(() => prefixes.parse(entityType, text)) };
|
|
108
|
+
}
|
|
109
|
+
/** Runs a parse already known to fail, and returns the error it throws. */
|
|
110
|
+
function failure(parse) {
|
|
111
|
+
try {
|
|
112
|
+
parse();
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if (error instanceof InvalidIdError) {
|
|
116
|
+
return error;
|
|
117
|
+
}
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
throw new Error('idora.js-zod internal error: an identifier was rejected but parses.');
|
|
121
|
+
}
|
|
122
|
+
/** Short, user-facing messages in Zod's own style, for the reasons an identifier is rejected. */
|
|
123
|
+
function defaultMessage(error) {
|
|
124
|
+
switch (error.reason) {
|
|
125
|
+
case 'noSeparator':
|
|
126
|
+
return `Invalid identifier: expected a prefix and a value separated by '_', such as ${error.expectedPrefix ?? 'usr'}_01k2f9v8ta7m3x0qbrn4dzeh5c`;
|
|
127
|
+
case 'prefix':
|
|
128
|
+
return `Invalid identifier: the prefix before '_' must be 1 to 16 ASCII letters or digits`;
|
|
129
|
+
case 'value':
|
|
130
|
+
return `Invalid identifier: the value after '_' must be a lowercase ULID or UUIDv7`;
|
|
131
|
+
case 'prefixMismatch':
|
|
132
|
+
return `Invalid identifier: expected the prefix '${error.expectedPrefix}', received '${error.text.slice(0, error.text.indexOf('_'))}'`;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,oBAAoB,EAA4B,KAAK,EAAE,OAAO,EAAE,cAAc,EAAwB,MAAM,UAAU,CAAC;AAChI,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAwCzB;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,OAAO,CAA4B,OAAO,GAA4B,EAAE;IACtF,MAAM,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACrC,MAAM,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC;IAE9B,+FAA+F;IAC/F,iGAAiG;IACjG,4DAA4D;IAC5D,OAAO,CAAC,CAAC,MAAM,EAAE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O,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC,EAAE,CAAC;AACxH,CAAC;AAED,2EAA2E;AAC3E,SAAS,OAAO,CAAC,KAAoB;IACnC,IAAI,CAAC;QACH,KAAK,EAAE,CAAC;IACV,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,cAAc,EAAE,CAAC;YACpC,OAAO,KAAK,CAAC;QACf,CAAC;QAED,MAAM,KAAK,CAAC;IACd,CAAC;IAED,MAAM,IAAI,KAAK,CAAC,qEAAqE,CAAC,CAAC;AACzF,CAAC;AAED,iGAAiG;AACjG,SAAS,cAAc,CAAC,KAAqB;IAC3C,QAAQ,KAAK,CAAC,MAAM,EAAE,CAAC;QACrB,KAAK,aAAa;YAChB,OAAO,+EAA+E,KAAK,CAAC,cAAc,IAAI,KAAK,6BAA6B,CAAC;QACnJ,KAAK,QAAQ;YACX,OAAO,mFAAmF,CAAC;QAC7F,KAAK,OAAO;YACV,OAAO,4EAA4E,CAAC;QACtF,KAAK,gBAAgB;YACnB,OAAO,4CAA4C,KAAK,CAAC,cAAc,gBAAgB,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC;IAC3I,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "idora.js-zod",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Zod schemas for Idora identifiers such as usr_01k2f9v8ta7m3x0qbrn4dzeh5c: validate an identifier and its prefix with a precise error, brand it as Id<'usr'>, or decode it into a parsed IdoraId.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"idora",
|
|
7
|
+
"zod",
|
|
8
|
+
"id",
|
|
9
|
+
"identifier",
|
|
10
|
+
"ulid",
|
|
11
|
+
"uuidv7",
|
|
12
|
+
"prefixed-id",
|
|
13
|
+
"validation",
|
|
14
|
+
"schema"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://github.com/SrKleRAimBot/Idora/tree/main/Idora.JS/extensions/zod#readme",
|
|
17
|
+
"bugs": "https://github.com/SrKleRAimBot/Idora/issues",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/SrKleRAimBot/Idora.git",
|
|
21
|
+
"directory": "Idora.JS/extensions/zod"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"author": "Jake Seracino",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"main": "./dist/index.js",
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"!dist/**/*.tsbuildinfo"
|
|
39
|
+
],
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=20"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsc -p tsconfig.json",
|
|
45
|
+
"build:test": "tsc -p tsconfig.test.json",
|
|
46
|
+
"typecheck": "tsc -p tsconfig.test.json --noEmit",
|
|
47
|
+
"test": "npm run build:test && node --test build/test/*.test.js",
|
|
48
|
+
"prepack": "npm run build"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"idora.js": "^1.1.0",
|
|
52
|
+
"zod": "^4.1.0"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@types/node": "22.20.5",
|
|
56
|
+
"idora.js": "file:../..",
|
|
57
|
+
"typescript": "7.0.2",
|
|
58
|
+
"zod": "4.6.7"
|
|
59
|
+
}
|
|
60
|
+
}
|