@atproto/lexicon 0.6.2 → 0.7.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/CHANGELOG.md +18 -0
- package/dist/blob-refs.d.ts +4 -4
- package/dist/blob-refs.js +20 -45
- package/dist/blob-refs.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -20
- package/dist/index.js.map +1 -1
- package/dist/lexicons.d.ts +1 -1
- package/dist/lexicons.d.ts.map +1 -1
- package/dist/lexicons.js +40 -54
- package/dist/lexicons.js.map +1 -1
- package/dist/serialize.js +22 -31
- package/dist/serialize.js.map +1 -1
- package/dist/types.js +193 -203
- package/dist/types.js.map +1 -1
- package/dist/util.js +6 -10
- package/dist/util.js.map +1 -1
- package/dist/validation.d.ts +2 -2
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +10 -17
- package/dist/validation.js.map +1 -1
- package/dist/validators/blob.d.ts +2 -2
- package/dist/validators/blob.d.ts.map +1 -1
- package/dist/validators/blob.js +5 -8
- package/dist/validators/blob.js.map +1 -1
- package/dist/validators/complex.d.ts +2 -2
- package/dist/validators/complex.d.ts.map +1 -1
- package/dist/validators/complex.js +20 -26
- package/dist/validators/complex.js.map +1 -1
- package/dist/validators/formats.d.ts +1 -1
- package/dist/validators/formats.d.ts.map +1 -1
- package/dist/validators/formats.js +38 -50
- package/dist/validators/formats.js.map +1 -1
- package/dist/validators/primitives.d.ts +2 -2
- package/dist/validators/primitives.d.ts.map +1 -1
- package/dist/validators/primitives.js +33 -69
- package/dist/validators/primitives.js.map +1 -1
- package/dist/validators/xrpc.d.ts +2 -2
- package/dist/validators/xrpc.d.ts.map +1 -1
- package/dist/validators/xrpc.js +6 -42
- package/dist/validators/xrpc.js.map +1 -1
- package/jest.config.cjs +21 -0
- package/package.json +17 -9
- package/src/index.ts +4 -4
- package/src/lexicons.ts +4 -4
- package/src/serialize.ts +1 -1
- package/src/types.ts +1 -1
- package/src/validation.ts +4 -4
- package/src/validators/blob.ts +3 -3
- package/src/validators/complex.ts +5 -5
- package/src/validators/formats.ts +3 -2
- package/src/validators/primitives.ts +3 -3
- package/src/validators/xrpc.ts +8 -4
- package/tests/_scaffolds/lexicons.ts +1 -1
- package/tests/general.test.ts +5 -5
- package/tsconfig.build.tsbuildinfo +1 -1
- package/jest.config.js +0 -7
|
@@ -1,81 +1,69 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
exports.atIdentifier = atIdentifier;
|
|
9
|
-
exports.nsid = nsid;
|
|
10
|
-
exports.cid = cid;
|
|
11
|
-
exports.language = language;
|
|
12
|
-
exports.tid = tid;
|
|
13
|
-
exports.recordKey = recordKey;
|
|
14
|
-
const iso_datestring_validator_1 = require("iso-datestring-validator");
|
|
15
|
-
const cid_1 = require("multiformats/cid");
|
|
16
|
-
const common_web_1 = require("@atproto/common-web");
|
|
17
|
-
const syntax_1 = require("@atproto/syntax");
|
|
18
|
-
const types_1 = require("../types");
|
|
19
|
-
function datetime(path, value) {
|
|
1
|
+
import isoDatestringValidator from 'iso-datestring-validator';
|
|
2
|
+
const { isValidISODateString } = isoDatestringValidator;
|
|
3
|
+
import { CID } from 'multiformats/cid';
|
|
4
|
+
import { validateLanguage } from '@atproto/common-web';
|
|
5
|
+
import { ensureValidAtUri, ensureValidDid, ensureValidHandle, ensureValidRecordKey, isValidNsid, isValidTid, isValidUri, } from '@atproto/syntax';
|
|
6
|
+
import { ValidationError } from '../types.js';
|
|
7
|
+
export function datetime(path, value) {
|
|
20
8
|
try {
|
|
21
|
-
if (!
|
|
9
|
+
if (!isValidISODateString(value)) {
|
|
22
10
|
throw new Error();
|
|
23
11
|
}
|
|
24
12
|
}
|
|
25
13
|
catch {
|
|
26
14
|
return {
|
|
27
15
|
success: false,
|
|
28
|
-
error: new
|
|
16
|
+
error: new ValidationError(`${path} must be an valid atproto datetime (both RFC-3339 and ISO-8601)`),
|
|
29
17
|
};
|
|
30
18
|
}
|
|
31
19
|
return { success: true, value };
|
|
32
20
|
}
|
|
33
|
-
function uri(path, value) {
|
|
34
|
-
if (!
|
|
21
|
+
export function uri(path, value) {
|
|
22
|
+
if (!isValidUri(value)) {
|
|
35
23
|
return {
|
|
36
24
|
success: false,
|
|
37
|
-
error: new
|
|
25
|
+
error: new ValidationError(`${path} must be a uri`),
|
|
38
26
|
};
|
|
39
27
|
}
|
|
40
28
|
return { success: true, value };
|
|
41
29
|
}
|
|
42
|
-
function atUri(path, value) {
|
|
30
|
+
export function atUri(path, value) {
|
|
43
31
|
try {
|
|
44
|
-
|
|
32
|
+
ensureValidAtUri(value);
|
|
45
33
|
}
|
|
46
34
|
catch {
|
|
47
35
|
return {
|
|
48
36
|
success: false,
|
|
49
|
-
error: new
|
|
37
|
+
error: new ValidationError(`${path} must be a valid at-uri`),
|
|
50
38
|
};
|
|
51
39
|
}
|
|
52
40
|
return { success: true, value };
|
|
53
41
|
}
|
|
54
|
-
function did(path, value) {
|
|
42
|
+
export function did(path, value) {
|
|
55
43
|
try {
|
|
56
|
-
|
|
44
|
+
ensureValidDid(value);
|
|
57
45
|
}
|
|
58
46
|
catch {
|
|
59
47
|
return {
|
|
60
48
|
success: false,
|
|
61
|
-
error: new
|
|
49
|
+
error: new ValidationError(`${path} must be a valid did`),
|
|
62
50
|
};
|
|
63
51
|
}
|
|
64
52
|
return { success: true, value };
|
|
65
53
|
}
|
|
66
|
-
function handle(path, value) {
|
|
54
|
+
export function handle(path, value) {
|
|
67
55
|
try {
|
|
68
|
-
|
|
56
|
+
ensureValidHandle(value);
|
|
69
57
|
}
|
|
70
58
|
catch {
|
|
71
59
|
return {
|
|
72
60
|
success: false,
|
|
73
|
-
error: new
|
|
61
|
+
error: new ValidationError(`${path} must be a valid handle`),
|
|
74
62
|
};
|
|
75
63
|
}
|
|
76
64
|
return { success: true, value };
|
|
77
65
|
}
|
|
78
|
-
function atIdentifier(path, value) {
|
|
66
|
+
export function atIdentifier(path, value) {
|
|
79
67
|
// We can discriminate based on the "did:" prefix
|
|
80
68
|
if (value.startsWith('did:')) {
|
|
81
69
|
const didResult = did(path, value);
|
|
@@ -89,11 +77,11 @@ function atIdentifier(path, value) {
|
|
|
89
77
|
}
|
|
90
78
|
return {
|
|
91
79
|
success: false,
|
|
92
|
-
error: new
|
|
80
|
+
error: new ValidationError(`${path} must be a valid did or a handle`),
|
|
93
81
|
};
|
|
94
82
|
}
|
|
95
|
-
function nsid(path, value) {
|
|
96
|
-
if (
|
|
83
|
+
export function nsid(path, value) {
|
|
84
|
+
if (isValidNsid(value)) {
|
|
97
85
|
return {
|
|
98
86
|
success: true,
|
|
99
87
|
value,
|
|
@@ -102,49 +90,49 @@ function nsid(path, value) {
|
|
|
102
90
|
else {
|
|
103
91
|
return {
|
|
104
92
|
success: false,
|
|
105
|
-
error: new
|
|
93
|
+
error: new ValidationError(`${path} must be a valid nsid`),
|
|
106
94
|
};
|
|
107
95
|
}
|
|
108
96
|
}
|
|
109
|
-
function cid(path, value) {
|
|
97
|
+
export function cid(path, value) {
|
|
110
98
|
try {
|
|
111
|
-
|
|
99
|
+
CID.parse(value);
|
|
112
100
|
}
|
|
113
101
|
catch {
|
|
114
102
|
return {
|
|
115
103
|
success: false,
|
|
116
|
-
error: new
|
|
104
|
+
error: new ValidationError(`${path} must be a cid string`),
|
|
117
105
|
};
|
|
118
106
|
}
|
|
119
107
|
return { success: true, value };
|
|
120
108
|
}
|
|
121
109
|
// The language format validates well-formed BCP 47 language tags: https://www.rfc-editor.org/info/bcp47
|
|
122
|
-
function language(path, value) {
|
|
123
|
-
if (
|
|
110
|
+
export function language(path, value) {
|
|
111
|
+
if (validateLanguage(value)) {
|
|
124
112
|
return { success: true, value };
|
|
125
113
|
}
|
|
126
114
|
return {
|
|
127
115
|
success: false,
|
|
128
|
-
error: new
|
|
116
|
+
error: new ValidationError(`${path} must be a well-formed BCP 47 language tag`),
|
|
129
117
|
};
|
|
130
118
|
}
|
|
131
|
-
function tid(path, value) {
|
|
132
|
-
if (
|
|
119
|
+
export function tid(path, value) {
|
|
120
|
+
if (isValidTid(value)) {
|
|
133
121
|
return { success: true, value };
|
|
134
122
|
}
|
|
135
123
|
return {
|
|
136
124
|
success: false,
|
|
137
|
-
error: new
|
|
125
|
+
error: new ValidationError(`${path} must be a valid TID`),
|
|
138
126
|
};
|
|
139
127
|
}
|
|
140
|
-
function recordKey(path, value) {
|
|
128
|
+
export function recordKey(path, value) {
|
|
141
129
|
try {
|
|
142
|
-
|
|
130
|
+
ensureValidRecordKey(value);
|
|
143
131
|
}
|
|
144
132
|
catch {
|
|
145
133
|
return {
|
|
146
134
|
success: false,
|
|
147
|
-
error: new
|
|
135
|
+
error: new ValidationError(`${path} must be a valid Record Key`),
|
|
148
136
|
};
|
|
149
137
|
}
|
|
150
138
|
return { success: true, value };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formats.js","sourceRoot":"","sources":["../../src/validators/formats.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"formats.js","sourceRoot":"","sources":["../../src/validators/formats.ts"],"names":[],"mappings":"AAAA,OAAO,sBAAsB,MAAM,0BAA0B,CAAA;AAC7D,MAAM,EAAE,oBAAoB,EAAE,GAAG,sBAAsB,CAAA;AACvD,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AACtD,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,iBAAiB,EACjB,oBAAoB,EACpB,WAAW,EACX,UAAU,EACV,UAAU,GACX,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,eAAe,EAAoB,MAAM,aAAa,CAAA;AAE/D,MAAM,UAAU,QAAQ,CAAC,IAAY,EAAE,KAAa;IAClD,IAAI,CAAC;QACH,IAAI,CAAC,oBAAoB,CAAC,KAAK,CAAC,EA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,CAAA;IAClB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO;YACL,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,IAAI,eAAe,CAAC,GAAG,IAAI,uBAAuB,CAAC;SAC3D,CAAA;IACH,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;AACjC,CAAC;AAED,wGAAwG;AACxG,MAAM,UAAU,QAAQ,CAAC,IAAY,EAAE,KAAa;IAClD,IAAI,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;QAC5B,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;IACjC,CAAC;IACD,OAAO;QACL,OAAO,EAAE,KAAK;QACd,KAAK,EAAE,IAAI,eAAe,CACxB,GAAG,IAAI,4CAA4C,CACpD;KACF,CAAA;AACH,CAAC;AAED,MAAM,UAAU,GAAG,CAAC,IAAY,EAAE,KAAa;IAC7C,IAAI,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;QACtB,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;IACjC,CAAC;IAED,OAAO;QACL,OAAO,EAAE,KAAK;QACd,KAAK,EAAE,IAAI,eAAe,CAAC,GAAG,IAAI,sBAAsB,CAAC;KAC1D,CAAA;AACH,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,IAAY,EAAE,KAAa;IACnD,IAAI,CAAC;QACH,oBAAoB,CAAC,KAAK,CAAC,CAAA;IAC7B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO;YACL,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,IAAI,eAAe,CAAC,GAAG,IAAI,6BAA6B,CAAC;SACjE,CAAA;IACH,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;AACjC,CAAC","sourcesContent":["import isoDatestringValidator from 'iso-datestring-validator'\nconst { isValidISODateString } = isoDatestringValidator\nimport { CID } from 'multiformats/cid'\nimport { validateLanguage } from '@atproto/common-web'\nimport {\n ensureValidAtUri,\n ensureValidDid,\n ensureValidHandle,\n ensureValidRecordKey,\n isValidNsid,\n isValidTid,\n isValidUri,\n} from '@atproto/syntax'\nimport { ValidationError, ValidationResult } from '../types.js'\n\nexport function datetime(path: string, value: string): ValidationResult {\n try {\n if (!isValidISODateString(value)) {\n throw new Error()\n }\n } catch {\n return {\n success: false,\n error: new ValidationError(\n `${path} must be an valid atproto datetime (both RFC-3339 and ISO-8601)`,\n ),\n }\n }\n return { success: true, value }\n}\n\nexport function uri(path: string, value: string): ValidationResult {\n if (!isValidUri(value)) {\n return {\n success: false,\n error: new ValidationError(`${path} must be a uri`),\n }\n }\n return { success: true, value }\n}\n\nexport function atUri(path: string, value: string): ValidationResult {\n try {\n ensureValidAtUri(value)\n } catch {\n return {\n success: false,\n error: new ValidationError(`${path} must be a valid at-uri`),\n }\n }\n\n return { success: true, value }\n}\n\nexport function did(path: string, value: string): ValidationResult {\n try {\n ensureValidDid(value)\n } catch {\n return {\n success: false,\n error: new ValidationError(`${path} must be a valid did`),\n }\n }\n\n return { success: true, value }\n}\n\nexport function handle(path: string, value: string): ValidationResult {\n try {\n ensureValidHandle(value)\n } catch {\n return {\n success: false,\n error: new ValidationError(`${path} must be a valid handle`),\n }\n }\n\n return { success: true, value }\n}\n\nexport function atIdentifier(path: string, value: string): ValidationResult {\n // We can discriminate based on the \"did:\" prefix\n if (value.startsWith('did:')) {\n const didResult = did(path, value)\n if (didResult.success) return didResult\n } else {\n const handleResult = handle(path, value)\n if (handleResult.success) return handleResult\n }\n\n return {\n success: false,\n error: new ValidationError(`${path} must be a valid did or a handle`),\n }\n}\n\nexport function nsid(path: string, value: string): ValidationResult {\n if (isValidNsid(value)) {\n return {\n success: true,\n value,\n }\n } else {\n return {\n success: false,\n error: new ValidationError(`${path} must be a valid nsid`),\n }\n }\n}\n\nexport function cid(path: string, value: string): ValidationResult {\n try {\n CID.parse(value)\n } catch {\n return {\n success: false,\n error: new ValidationError(`${path} must be a cid string`),\n }\n }\n return { success: true, value }\n}\n\n// The language format validates well-formed BCP 47 language tags: https://www.rfc-editor.org/info/bcp47\nexport function language(path: string, value: string): ValidationResult {\n if (validateLanguage(value)) {\n return { success: true, value }\n }\n return {\n success: false,\n error: new ValidationError(\n `${path} must be a well-formed BCP 47 language tag`,\n ),\n }\n}\n\nexport function tid(path: string, value: string): ValidationResult {\n if (isValidTid(value)) {\n return { success: true, value }\n }\n\n return {\n success: false,\n error: new ValidationError(`${path} must be a valid TID`),\n }\n}\n\nexport function recordKey(path: string, value: string): ValidationResult {\n try {\n ensureValidRecordKey(value)\n } catch {\n return {\n success: false,\n error: new ValidationError(`${path} must be a valid Record Key`),\n }\n }\n return { success: true, value }\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Lexicons } from '../lexicons';
|
|
2
|
-
import { LexUserType, ValidationResult } from '../types';
|
|
1
|
+
import { Lexicons } from '../lexicons.js';
|
|
2
|
+
import { LexUserType, ValidationResult } from '../types.js';
|
|
3
3
|
export declare function validate(lexicons: Lexicons, path: string, def: LexUserType, value: unknown): ValidationResult;
|
|
4
4
|
//# sourceMappingURL=primitives.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"primitives.d.ts","sourceRoot":"","sources":["../../src/validators/primitives.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"primitives.d.ts","sourceRoot":"","sources":["../../src/validators/primitives.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AACzC,OAAO,EAKL,WAAW,EAEX,gBAAgB,EACjB,MAAM,aAAa,CAAA;AAGpB,wBAAgB,QAAQ,CACtB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,WAAW,EAChB,KAAK,EAAE,OAAO,GACb,gBAAgB,CAoBlB"}
|
|
@@ -1,44 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.validate = validate;
|
|
37
|
-
const cid_1 = require("multiformats/cid");
|
|
38
|
-
const common_web_1 = require("@atproto/common-web");
|
|
39
|
-
const types_1 = require("../types");
|
|
40
|
-
const formats = __importStar(require("./formats"));
|
|
41
|
-
function validate(lexicons, path, def, value) {
|
|
1
|
+
import { CID } from 'multiformats/cid';
|
|
2
|
+
import { graphemeLen, utf8Len } from '@atproto/common-web';
|
|
3
|
+
import { ValidationError, } from '../types.js';
|
|
4
|
+
import * as formats from './formats.js';
|
|
5
|
+
export function validate(lexicons, path, def, value) {
|
|
42
6
|
switch (def.type) {
|
|
43
7
|
case 'boolean':
|
|
44
8
|
return boolean(lexicons, path, def, value);
|
|
@@ -55,7 +19,7 @@ function validate(lexicons, path, def, value) {
|
|
|
55
19
|
default:
|
|
56
20
|
return {
|
|
57
21
|
success: false,
|
|
58
|
-
error: new
|
|
22
|
+
error: new ValidationError(`Unexpected lexicon type: ${def.type}`),
|
|
59
23
|
};
|
|
60
24
|
}
|
|
61
25
|
}
|
|
@@ -69,13 +33,13 @@ function boolean(lexicons, path, def, value) {
|
|
|
69
33
|
}
|
|
70
34
|
return {
|
|
71
35
|
success: false,
|
|
72
|
-
error: new
|
|
36
|
+
error: new ValidationError(`${path} must be a boolean`),
|
|
73
37
|
};
|
|
74
38
|
}
|
|
75
39
|
else if (type !== 'boolean') {
|
|
76
40
|
return {
|
|
77
41
|
success: false,
|
|
78
|
-
error: new
|
|
42
|
+
error: new ValidationError(`${path} must be a boolean`),
|
|
79
43
|
};
|
|
80
44
|
}
|
|
81
45
|
// const
|
|
@@ -83,7 +47,7 @@ function boolean(lexicons, path, def, value) {
|
|
|
83
47
|
if (value !== def.const) {
|
|
84
48
|
return {
|
|
85
49
|
success: false,
|
|
86
|
-
error: new
|
|
50
|
+
error: new ValidationError(`${path} must be ${def.const}`),
|
|
87
51
|
};
|
|
88
52
|
}
|
|
89
53
|
}
|
|
@@ -99,13 +63,13 @@ function integer(lexicons, path, def, value) {
|
|
|
99
63
|
}
|
|
100
64
|
return {
|
|
101
65
|
success: false,
|
|
102
|
-
error: new
|
|
66
|
+
error: new ValidationError(`${path} must be an integer`),
|
|
103
67
|
};
|
|
104
68
|
}
|
|
105
69
|
else if (!Number.isInteger(value)) {
|
|
106
70
|
return {
|
|
107
71
|
success: false,
|
|
108
|
-
error: new
|
|
72
|
+
error: new ValidationError(`${path} must be an integer`),
|
|
109
73
|
};
|
|
110
74
|
}
|
|
111
75
|
// const
|
|
@@ -113,7 +77,7 @@ function integer(lexicons, path, def, value) {
|
|
|
113
77
|
if (value !== def.const) {
|
|
114
78
|
return {
|
|
115
79
|
success: false,
|
|
116
|
-
error: new
|
|
80
|
+
error: new ValidationError(`${path} must be ${def.const}`),
|
|
117
81
|
};
|
|
118
82
|
}
|
|
119
83
|
}
|
|
@@ -122,7 +86,7 @@ function integer(lexicons, path, def, value) {
|
|
|
122
86
|
if (!def.enum.includes(value)) {
|
|
123
87
|
return {
|
|
124
88
|
success: false,
|
|
125
|
-
error: new
|
|
89
|
+
error: new ValidationError(`${path} must be one of (${def.enum.join('|')})`),
|
|
126
90
|
};
|
|
127
91
|
}
|
|
128
92
|
}
|
|
@@ -131,7 +95,7 @@ function integer(lexicons, path, def, value) {
|
|
|
131
95
|
if (value > def.maximum) {
|
|
132
96
|
return {
|
|
133
97
|
success: false,
|
|
134
|
-
error: new
|
|
98
|
+
error: new ValidationError(`${path} can not be greater than ${def.maximum}`),
|
|
135
99
|
};
|
|
136
100
|
}
|
|
137
101
|
}
|
|
@@ -140,7 +104,7 @@ function integer(lexicons, path, def, value) {
|
|
|
140
104
|
if (value < def.minimum) {
|
|
141
105
|
return {
|
|
142
106
|
success: false,
|
|
143
|
-
error: new
|
|
107
|
+
error: new ValidationError(`${path} can not be less than ${def.minimum}`),
|
|
144
108
|
};
|
|
145
109
|
}
|
|
146
110
|
}
|
|
@@ -155,13 +119,13 @@ function string(lexicons, path, def, value) {
|
|
|
155
119
|
}
|
|
156
120
|
return {
|
|
157
121
|
success: false,
|
|
158
|
-
error: new
|
|
122
|
+
error: new ValidationError(`${path} must be a string`),
|
|
159
123
|
};
|
|
160
124
|
}
|
|
161
125
|
else if (typeof value !== 'string') {
|
|
162
126
|
return {
|
|
163
127
|
success: false,
|
|
164
|
-
error: new
|
|
128
|
+
error: new ValidationError(`${path} must be a string`),
|
|
165
129
|
};
|
|
166
130
|
}
|
|
167
131
|
// const
|
|
@@ -169,7 +133,7 @@ function string(lexicons, path, def, value) {
|
|
|
169
133
|
if (value !== def.const) {
|
|
170
134
|
return {
|
|
171
135
|
success: false,
|
|
172
|
-
error: new
|
|
136
|
+
error: new ValidationError(`${path} must be ${def.const}`),
|
|
173
137
|
};
|
|
174
138
|
}
|
|
175
139
|
}
|
|
@@ -178,7 +142,7 @@ function string(lexicons, path, def, value) {
|
|
|
178
142
|
if (!def.enum.includes(value)) {
|
|
179
143
|
return {
|
|
180
144
|
success: false,
|
|
181
|
-
error: new
|
|
145
|
+
error: new ValidationError(`${path} must be one of (${def.enum.join('|')})`),
|
|
182
146
|
};
|
|
183
147
|
}
|
|
184
148
|
}
|
|
@@ -189,7 +153,7 @@ function string(lexicons, path, def, value) {
|
|
|
189
153
|
if (typeof def.minLength === 'number' && value.length * 3 < def.minLength) {
|
|
190
154
|
return {
|
|
191
155
|
success: false,
|
|
192
|
-
error: new
|
|
156
|
+
error: new ValidationError(`${path} must not be shorter than ${def.minLength} characters`),
|
|
193
157
|
};
|
|
194
158
|
}
|
|
195
159
|
// If the JavaScript string length * 3 is within the maximum limit,
|
|
@@ -202,12 +166,12 @@ function string(lexicons, path, def, value) {
|
|
|
202
166
|
canSkipUtf8LenChecks = true;
|
|
203
167
|
}
|
|
204
168
|
if (!canSkipUtf8LenChecks) {
|
|
205
|
-
const len =
|
|
169
|
+
const len = utf8Len(value);
|
|
206
170
|
if (typeof def.maxLength === 'number') {
|
|
207
171
|
if (len > def.maxLength) {
|
|
208
172
|
return {
|
|
209
173
|
success: false,
|
|
210
|
-
error: new
|
|
174
|
+
error: new ValidationError(`${path} must not be longer than ${def.maxLength} characters`),
|
|
211
175
|
};
|
|
212
176
|
}
|
|
213
177
|
}
|
|
@@ -215,7 +179,7 @@ function string(lexicons, path, def, value) {
|
|
|
215
179
|
if (len < def.minLength) {
|
|
216
180
|
return {
|
|
217
181
|
success: false,
|
|
218
|
-
error: new
|
|
182
|
+
error: new ValidationError(`${path} must not be shorter than ${def.minLength} characters`),
|
|
219
183
|
};
|
|
220
184
|
}
|
|
221
185
|
}
|
|
@@ -243,7 +207,7 @@ function string(lexicons, path, def, value) {
|
|
|
243
207
|
// Fail early.
|
|
244
208
|
return {
|
|
245
209
|
success: false,
|
|
246
|
-
error: new
|
|
210
|
+
error: new ValidationError(`${path} must not be shorter than ${def.minGraphemes} graphemes`),
|
|
247
211
|
};
|
|
248
212
|
}
|
|
249
213
|
else {
|
|
@@ -251,12 +215,12 @@ function string(lexicons, path, def, value) {
|
|
|
251
215
|
}
|
|
252
216
|
}
|
|
253
217
|
if (needsMaxGraphemesCheck || needsMinGraphemesCheck) {
|
|
254
|
-
const len =
|
|
218
|
+
const len = graphemeLen(value);
|
|
255
219
|
if (typeof def.maxGraphemes === 'number') {
|
|
256
220
|
if (len > def.maxGraphemes) {
|
|
257
221
|
return {
|
|
258
222
|
success: false,
|
|
259
|
-
error: new
|
|
223
|
+
error: new ValidationError(`${path} must not be longer than ${def.maxGraphemes} graphemes`),
|
|
260
224
|
};
|
|
261
225
|
}
|
|
262
226
|
}
|
|
@@ -264,7 +228,7 @@ function string(lexicons, path, def, value) {
|
|
|
264
228
|
if (len < def.minGraphemes) {
|
|
265
229
|
return {
|
|
266
230
|
success: false,
|
|
267
|
-
error: new
|
|
231
|
+
error: new ValidationError(`${path} must not be shorter than ${def.minGraphemes} graphemes`),
|
|
268
232
|
};
|
|
269
233
|
}
|
|
270
234
|
}
|
|
@@ -303,7 +267,7 @@ function bytes(lexicons, path, def, value) {
|
|
|
303
267
|
if (!value || !(value instanceof Uint8Array)) {
|
|
304
268
|
return {
|
|
305
269
|
success: false,
|
|
306
|
-
error: new
|
|
270
|
+
error: new ValidationError(`${path} must be a byte array`),
|
|
307
271
|
};
|
|
308
272
|
}
|
|
309
273
|
// maxLength
|
|
@@ -311,7 +275,7 @@ function bytes(lexicons, path, def, value) {
|
|
|
311
275
|
if (value.byteLength > def.maxLength) {
|
|
312
276
|
return {
|
|
313
277
|
success: false,
|
|
314
|
-
error: new
|
|
278
|
+
error: new ValidationError(`${path} must not be larger than ${def.maxLength} bytes`),
|
|
315
279
|
};
|
|
316
280
|
}
|
|
317
281
|
}
|
|
@@ -320,17 +284,17 @@ function bytes(lexicons, path, def, value) {
|
|
|
320
284
|
if (value.byteLength < def.minLength) {
|
|
321
285
|
return {
|
|
322
286
|
success: false,
|
|
323
|
-
error: new
|
|
287
|
+
error: new ValidationError(`${path} must not be smaller than ${def.minLength} bytes`),
|
|
324
288
|
};
|
|
325
289
|
}
|
|
326
290
|
}
|
|
327
291
|
return { success: true, value };
|
|
328
292
|
}
|
|
329
293
|
function cidLink(lexicons, path, def, value) {
|
|
330
|
-
if (
|
|
294
|
+
if (CID.asCID(value) === null) {
|
|
331
295
|
return {
|
|
332
296
|
success: false,
|
|
333
|
-
error: new
|
|
297
|
+
error: new ValidationError(`${path} must be a CID`),
|
|
334
298
|
};
|
|
335
299
|
}
|
|
336
300
|
return { success: true, value };
|
|
@@ -340,7 +304,7 @@ function unknown(lexicons, path, def, value) {
|
|
|
340
304
|
if (!value || typeof value !== 'object') {
|
|
341
305
|
return {
|
|
342
306
|
success: false,
|
|
343
|
-
error: new
|
|
307
|
+
error: new ValidationError(`${path} must be an object`),
|
|
344
308
|
};
|
|
345
309
|
}
|
|
346
310
|
return { success: true, value };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"primitives.js","sourceRoot":"","sources":["../../src/validators/primitives.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAcA,4BAyBC;AAvCD,0CAAsC;AACtC,oDAA0D;AAE1D,oCAQiB;AACjB,mDAAoC;AAEpC,SAAgB,QAAQ,CACtB,QAAkB,EAClB,IAAY,EACZ,GAAgB,EAChB,KAAc;IAEd,QAAQ,GAAG,CAAC,IAAI,EAAE,CAAC;QACjB,KAAK,SAAS;YACZ,OAAO,OAAO,CAAC,QAAQ,EAAE,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;AAED,SAAS,OAAO,CACd,QAAkB,EAClB,IAAY,EACZ,GAAgB,EAChB,KAAc;IAEd,IAAI,SAAG,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC;QAC9B,OAAO;YACL,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,IAAI,uBAAe,CAAC,GAAG,IAAI,gBAAgB,CAAC;SACpD,CAAA;IACH,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;AACjC,CAAC;AAED,SAAS,OAAO,CACd,QAAkB,EAClB,IAAY,EACZ,GAAgB,EAChB,KAAc;IAEd,OAAO;IACP,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACxC,OAAO;YACL,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,IAAI,uBAAe,CAAC,GAAG,IAAI,oBAAoB,CAAC;SACxD,CAAA;IACH,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;AACjC,CAAC","sourcesContent":["import { CID } from 'multiformats/cid'\nimport { graphemeLen, utf8Len } from '@atproto/common-web'\nimport { Lexicons } from '../lexicons'\nimport {\n LexBoolean,\n LexBytes,\n LexInteger,\n LexString,\n LexUserType,\n ValidationError,\n ValidationResult,\n} from '../types'\nimport * as formats from './formats'\n\nexport function validate(\n lexicons: Lexicons,\n path: string,\n def: LexUserType,\n value: unknown,\n): ValidationResult {\n switch (def.type) {\n case 'boolean':\n return boolean(lexicons, path, def, value)\n case 'integer':\n return integer(lexicons, path, def, value)\n case 'string':\n return string(lexicons, path, def, value)\n case 'bytes':\n return bytes(lexicons, path, def, value)\n case 'cid-link':\n return cidLink(lexicons, path, def, value)\n case 'unknown':\n return unknown(lexicons, path, def, value)\n default:\n return {\n success: false,\n error: new ValidationError(`Unexpected lexicon type: ${def.type}`),\n }\n }\n}\n\nfunction boolean(\n lexicons: Lexicons,\n path: string,\n def: LexUserType,\n value: unknown,\n): ValidationResult {\n def = def as LexBoolean\n\n // type\n const type = typeof value\n if (type === 'undefined') {\n if (typeof def.default === 'boolean') {\n return { success: true, value: def.default }\n }\n return {\n success: false,\n error: new ValidationError(`${path} must be a boolean`),\n }\n } else if (type !== 'boolean') {\n return {\n success: false,\n error: new ValidationError(`${path} must be a boolean`),\n }\n }\n\n // const\n if (typeof def.const === 'boolean') {\n if (value !== def.const) {\n return {\n success: false,\n error: new ValidationError(`${path} must be ${def.const}`),\n }\n }\n }\n\n return { success: true, value }\n}\n\nfunction integer(\n lexicons: Lexicons,\n path: string,\n def: LexUserType,\n value: unknown,\n): ValidationResult {\n def = def as LexInteger\n\n // type\n const type = typeof value\n if (type === 'undefined') {\n if (typeof def.default === 'number') {\n return { success: true, value: def.default }\n }\n return {\n success: false,\n error: new ValidationError(`${path} must be an integer`),\n }\n } else if (!Number.isInteger(value)) {\n return {\n success: false,\n error: new ValidationError(`${path} must be an integer`),\n }\n }\n\n // const\n if (typeof def.const === 'number') {\n if (value !== def.const) {\n return {\n success: false,\n error: new ValidationError(`${path} must be ${def.const}`),\n }\n }\n }\n\n // enum\n if (Array.isArray(def.enum)) {\n if (!def.enum.includes(value as number)) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must be one of (${def.enum.join('|')})`,\n ),\n }\n }\n }\n\n // maximum\n if (typeof def.maximum === 'number') {\n if ((value as number) > def.maximum) {\n return {\n success: false,\n error: new ValidationError(\n `${path} can not be greater than ${def.maximum}`,\n ),\n }\n }\n }\n\n // minimum\n if (typeof def.minimum === 'number') {\n if ((value as number) < def.minimum) {\n return {\n success: false,\n error: new ValidationError(\n `${path} can not be less than ${def.minimum}`,\n ),\n }\n }\n }\n\n return { success: true, value }\n}\n\nfunction string(\n lexicons: Lexicons,\n path: string,\n def: LexUserType,\n value: unknown,\n): ValidationResult {\n def = def as LexString\n\n // type\n if (typeof value === 'undefined') {\n if (typeof def.default === 'string') {\n return { success: true, value: def.default }\n }\n return {\n success: false,\n error: new ValidationError(`${path} must be a string`),\n }\n } else if (typeof value !== 'string') {\n return {\n success: false,\n error: new ValidationError(`${path} must be a string`),\n }\n }\n\n // const\n if (typeof def.const === 'string') {\n if (value !== def.const) {\n return {\n success: false,\n error: new ValidationError(`${path} must be ${def.const}`),\n }\n }\n }\n\n // enum\n if (Array.isArray(def.enum)) {\n if (!def.enum.includes(value as string)) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must be one of (${def.enum.join('|')})`,\n ),\n }\n }\n }\n\n // maxLength and minLength\n if (typeof def.minLength === 'number' || typeof def.maxLength === 'number') {\n // If the JavaScript string length * 3 is below the maximum limit,\n // its UTF8 length (which <= .length * 3) will also be below.\n if (typeof def.minLength === 'number' && value.length * 3 < def.minLength) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be shorter than ${def.minLength} characters`,\n ),\n }\n }\n\n // If the JavaScript string length * 3 is within the maximum limit,\n // its UTF8 length (which <= .length * 3) will also be within.\n // When there's no minimal length, this lets us skip the UTF8 length check.\n let canSkipUtf8LenChecks = false\n if (\n typeof def.minLength === 'undefined' &&\n typeof def.maxLength === 'number' &&\n value.length * 3 <= def.maxLength\n ) {\n canSkipUtf8LenChecks = true\n }\n\n if (!canSkipUtf8LenChecks) {\n const len = utf8Len(value)\n\n if (typeof def.maxLength === 'number') {\n if (len > def.maxLength) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be longer than ${def.maxLength} characters`,\n ),\n }\n }\n }\n\n if (typeof def.minLength === 'number') {\n if (len < def.minLength) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be shorter than ${def.minLength} characters`,\n ),\n }\n }\n }\n }\n }\n\n // maxGraphemes and minGraphemes\n if (\n typeof def.maxGraphemes === 'number' ||\n typeof def.minGraphemes === 'number'\n ) {\n let needsMaxGraphemesCheck = false\n let needsMinGraphemesCheck = false\n\n if (typeof def.maxGraphemes === 'number') {\n if (value.length <= def.maxGraphemes) {\n // If the JavaScript string length (UTF-16) is within the maximum limit,\n // its grapheme length (which <= .length) will also be within.\n needsMaxGraphemesCheck = false\n } else {\n needsMaxGraphemesCheck = true\n }\n }\n\n if (typeof def.minGraphemes === 'number') {\n if (value.length < def.minGraphemes) {\n // If the JavaScript string length (UTF-16) is below the minimal limit,\n // its grapheme length (which <= .length) will also be below.\n // Fail early.\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be shorter than ${def.minGraphemes} graphemes`,\n ),\n }\n } else {\n needsMinGraphemesCheck = true\n }\n }\n\n if (needsMaxGraphemesCheck || needsMinGraphemesCheck) {\n const len = graphemeLen(value)\n\n if (typeof def.maxGraphemes === 'number') {\n if (len > def.maxGraphemes) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be longer than ${def.maxGraphemes} graphemes`,\n ),\n }\n }\n }\n\n if (typeof def.minGraphemes === 'number') {\n if (len < def.minGraphemes) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be shorter than ${def.minGraphemes} graphemes`,\n ),\n }\n }\n }\n }\n }\n\n if (typeof def.format === 'string') {\n switch (def.format) {\n case 'datetime':\n return formats.datetime(path, value)\n case 'uri':\n return formats.uri(path, value)\n case 'at-uri':\n return formats.atUri(path, value)\n case 'did':\n return formats.did(path, value)\n case 'handle':\n return formats.handle(path, value)\n case 'at-identifier':\n return formats.atIdentifier(path, value)\n case 'nsid':\n return formats.nsid(path, value)\n case 'cid':\n return formats.cid(path, value)\n case 'language':\n return formats.language(path, value)\n case 'tid':\n return formats.tid(path, value)\n case 'record-key':\n return formats.recordKey(path, value)\n }\n }\n\n return { success: true, value }\n}\n\nfunction bytes(\n lexicons: Lexicons,\n path: string,\n def: LexUserType,\n value: unknown,\n): ValidationResult {\n def = def as LexBytes\n\n if (!value || !(value instanceof Uint8Array)) {\n return {\n success: false,\n error: new ValidationError(`${path} must be a byte array`),\n }\n }\n\n // maxLength\n if (typeof def.maxLength === 'number') {\n if (value.byteLength > def.maxLength) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be larger than ${def.maxLength} bytes`,\n ),\n }\n }\n }\n\n // minLength\n if (typeof def.minLength === 'number') {\n if (value.byteLength < def.minLength) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be smaller than ${def.minLength} bytes`,\n ),\n }\n }\n }\n\n return { success: true, value }\n}\n\nfunction cidLink(\n lexicons: Lexicons,\n path: string,\n def: LexUserType,\n value: unknown,\n): ValidationResult {\n if (CID.asCID(value) === null) {\n return {\n success: false,\n error: new ValidationError(`${path} must be a CID`),\n }\n }\n\n return { success: true, value }\n}\n\nfunction unknown(\n lexicons: Lexicons,\n path: string,\n def: LexUserType,\n value: unknown,\n): ValidationResult {\n // type\n if (!value || typeof value !== 'object') {\n return {\n success: false,\n error: new ValidationError(`${path} must be an object`),\n }\n }\n\n return { success: true, value }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"primitives.js","sourceRoot":"","sources":["../../src/validators/primitives.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAE1D,OAAO,EAML,eAAe,GAEhB,MAAM,aAAa,CAAA;AACpB,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AAEvC,MAAM,UAAU,QAAQ,CACtB,QAAkB,EAClB,IAAY,EACZ,GAAgB,EAChB,KAAc;IAEd,QAAQ,GAAG,CAAC,IAAI,EAAE,CAAC;QACjB,KAAK,SAAS;YACZ,OAAO,OAAO,CAAC,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;QAC5C,KAAK,SAAS;YACZ,OAAO,OAAO,CAAC,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;QAC5C,KAAK,QAAQ;YACX,OAAO,MAAM,CAAC,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;QAC3C,KAAK,OAAO;YACV,OAAO,KAAK,CAAC,Q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CAAC;AAED,SAAS,OAAO,CACd,QAAkB,EAClB,IAAY,EACZ,GAAgB,EAChB,KAAc;IAEd,OAAO;IACP,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACxC,OAAO;YACL,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,IAAI,eAAe,CAAC,GAAG,IAAI,oBAAoB,CAAC;SACxD,CAAA;IACH,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;AACjC,CAAC","sourcesContent":["import { CID } from 'multiformats/cid'\nimport { graphemeLen, utf8Len } from '@atproto/common-web'\nimport { Lexicons } from '../lexicons.js'\nimport {\n LexBoolean,\n LexBytes,\n LexInteger,\n LexString,\n LexUserType,\n ValidationError,\n ValidationResult,\n} from '../types.js'\nimport * as formats from './formats.js'\n\nexport function validate(\n lexicons: Lexicons,\n path: string,\n def: LexUserType,\n value: unknown,\n): ValidationResult {\n switch (def.type) {\n case 'boolean':\n return boolean(lexicons, path, def, value)\n case 'integer':\n return integer(lexicons, path, def, value)\n case 'string':\n return string(lexicons, path, def, value)\n case 'bytes':\n return bytes(lexicons, path, def, value)\n case 'cid-link':\n return cidLink(lexicons, path, def, value)\n case 'unknown':\n return unknown(lexicons, path, def, value)\n default:\n return {\n success: false,\n error: new ValidationError(`Unexpected lexicon type: ${def.type}`),\n }\n }\n}\n\nfunction boolean(\n lexicons: Lexicons,\n path: string,\n def: LexUserType,\n value: unknown,\n): ValidationResult {\n def = def as LexBoolean\n\n // type\n const type = typeof value\n if (type === 'undefined') {\n if (typeof def.default === 'boolean') {\n return { success: true, value: def.default }\n }\n return {\n success: false,\n error: new ValidationError(`${path} must be a boolean`),\n }\n } else if (type !== 'boolean') {\n return {\n success: false,\n error: new ValidationError(`${path} must be a boolean`),\n }\n }\n\n // const\n if (typeof def.const === 'boolean') {\n if (value !== def.const) {\n return {\n success: false,\n error: new ValidationError(`${path} must be ${def.const}`),\n }\n }\n }\n\n return { success: true, value }\n}\n\nfunction integer(\n lexicons: Lexicons,\n path: string,\n def: LexUserType,\n value: unknown,\n): ValidationResult {\n def = def as LexInteger\n\n // type\n const type = typeof value\n if (type === 'undefined') {\n if (typeof def.default === 'number') {\n return { success: true, value: def.default }\n }\n return {\n success: false,\n error: new ValidationError(`${path} must be an integer`),\n }\n } else if (!Number.isInteger(value)) {\n return {\n success: false,\n error: new ValidationError(`${path} must be an integer`),\n }\n }\n\n // const\n if (typeof def.const === 'number') {\n if (value !== def.const) {\n return {\n success: false,\n error: new ValidationError(`${path} must be ${def.const}`),\n }\n }\n }\n\n // enum\n if (Array.isArray(def.enum)) {\n if (!def.enum.includes(value as number)) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must be one of (${def.enum.join('|')})`,\n ),\n }\n }\n }\n\n // maximum\n if (typeof def.maximum === 'number') {\n if ((value as number) > def.maximum) {\n return {\n success: false,\n error: new ValidationError(\n `${path} can not be greater than ${def.maximum}`,\n ),\n }\n }\n }\n\n // minimum\n if (typeof def.minimum === 'number') {\n if ((value as number) < def.minimum) {\n return {\n success: false,\n error: new ValidationError(\n `${path} can not be less than ${def.minimum}`,\n ),\n }\n }\n }\n\n return { success: true, value }\n}\n\nfunction string(\n lexicons: Lexicons,\n path: string,\n def: LexUserType,\n value: unknown,\n): ValidationResult {\n def = def as LexString\n\n // type\n if (typeof value === 'undefined') {\n if (typeof def.default === 'string') {\n return { success: true, value: def.default }\n }\n return {\n success: false,\n error: new ValidationError(`${path} must be a string`),\n }\n } else if (typeof value !== 'string') {\n return {\n success: false,\n error: new ValidationError(`${path} must be a string`),\n }\n }\n\n // const\n if (typeof def.const === 'string') {\n if (value !== def.const) {\n return {\n success: false,\n error: new ValidationError(`${path} must be ${def.const}`),\n }\n }\n }\n\n // enum\n if (Array.isArray(def.enum)) {\n if (!def.enum.includes(value as string)) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must be one of (${def.enum.join('|')})`,\n ),\n }\n }\n }\n\n // maxLength and minLength\n if (typeof def.minLength === 'number' || typeof def.maxLength === 'number') {\n // If the JavaScript string length * 3 is below the maximum limit,\n // its UTF8 length (which <= .length * 3) will also be below.\n if (typeof def.minLength === 'number' && value.length * 3 < def.minLength) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be shorter than ${def.minLength} characters`,\n ),\n }\n }\n\n // If the JavaScript string length * 3 is within the maximum limit,\n // its UTF8 length (which <= .length * 3) will also be within.\n // When there's no minimal length, this lets us skip the UTF8 length check.\n let canSkipUtf8LenChecks = false\n if (\n typeof def.minLength === 'undefined' &&\n typeof def.maxLength === 'number' &&\n value.length * 3 <= def.maxLength\n ) {\n canSkipUtf8LenChecks = true\n }\n\n if (!canSkipUtf8LenChecks) {\n const len = utf8Len(value)\n\n if (typeof def.maxLength === 'number') {\n if (len > def.maxLength) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be longer than ${def.maxLength} characters`,\n ),\n }\n }\n }\n\n if (typeof def.minLength === 'number') {\n if (len < def.minLength) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be shorter than ${def.minLength} characters`,\n ),\n }\n }\n }\n }\n }\n\n // maxGraphemes and minGraphemes\n if (\n typeof def.maxGraphemes === 'number' ||\n typeof def.minGraphemes === 'number'\n ) {\n let needsMaxGraphemesCheck = false\n let needsMinGraphemesCheck = false\n\n if (typeof def.maxGraphemes === 'number') {\n if (value.length <= def.maxGraphemes) {\n // If the JavaScript string length (UTF-16) is within the maximum limit,\n // its grapheme length (which <= .length) will also be within.\n needsMaxGraphemesCheck = false\n } else {\n needsMaxGraphemesCheck = true\n }\n }\n\n if (typeof def.minGraphemes === 'number') {\n if (value.length < def.minGraphemes) {\n // If the JavaScript string length (UTF-16) is below the minimal limit,\n // its grapheme length (which <= .length) will also be below.\n // Fail early.\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be shorter than ${def.minGraphemes} graphemes`,\n ),\n }\n } else {\n needsMinGraphemesCheck = true\n }\n }\n\n if (needsMaxGraphemesCheck || needsMinGraphemesCheck) {\n const len = graphemeLen(value)\n\n if (typeof def.maxGraphemes === 'number') {\n if (len > def.maxGraphemes) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be longer than ${def.maxGraphemes} graphemes`,\n ),\n }\n }\n }\n\n if (typeof def.minGraphemes === 'number') {\n if (len < def.minGraphemes) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be shorter than ${def.minGraphemes} graphemes`,\n ),\n }\n }\n }\n }\n }\n\n if (typeof def.format === 'string') {\n switch (def.format) {\n case 'datetime':\n return formats.datetime(path, value)\n case 'uri':\n return formats.uri(path, value)\n case 'at-uri':\n return formats.atUri(path, value)\n case 'did':\n return formats.did(path, value)\n case 'handle':\n return formats.handle(path, value)\n case 'at-identifier':\n return formats.atIdentifier(path, value)\n case 'nsid':\n return formats.nsid(path, value)\n case 'cid':\n return formats.cid(path, value)\n case 'language':\n return formats.language(path, value)\n case 'tid':\n return formats.tid(path, value)\n case 'record-key':\n return formats.recordKey(path, value)\n }\n }\n\n return { success: true, value }\n}\n\nfunction bytes(\n lexicons: Lexicons,\n path: string,\n def: LexUserType,\n value: unknown,\n): ValidationResult {\n def = def as LexBytes\n\n if (!value || !(value instanceof Uint8Array)) {\n return {\n success: false,\n error: new ValidationError(`${path} must be a byte array`),\n }\n }\n\n // maxLength\n if (typeof def.maxLength === 'number') {\n if (value.byteLength > def.maxLength) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be larger than ${def.maxLength} bytes`,\n ),\n }\n }\n }\n\n // minLength\n if (typeof def.minLength === 'number') {\n if (value.byteLength < def.minLength) {\n return {\n success: false,\n error: new ValidationError(\n `${path} must not be smaller than ${def.minLength} bytes`,\n ),\n }\n }\n }\n\n return { success: true, value }\n}\n\nfunction cidLink(\n lexicons: Lexicons,\n path: string,\n def: LexUserType,\n value: unknown,\n): ValidationResult {\n if (CID.asCID(value) === null) {\n return {\n success: false,\n error: new ValidationError(`${path} must be a CID`),\n }\n }\n\n return { success: true, value }\n}\n\nfunction unknown(\n lexicons: Lexicons,\n path: string,\n def: LexUserType,\n value: unknown,\n): ValidationResult {\n // type\n if (!value || typeof value !== 'object') {\n return {\n success: false,\n error: new ValidationError(`${path} must be an object`),\n }\n }\n\n return { success: true, value }\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Lexicons } from '../lexicons';
|
|
2
|
-
import { LexXrpcParameters, ValidationResult } from '../types';
|
|
1
|
+
import { Lexicons } from '../lexicons.js';
|
|
2
|
+
import { LexXrpcParameters, ValidationResult } from '../types.js';
|
|
3
3
|
export declare function params(lexicons: Lexicons, path: string, def: LexXrpcParameters, val: unknown): ValidationResult<Record<string, unknown>>;
|
|
4
4
|
//# sourceMappingURL=xrpc.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"xrpc.d.ts","sourceRoot":"","sources":["../../src/validators/xrpc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"xrpc.d.ts","sourceRoot":"","sources":["../../src/validators/xrpc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AACzC,OAAO,EACL,iBAAiB,EAEjB,gBAAgB,EACjB,MAAM,aAAa,CAAA;AAIpB,wBAAgB,MAAM,CACpB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,iBAAiB,EACtB,GAAG,EAAE,OAAO,GACX,gBAAgB,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAsC3C"}
|