Skip to content

Commit 527ecee

Browse files
committed
fix: fix NSID validation function
Source code: atcute/packages/lexicons/lexicons/lib/syntax/nsid.ts at b93fbbf94fec2081b827ae1e366dd59ef6a2116d · mary-ext/atcute - https://github.com/mary-ext/atcute/blob/b93fbbf94fec2081b827ae1e366dd59ef6a2116d/packages/lexicons/lexicons/lib/syntax/nsid.ts
1 parent ef6ea0b commit 527ecee

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Source code: atcute/packages/lexicons/lexicons/lib/syntax/nsid.ts at b93fbbf94fec2081b827ae1e366dd59ef6a2116d · mary-ext/atcute
2+
// - https://github.com/mary-ext/atcute/blob/b93fbbf94fec2081b827ae1e366dd59ef6a2116d/packages/lexicons/lexicons/lib/syntax/nsid.ts
3+
4+
/**
5+
* represents a namespace identifier (NSID)
6+
*/
7+
export type Nsid = `${string}.${string}.${string}`;
8+
9+
const NSID_RE =
10+
/^[a-zA-Z](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+\.[a-zA-Z][a-zA-Z0-9]{0,62}?$/;
11+
12+
// #__NO_SIDE_EFFECTS__
13+
export const isNsid = (input: unknown): input is Nsid => {
14+
return (
15+
typeof input === 'string' &&
16+
input.length >= 5 &&
17+
input.length <= 317 &&
18+
NSID_RE.test(input)
19+
);
20+
};

packages/lex-cli/src/generator/schema.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as t from 'typanion';
2+
import { isNsid } from './nsid';
23

34
const isPositiveInteger = t.cascade(t.isNumber(), (value): value is number => {
45
return Number.isInteger(value) && value >= 0;
@@ -368,9 +369,7 @@ export const userTypeSchema = t.isOneOf([
368369

369370
export type UserTypeSchema = t.InferType<typeof userTypeSchema>;
370371

371-
const NSID_RE =
372-
/^[a-z](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+\.[a-z]{1,63}$/i;
373-
const nsidType = t.cascade(t.isString(), (value) => NSID_RE.test(value));
372+
const nsidType = t.cascade(t.isString(), (value) => isNsid(value));
374373

375374
export const documentSchema = t.cascade(
376375
t.isObject({

0 commit comments

Comments
 (0)