@atproto/lex-schema 0.0.11 → 0.0.12
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 +19 -0
- package/dist/core/$type.d.ts +149 -0
- package/dist/core/$type.d.ts.map +1 -1
- package/dist/core/$type.js +44 -0
- package/dist/core/$type.js.map +1 -1
- package/dist/core/record-key.d.ts +44 -0
- package/dist/core/record-key.d.ts.map +1 -1
- package/dist/core/record-key.js +30 -0
- package/dist/core/record-key.js.map +1 -1
- package/dist/core/result.d.ts +85 -4
- package/dist/core/result.d.ts.map +1 -1
- package/dist/core/result.js +60 -4
- package/dist/core/result.js.map +1 -1
- package/dist/core/schema.d.ts +229 -2
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +197 -4
- package/dist/core/schema.js.map +1 -1
- package/dist/core/string-format.d.ts +244 -11
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +150 -0
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/types.d.ts +90 -3
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/validation-error.d.ts +60 -0
- package/dist/core/validation-error.d.ts.map +1 -1
- package/dist/core/validation-error.js +60 -0
- package/dist/core/validation-error.js.map +1 -1
- package/dist/core/validation-issue.d.ts +61 -0
- package/dist/core/validation-issue.d.ts.map +1 -1
- package/dist/core/validation-issue.js +51 -0
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +347 -10
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +184 -3
- package/dist/core/validator.js.map +1 -1
- package/dist/helpers.d.ts +9 -24
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js.map +1 -1
- package/dist/schema/array.d.ts +45 -0
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +14 -0
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +46 -0
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +39 -0
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +28 -0
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +28 -0
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +38 -0
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +32 -0
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +38 -0
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +33 -0
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +66 -1
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +54 -0
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +44 -0
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +44 -0
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +58 -0
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +45 -0
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +48 -0
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +48 -0
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +42 -0
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +36 -0
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +54 -0
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +49 -0
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/literal.d.ts +44 -0
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +44 -0
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +42 -0
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +42 -0
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +29 -0
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +29 -0
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +41 -0
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +41 -0
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +56 -0
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +51 -0
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +42 -0
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +42 -0
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +90 -10
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +84 -10
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +111 -15
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js +70 -0
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/permission-set.d.ts +58 -0
- package/dist/schema/permission-set.d.ts.map +1 -1
- package/dist/schema/permission-set.js +50 -0
- package/dist/schema/permission-set.js.map +1 -1
- package/dist/schema/permission.d.ts +42 -0
- package/dist/schema/permission.d.ts.map +1 -1
- package/dist/schema/permission.js +39 -0
- package/dist/schema/permission.js.map +1 -1
- package/dist/schema/procedure.d.ts +64 -0
- package/dist/schema/procedure.d.ts.map +1 -1
- package/dist/schema/procedure.js +64 -0
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.d.ts +55 -0
- package/dist/schema/query.d.ts.map +1 -1
- package/dist/schema/query.js +55 -0
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.d.ts +63 -8
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +20 -0
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +50 -0
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +17 -0
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +58 -9
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.d.ts +44 -0
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +44 -0
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +50 -0
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +41 -0
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +72 -2
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js +59 -0
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.d.ts +47 -0
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +47 -0
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +62 -8
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +18 -0
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +53 -0
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +15 -0
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +50 -1
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +50 -1
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +45 -0
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +40 -0
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown-object.d.ts +34 -0
- package/dist/schema/unknown-object.d.ts.map +1 -1
- package/dist/schema/unknown-object.js +31 -0
- package/dist/schema/unknown-object.js.map +1 -1
- package/dist/schema/unknown.d.ts +33 -0
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +33 -0
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +44 -0
- package/dist/schema/with-default.d.ts.map +1 -1
- package/dist/schema/with-default.js +44 -0
- package/dist/schema/with-default.js.map +1 -1
- package/package.json +3 -3
- package/src/core/$type.ts +150 -18
- package/src/core/record-key.ts +44 -0
- package/src/core/result.ts +86 -4
- package/src/core/schema.ts +236 -7
- package/src/core/string-format.ts +259 -13
- package/src/core/types.ts +91 -3
- package/src/core/validation-error.ts +60 -0
- package/src/core/validation-issue.ts +65 -0
- package/src/core/validator.ts +351 -10
- package/src/helpers.test.ts +110 -29
- package/src/helpers.ts +9 -14
- package/src/schema/array.test.ts +94 -79
- package/src/schema/array.ts +45 -0
- package/src/schema/blob.ts +46 -0
- package/src/schema/boolean.ts +28 -0
- package/src/schema/bytes.ts +38 -0
- package/src/schema/cid.ts +38 -0
- package/src/schema/custom.ts +66 -1
- package/src/schema/dict.ts +44 -0
- package/src/schema/discriminated-union.ts +58 -0
- package/src/schema/enum.ts +48 -0
- package/src/schema/integer.ts +42 -0
- package/src/schema/intersection.ts +54 -0
- package/src/schema/literal.ts +44 -0
- package/src/schema/never.ts +42 -0
- package/src/schema/null.ts +29 -0
- package/src/schema/nullable.ts +41 -0
- package/src/schema/object.ts +56 -0
- package/src/schema/optional.ts +42 -0
- package/src/schema/params.test.ts +58 -2
- package/src/schema/params.ts +125 -19
- package/src/schema/payload.test.ts +3 -3
- package/src/schema/payload.ts +142 -38
- package/src/schema/permission-set.ts +58 -0
- package/src/schema/permission.ts +42 -0
- package/src/schema/procedure.ts +64 -0
- package/src/schema/query.ts +55 -0
- package/src/schema/record.ts +63 -8
- package/src/schema/ref.ts +50 -0
- package/src/schema/refine.ts +58 -9
- package/src/schema/regexp.ts +44 -0
- package/src/schema/string.ts +50 -0
- package/src/schema/subscription.ts +72 -2
- package/src/schema/token.ts +47 -0
- package/src/schema/typed-object.ts +62 -8
- package/src/schema/typed-ref.ts +53 -0
- package/src/schema/typed-union.ts +55 -2
- package/src/schema/union.ts +45 -0
- package/src/schema/unknown-object.ts +34 -0
- package/src/schema/unknown.ts +33 -0
- package/src/schema/with-default.ts +44 -0
package/dist/schema/blob.js
CHANGED
|
@@ -4,6 +4,21 @@ exports.blob = exports.BlobSchema = void 0;
|
|
|
4
4
|
const lex_data_1 = require("@atproto/lex-data");
|
|
5
5
|
const core_js_1 = require("../core.js");
|
|
6
6
|
const memoize_js_1 = require("../util/memoize.js");
|
|
7
|
+
/**
|
|
8
|
+
* Schema for validating blob references in AT Protocol.
|
|
9
|
+
*
|
|
10
|
+
* Validates BlobRef objects which contain a CID reference to binary data,
|
|
11
|
+
* along with metadata like MIME type and size. Can optionally accept
|
|
12
|
+
* legacy blob reference format.
|
|
13
|
+
*
|
|
14
|
+
* @template TOptions - The configuration options type
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const schema = new BlobSchema({ accept: ['image/*'], maxSize: 1000000 })
|
|
19
|
+
* const result = schema.validate(blobRef)
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
7
22
|
class BlobSchema extends core_js_1.Schema {
|
|
8
23
|
options;
|
|
9
24
|
constructor(options) {
|
|
@@ -51,6 +66,30 @@ function matchesMime(mime, accepted) {
|
|
|
51
66
|
}
|
|
52
67
|
return false;
|
|
53
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Creates a blob schema for validating blob references with optional constraints.
|
|
71
|
+
*
|
|
72
|
+
* Blob references are used in AT Protocol to reference binary data stored
|
|
73
|
+
* separately from records. They contain a CID, MIME type, and size information.
|
|
74
|
+
*
|
|
75
|
+
* @param options - Optional configuration for MIME type filtering and size limits
|
|
76
|
+
* @returns A new {@link BlobSchema} instance
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* // Basic blob reference
|
|
81
|
+
* const fileSchema = l.blob()
|
|
82
|
+
*
|
|
83
|
+
* // Image files only
|
|
84
|
+
* const imageSchema = l.blob({ accept: ['image/png', 'image/jpeg', 'image/gif'] })
|
|
85
|
+
*
|
|
86
|
+
* // Any image type with size limit
|
|
87
|
+
* const avatarSchema = l.blob({ accept: ['image/*'], maxSize: 1000000 })
|
|
88
|
+
*
|
|
89
|
+
* // Allow legacy format
|
|
90
|
+
* const legacySchema = l.blob({ allowLegacy: true })
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
54
93
|
exports.blob = (0, memoize_js_1.memoizedOptions)(function (options) {
|
|
55
94
|
return new BlobSchema(options);
|
|
56
95
|
});
|
package/dist/schema/blob.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"blob.js","sourceRoot":"","sources":["../../src/schema/blob.ts"],"names":[],"mappings":";;;AAAA,gDAM0B;AAC1B,wCAAsD;AACtD,mDAAoD;
|
|
1
|
+
{"version":3,"file":"blob.js","sourceRoot":"","sources":["../../src/schema/blob.ts"],"names":[],"mappings":";;;AAAA,gDAM0B;AAC1B,wCAAsD;AACtD,mDAAoD;AA2BpD;;;;;;;;;;;;;;GAcG;AACH,MAAa,UAEX,SAAQ,gBAET;IACsB;IAArB,YAAqB,OAAkB;QACrC,KAAK,EAAE,CAAA;QADY,YAAO,GAAP,OAAO,CAAW;IAEvC,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,MAAM,IAAI,GACP,KAAa,EAAE,KAAK,KAAK,SAAS;YACjC,CAAC,CAAC,IAAA,oBAAS,EAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC;gBAC9B,CAAC,CAAC,KAAK;gBACP,CAAC,CAAC,IAAI;YACR,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,WAAW,KAAK,IAAI,IAAI,IAAA,0BAAe,EAAC,KAAK,CAAC;gBAC5D,CAAC,CAAC,KAAK;gBACP,CAAC,CAAC,IAAI,CAAA;QAEZ,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;QAC5C,CAAC;QAED,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,EAAE,MAAM,CAAA;QACnC,IAAI,MAAM,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC,EAAE,CAAC;YAClD,OAAO,GAAG,CAAC,yBAAyB,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,CAAC,CAAA;QAChE,CAAC;QAED,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,EAAE,OAAO,CAAA;QACrC,IAAI,OAAO,IAAI,IAAI,IAAI,MAAM,IAAI,IAAI,IAAI,IAAI,CAAC,IAAI,GAAG,OAAO,EAAE,CAAC;YAC7D,OAAO,GAAG,CAAC,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;QAC1D,CAAC;QAED,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IAC1B,CAAC;IAED,WAAW,CAAC,IAAY;QACtB,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,EAAE,MAAM,CAAA;QACnC,IAAI,CAAC,MAAM;YAAE,OAAO,IAAI,CAAA;QACxB,OAAO,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IAClC,CAAC;CACF;AAzCD,gCAyCC;AAED,SAAS,WAAW,CAAC,IAAY,EAAE,QAAkB;IACnD,IAAI,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAA;IACzC,IAAI,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAA;IACxC,KAAK,MAAM,KAAK,IAAI,QAAQ,EAAE,CAAC;QAC7B,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YAChE,OAAO,IAAI,CAAA;QACb,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACU,QAAA,IAAI,GAAiB,IAAA,4BAAe,EAAC,UAEhD,OAAW;IACX,OAAO,IAAI,UAAU,CAAC,OAAO,CAAC,CAAA;AAChC,CAAC,CAAC,CAAA","sourcesContent":["import {\n BlobRef,\n BlobRefCheckOptions,\n LegacyBlobRef,\n isBlobRef,\n isLegacyBlobRef,\n} from '@atproto/lex-data'\nimport { Schema, ValidationContext } from '../core.js'\nimport { memoizedOptions } from '../util/memoize.js'\n\n/**\n * Configuration options for blob schema validation.\n *\n * @property allowLegacy - Whether to allow legacy blob references format\n * @property accept - List of accepted MIME types (supports wildcards like 'image/*' or '*\\/*')\n * @property maxSize - Maximum blob size in bytes\n */\nexport type BlobSchemaOptions = BlobRefCheckOptions & {\n /**\n * Whether to allow legacy blob references format\n * @see {@link LegacyBlobRef}\n */\n allowLegacy?: boolean\n /**\n * List of accepted mime types\n */\n accept?: string[]\n /**\n * Maximum size in bytes\n */\n maxSize?: number\n}\n\nexport type { BlobRef, LegacyBlobRef }\n\n/**\n * Schema for validating blob references in AT Protocol.\n *\n * Validates BlobRef objects which contain a CID reference to binary data,\n * along with metadata like MIME type and size. Can optionally accept\n * legacy blob reference format.\n *\n * @template TOptions - The configuration options type\n *\n * @example\n * ```ts\n * const schema = new BlobSchema({ accept: ['image/*'], maxSize: 1000000 })\n * const result = schema.validate(blobRef)\n * ```\n */\nexport class BlobSchema<\n const TOptions extends BlobSchemaOptions = NonNullable<unknown>,\n> extends Schema<\n TOptions extends { allowLegacy: true } ? BlobRef | LegacyBlobRef : BlobRef\n> {\n constructor(readonly options?: TOptions) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n const blob: null | BlobRef | LegacyBlobRef =\n (input as any)?.$type !== undefined\n ? isBlobRef(input, this.options)\n ? input\n : null\n : this.options?.allowLegacy === true && isLegacyBlobRef(input)\n ? input\n : null\n\n if (!blob) {\n return ctx.issueInvalidType(input, 'blob')\n }\n\n const accept = this.options?.accept\n if (accept && !matchesMime(blob.mimeType, accept)) {\n return ctx.issueInvalidPropertyValue(blob, 'mimeType', accept)\n }\n\n const maxSize = this.options?.maxSize\n if (maxSize != null && 'size' in blob && blob.size > maxSize) {\n return ctx.issueTooBig(blob, 'blob', maxSize, blob.size)\n }\n\n return ctx.success(blob)\n }\n\n matchesMime(mime: string): boolean {\n const accept = this.options?.accept\n if (!accept) return true\n return matchesMime(mime, accept)\n }\n}\n\nfunction matchesMime(mime: string, accepted: string[]): boolean {\n if (accepted.includes('*/*')) return true\n if (accepted.includes(mime)) return true\n for (const value of accepted) {\n if (value.endsWith('/*') && mime.startsWith(value.slice(0, -1))) {\n return true\n }\n }\n return false\n}\n\n/**\n * Creates a blob schema for validating blob references with optional constraints.\n *\n * Blob references are used in AT Protocol to reference binary data stored\n * separately from records. They contain a CID, MIME type, and size information.\n *\n * @param options - Optional configuration for MIME type filtering and size limits\n * @returns A new {@link BlobSchema} instance\n *\n * @example\n * ```ts\n * // Basic blob reference\n * const fileSchema = l.blob()\n *\n * // Image files only\n * const imageSchema = l.blob({ accept: ['image/png', 'image/jpeg', 'image/gif'] })\n *\n * // Any image type with size limit\n * const avatarSchema = l.blob({ accept: ['image/*'], maxSize: 1000000 })\n *\n * // Allow legacy format\n * const legacySchema = l.blob({ allowLegacy: true })\n * ```\n */\nexport const blob = /*#__PURE__*/ memoizedOptions(function <\n O extends BlobSchemaOptions = { allowLegacy?: false },\n>(options?: O) {\n return new BlobSchema(options)\n})\n"]}
|
package/dist/schema/boolean.d.ts
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
import { Schema, ValidationContext } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Schema for validating boolean values.
|
|
4
|
+
*
|
|
5
|
+
* Only accepts JavaScript `true` or `false` values. Does not perform
|
|
6
|
+
* any coercion from strings or numbers.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const schema = new BooleanSchema()
|
|
11
|
+
* schema.validate(true) // success
|
|
12
|
+
* schema.validate(false) // success
|
|
13
|
+
* schema.validate('true') // fails - no string coercion
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
2
16
|
export declare class BooleanSchema extends Schema<boolean> {
|
|
3
17
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<boolean>;
|
|
4
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Creates a boolean schema that validates true/false values.
|
|
21
|
+
*
|
|
22
|
+
* @returns A new {@link BooleanSchema} instance
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const enabledSchema = l.boolean()
|
|
27
|
+
*
|
|
28
|
+
* enabledSchema.parse(true) // true
|
|
29
|
+
* enabledSchema.parse(false) // false
|
|
30
|
+
* enabledSchema.parse('true') // throws - strings not accepted
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
5
33
|
export declare const boolean: () => BooleanSchema;
|
|
6
34
|
//# sourceMappingURL=boolean.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"boolean.d.ts","sourceRoot":"","sources":["../../src/schema/boolean.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAGtD,qBAAa,aAAc,SAAQ,MAAM,CAAC,OAAO,CAAC;IAChD,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAOzD;AAED,eAAO,MAAM,OAAO,qBAElB,CAAA"}
|
|
1
|
+
{"version":3,"file":"boolean.d.ts","sourceRoot":"","sources":["../../src/schema/boolean.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAGtD;;;;;;;;;;;;;GAaG;AACH,qBAAa,aAAc,SAAQ,MAAM,CAAC,OAAO,CAAC;IAChD,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAOzD;AAED;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,OAAO,qBAElB,CAAA"}
|
package/dist/schema/boolean.js
CHANGED
|
@@ -3,6 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.boolean = exports.BooleanSchema = void 0;
|
|
4
4
|
const core_js_1 = require("../core.js");
|
|
5
5
|
const memoize_js_1 = require("../util/memoize.js");
|
|
6
|
+
/**
|
|
7
|
+
* Schema for validating boolean values.
|
|
8
|
+
*
|
|
9
|
+
* Only accepts JavaScript `true` or `false` values. Does not perform
|
|
10
|
+
* any coercion from strings or numbers.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const schema = new BooleanSchema()
|
|
15
|
+
* schema.validate(true) // success
|
|
16
|
+
* schema.validate(false) // success
|
|
17
|
+
* schema.validate('true') // fails - no string coercion
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
6
20
|
class BooleanSchema extends core_js_1.Schema {
|
|
7
21
|
validateInContext(input, ctx) {
|
|
8
22
|
if (typeof input === 'boolean') {
|
|
@@ -12,6 +26,20 @@ class BooleanSchema extends core_js_1.Schema {
|
|
|
12
26
|
}
|
|
13
27
|
}
|
|
14
28
|
exports.BooleanSchema = BooleanSchema;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a boolean schema that validates true/false values.
|
|
31
|
+
*
|
|
32
|
+
* @returns A new {@link BooleanSchema} instance
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* const enabledSchema = l.boolean()
|
|
37
|
+
*
|
|
38
|
+
* enabledSchema.parse(true) // true
|
|
39
|
+
* enabledSchema.parse(false) // false
|
|
40
|
+
* enabledSchema.parse('true') // throws - strings not accepted
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
15
43
|
exports.boolean = (0, memoize_js_1.memoizedOptions)(function () {
|
|
16
44
|
return new BooleanSchema();
|
|
17
45
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"boolean.js","sourceRoot":"","sources":["../../src/schema/boolean.ts"],"names":[],"mappings":";;;AAAA,wCAAsD;AACtD,mDAAoD;AAEpD,MAAa,aAAc,SAAQ,gBAAe;IAChD,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,OAAO,KAAK,KAAK,SAAS,EAAE,CAAC;YAC/B,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAC3B,CAAC;QAED,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;IAC/C,CAAC;CACF;AARD,sCAQC;
|
|
1
|
+
{"version":3,"file":"boolean.js","sourceRoot":"","sources":["../../src/schema/boolean.ts"],"names":[],"mappings":";;;AAAA,wCAAsD;AACtD,mDAAoD;AAEpD;;;;;;;;;;;;;GAaG;AACH,MAAa,aAAc,SAAQ,gBAAe;IAChD,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,OAAO,KAAK,KAAK,SAAS,EAAE,CAAC;YAC/B,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAC3B,CAAC;QAED,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;IAC/C,CAAC;CACF;AARD,sCAQC;AAED;;;;;;;;;;;;;GAaG;AACU,QAAA,OAAO,GAAiB,IAAA,4BAAe,EAAC;IACnD,OAAO,IAAI,aAAa,EAAE,CAAA;AAC5B,CAAC,CAAC,CAAA","sourcesContent":["import { Schema, ValidationContext } from '../core.js'\nimport { memoizedOptions } from '../util/memoize.js'\n\n/**\n * Schema for validating boolean values.\n *\n * Only accepts JavaScript `true` or `false` values. Does not perform\n * any coercion from strings or numbers.\n *\n * @example\n * ```ts\n * const schema = new BooleanSchema()\n * schema.validate(true) // success\n * schema.validate(false) // success\n * schema.validate('true') // fails - no string coercion\n * ```\n */\nexport class BooleanSchema extends Schema<boolean> {\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (typeof input === 'boolean') {\n return ctx.success(input)\n }\n\n return ctx.issueInvalidType(input, 'boolean')\n }\n}\n\n/**\n * Creates a boolean schema that validates true/false values.\n *\n * @returns A new {@link BooleanSchema} instance\n *\n * @example\n * ```ts\n * const enabledSchema = l.boolean()\n *\n * enabledSchema.parse(true) // true\n * enabledSchema.parse(false) // false\n * enabledSchema.parse('true') // throws - strings not accepted\n * ```\n */\nexport const boolean = /*#__PURE__*/ memoizedOptions(function () {\n return new BooleanSchema()\n})\n"]}
|
package/dist/schema/bytes.d.ts
CHANGED
|
@@ -1,12 +1,50 @@
|
|
|
1
1
|
import { Schema, ValidationContext } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Configuration options for bytes schema validation.
|
|
4
|
+
*
|
|
5
|
+
* @property minLength - Minimum length in bytes
|
|
6
|
+
* @property maxLength - Maximum length in bytes
|
|
7
|
+
*/
|
|
2
8
|
export type BytesSchemaOptions = {
|
|
3
9
|
minLength?: number;
|
|
4
10
|
maxLength?: number;
|
|
5
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* Schema for validating binary data as Uint8Array with optional length constraints.
|
|
14
|
+
*
|
|
15
|
+
* In "parse" mode, coerces various binary formats (Buffer, ArrayBuffer, etc.)
|
|
16
|
+
* into Uint8Array. In "validate" mode, only accepts Uint8Array directly.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const schema = new BytesSchema({ maxLength: 1024 })
|
|
21
|
+
* const result = schema.validate(new Uint8Array([1, 2, 3]))
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
6
24
|
export declare class BytesSchema extends Schema<Uint8Array> {
|
|
7
25
|
readonly options: BytesSchemaOptions;
|
|
8
26
|
constructor(options?: BytesSchemaOptions);
|
|
9
27
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<Uint8Array<ArrayBufferLike>>;
|
|
10
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Creates a bytes schema for validating binary data with optional length constraints.
|
|
31
|
+
*
|
|
32
|
+
* Validates Uint8Array values and can coerce other binary formats in parse mode.
|
|
33
|
+
*
|
|
34
|
+
* @param options - Optional configuration for minimum and maximum byte length
|
|
35
|
+
* @returns A new {@link BytesSchema} instance
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* // Basic bytes schema
|
|
40
|
+
* const dataSchema = l.bytes()
|
|
41
|
+
*
|
|
42
|
+
* // With size constraints
|
|
43
|
+
* const avatarSchema = l.bytes({ maxLength: 1000000 }) // 1MB max
|
|
44
|
+
*
|
|
45
|
+
* // With minimum size
|
|
46
|
+
* const hashSchema = l.bytes({ minLength: 32, maxLength: 32 }) // Exactly 32 bytes
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
11
49
|
export declare const bytes: (options?: BytesSchemaOptions) => BytesSchema;
|
|
12
50
|
//# sourceMappingURL=bytes.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../../src/schema/bytes.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAGtD,MAAM,MAAM,kBAAkB,GAAG;IAC/B,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAED,qBAAa,WAAY,SAAQ,MAAM,CAAC,UAAU,CAAC;IACrC,QAAQ,CAAC,OAAO,EAAE,kBAAkB;gBAA3B,OAAO,GAAE,kBAAuB;IAIrD,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAoBzD;AAED,eAAO,MAAM,KAAK,aACN,kBAAkB,gBAG5B,CAAA"}
|
|
1
|
+
{"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../../src/schema/bytes.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAGtD;;;;;GAKG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAED;;;;;;;;;;;GAWG;AACH,qBAAa,WAAY,SAAQ,MAAM,CAAC,UAAU,CAAC;IACrC,QAAQ,CAAC,OAAO,EAAE,kBAAkB;gBAA3B,OAAO,GAAE,kBAAuB;IAIrD,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAoBzD;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,KAAK,aACN,kBAAkB,gBAG5B,CAAA"}
|
package/dist/schema/bytes.js
CHANGED
|
@@ -4,6 +4,18 @@ exports.bytes = exports.BytesSchema = void 0;
|
|
|
4
4
|
const lex_data_1 = require("@atproto/lex-data");
|
|
5
5
|
const core_js_1 = require("../core.js");
|
|
6
6
|
const memoize_js_1 = require("../util/memoize.js");
|
|
7
|
+
/**
|
|
8
|
+
* Schema for validating binary data as Uint8Array with optional length constraints.
|
|
9
|
+
*
|
|
10
|
+
* In "parse" mode, coerces various binary formats (Buffer, ArrayBuffer, etc.)
|
|
11
|
+
* into Uint8Array. In "validate" mode, only accepts Uint8Array directly.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const schema = new BytesSchema({ maxLength: 1024 })
|
|
16
|
+
* const result = schema.validate(new Uint8Array([1, 2, 3]))
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
7
19
|
class BytesSchema extends core_js_1.Schema {
|
|
8
20
|
options;
|
|
9
21
|
constructor(options = {}) {
|
|
@@ -28,6 +40,26 @@ class BytesSchema extends core_js_1.Schema {
|
|
|
28
40
|
}
|
|
29
41
|
}
|
|
30
42
|
exports.BytesSchema = BytesSchema;
|
|
43
|
+
/**
|
|
44
|
+
* Creates a bytes schema for validating binary data with optional length constraints.
|
|
45
|
+
*
|
|
46
|
+
* Validates Uint8Array values and can coerce other binary formats in parse mode.
|
|
47
|
+
*
|
|
48
|
+
* @param options - Optional configuration for minimum and maximum byte length
|
|
49
|
+
* @returns A new {@link BytesSchema} instance
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* // Basic bytes schema
|
|
54
|
+
* const dataSchema = l.bytes()
|
|
55
|
+
*
|
|
56
|
+
* // With size constraints
|
|
57
|
+
* const avatarSchema = l.bytes({ maxLength: 1000000 }) // 1MB max
|
|
58
|
+
*
|
|
59
|
+
* // With minimum size
|
|
60
|
+
* const hashSchema = l.bytes({ minLength: 32, maxLength: 32 }) // Exactly 32 bytes
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
31
63
|
exports.bytes = (0, memoize_js_1.memoizedOptions)(function (options) {
|
|
32
64
|
return new BytesSchema(options);
|
|
33
65
|
});
|
package/dist/schema/bytes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bytes.js","sourceRoot":"","sources":["../../src/schema/bytes.ts"],"names":[],"mappings":";;;AAAA,gDAA8D;AAC9D,wCAAsD;AACtD,mDAAoD;
|
|
1
|
+
{"version":3,"file":"bytes.js","sourceRoot":"","sources":["../../src/schema/bytes.ts"],"names":[],"mappings":";;;AAAA,gDAA8D;AAC9D,wCAAsD;AACtD,mDAAoD;AAapD;;;;;;;;;;;GAWG;AACH,MAAa,WAAY,SAAQ,gBAAkB;IAC5B;IAArB,YAAqB,UAA8B,EAAE;QACnD,KAAK,EAAE,CAAA;QADY,YAAO,GAAP,OAAO,CAAyB;IAErD,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,mEAAmE;QACnE,MAAM,KAAK,GACT,GAAG,CAAC,OAAO,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,IAAA,uBAAY,EAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAA,uBAAY,EAAC,KAAK,CAAC,CAAA;QAC1E,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;QAC7C,CAAC;QAED,MAAM,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC,OAAO,CAAA;QAClC,IAAI,SAAS,IAAI,IAAI,IAAI,KAAK,CAAC,MAAM,GAAG,SAAS,EAAE,CAAC;YAClD,OAAO,GAAG,CAAC,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,CAAA;QACnE,CAAC;QAED,MAAM,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC,OAAO,CAAA;QAClC,IAAI,SAAS,IAAI,IAAI,IAAI,KAAK,CAAC,MAAM,GAAG,SAAS,EAAE,CAAC;YAClD,OAAO,GAAG,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,CAAA;QACjE,CAAC;QAED,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC3B,CAAC;CACF;AAzBD,kCAyBC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACU,QAAA,KAAK,GAAiB,IAAA,4BAAe,EAAC,UACjD,OAA4B;IAE5B,OAAO,IAAI,WAAW,CAAC,OAAO,CAAC,CAAA;AACjC,CAAC,CAAC,CAAA","sourcesContent":["import { asUint8Array, ifUint8Array } from '@atproto/lex-data'\nimport { Schema, ValidationContext } from '../core.js'\nimport { memoizedOptions } from '../util/memoize.js'\n\n/**\n * Configuration options for bytes schema validation.\n *\n * @property minLength - Minimum length in bytes\n * @property maxLength - Maximum length in bytes\n */\nexport type BytesSchemaOptions = {\n minLength?: number\n maxLength?: number\n}\n\n/**\n * Schema for validating binary data as Uint8Array with optional length constraints.\n *\n * In \"parse\" mode, coerces various binary formats (Buffer, ArrayBuffer, etc.)\n * into Uint8Array. In \"validate\" mode, only accepts Uint8Array directly.\n *\n * @example\n * ```ts\n * const schema = new BytesSchema({ maxLength: 1024 })\n * const result = schema.validate(new Uint8Array([1, 2, 3]))\n * ```\n */\nexport class BytesSchema extends Schema<Uint8Array> {\n constructor(readonly options: BytesSchemaOptions = {}) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n // In \"parse\" mode, coerce different binary formats into Uint8Array\n const bytes =\n ctx.options.mode === 'parse' ? asUint8Array(input) : ifUint8Array(input)\n if (!bytes) {\n return ctx.issueInvalidType(input, 'bytes')\n }\n\n const { minLength } = this.options\n if (minLength != null && bytes.length < minLength) {\n return ctx.issueTooSmall(bytes, 'bytes', minLength, bytes.length)\n }\n\n const { maxLength } = this.options\n if (maxLength != null && bytes.length > maxLength) {\n return ctx.issueTooBig(bytes, 'bytes', maxLength, bytes.length)\n }\n\n return ctx.success(bytes)\n }\n}\n\n/**\n * Creates a bytes schema for validating binary data with optional length constraints.\n *\n * Validates Uint8Array values and can coerce other binary formats in parse mode.\n *\n * @param options - Optional configuration for minimum and maximum byte length\n * @returns A new {@link BytesSchema} instance\n *\n * @example\n * ```ts\n * // Basic bytes schema\n * const dataSchema = l.bytes()\n *\n * // With size constraints\n * const avatarSchema = l.bytes({ maxLength: 1000000 }) // 1MB max\n *\n * // With minimum size\n * const hashSchema = l.bytes({ minLength: 32, maxLength: 32 }) // Exactly 32 bytes\n * ```\n */\nexport const bytes = /*#__PURE__*/ memoizedOptions(function (\n options?: BytesSchemaOptions,\n) {\n return new BytesSchema(options)\n})\n"]}
|
package/dist/schema/cid.d.ts
CHANGED
|
@@ -1,7 +1,27 @@
|
|
|
1
1
|
import { CheckCidOptions, Cid, InferCheckedCid } from '@atproto/lex-data';
|
|
2
2
|
import { Schema, ValidationContext } from '../core.js';
|
|
3
3
|
export type { Cid };
|
|
4
|
+
/**
|
|
5
|
+
* Configuration options for CID schema validation.
|
|
6
|
+
*
|
|
7
|
+
* @see CheckCidOptions from @atproto/lex-data for available options
|
|
8
|
+
*/
|
|
4
9
|
export type CidSchemaOptions = CheckCidOptions;
|
|
10
|
+
/**
|
|
11
|
+
* Schema for validating Content Identifiers (CIDs).
|
|
12
|
+
*
|
|
13
|
+
* CIDs are self-describing content-addressed identifiers used in AT Protocol
|
|
14
|
+
* to reference data by its cryptographic hash. This schema validates that
|
|
15
|
+
* the input is a valid CID object.
|
|
16
|
+
*
|
|
17
|
+
* @template TOptions - The configuration options type
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const schema = new CidSchema()
|
|
22
|
+
* const result = schema.validate(someCid)
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
5
25
|
export declare class CidSchema<const TOptions extends CidSchemaOptions = {
|
|
6
26
|
flavor: undefined;
|
|
7
27
|
}> extends Schema<InferCheckedCid<TOptions>> {
|
|
@@ -9,5 +29,23 @@ export declare class CidSchema<const TOptions extends CidSchemaOptions = {
|
|
|
9
29
|
constructor(options?: TOptions | undefined);
|
|
10
30
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<Cid<0 | 1, number, number>>;
|
|
11
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates a CID schema for validating Content Identifiers.
|
|
34
|
+
*
|
|
35
|
+
* CIDs are used throughout AT Protocol to reference content by its hash.
|
|
36
|
+
* This is commonly used for referencing blobs, commits, and other data.
|
|
37
|
+
*
|
|
38
|
+
* @param options - Optional configuration for CID validation
|
|
39
|
+
* @returns A new {@link CidSchema} instance
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* // Basic CID validation
|
|
44
|
+
* const cidSchema = l.cid()
|
|
45
|
+
*
|
|
46
|
+
* // Validate a CID from a blob reference
|
|
47
|
+
* const result = cidSchema.validate(blobRef.ref)
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
12
50
|
export declare const cid: <O extends CidSchemaOptions = {}>(options?: O) => CidSchema<O>;
|
|
13
51
|
//# sourceMappingURL=cid.d.ts.map
|
package/dist/schema/cid.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cid.d.ts","sourceRoot":"","sources":["../../src/schema/cid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,GAAG,EAAE,eAAe,EAAS,MAAM,mBAAmB,CAAA;AAChF,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAGtD,YAAY,EAAE,GAAG,EAAE,CAAA;AAEnB,MAAM,MAAM,gBAAgB,GAAG,eAAe,CAAA;AAE9C,qBAAa,SAAS,CACpB,KAAK,CAAC,QAAQ,SAAS,gBAAgB,GAAG;IAAE,MAAM,EAAE,SAAS,CAAA;CAAE,CAC/D,SAAQ,MAAM,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC;IAC7B,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ;gBAAlB,OAAO,CAAC,EAAE,QAAQ,YAAA;IAIvC,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAOzD;AAED,eAAO,MAAM,GAAG,GACd,CAAC,SAAS,gBAAgB,iBAChB,CAAC,iBAEX,CAAA"}
|
|
1
|
+
{"version":3,"file":"cid.d.ts","sourceRoot":"","sources":["../../src/schema/cid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,GAAG,EAAE,eAAe,EAAS,MAAM,mBAAmB,CAAA;AAChF,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAGtD,YAAY,EAAE,GAAG,EAAE,CAAA;AAEnB;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,eAAe,CAAA;AAE9C;;;;;;;;;;;;;;GAcG;AACH,qBAAa,SAAS,CACpB,KAAK,CAAC,QAAQ,SAAS,gBAAgB,GAAG;IAAE,MAAM,EAAE,SAAS,CAAA;CAAE,CAC/D,SAAQ,MAAM,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC;IAC7B,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ;gBAAlB,OAAO,CAAC,EAAE,QAAQ,YAAA;IAIvC,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAOzD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,GAAG,GACd,CAAC,SAAS,gBAAgB,iBAChB,CAAC,iBAEX,CAAA"}
|
package/dist/schema/cid.js
CHANGED
|
@@ -4,6 +4,21 @@ exports.cid = exports.CidSchema = void 0;
|
|
|
4
4
|
const lex_data_1 = require("@atproto/lex-data");
|
|
5
5
|
const core_js_1 = require("../core.js");
|
|
6
6
|
const memoize_js_1 = require("../util/memoize.js");
|
|
7
|
+
/**
|
|
8
|
+
* Schema for validating Content Identifiers (CIDs).
|
|
9
|
+
*
|
|
10
|
+
* CIDs are self-describing content-addressed identifiers used in AT Protocol
|
|
11
|
+
* to reference data by its cryptographic hash. This schema validates that
|
|
12
|
+
* the input is a valid CID object.
|
|
13
|
+
*
|
|
14
|
+
* @template TOptions - The configuration options type
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const schema = new CidSchema()
|
|
19
|
+
* const result = schema.validate(someCid)
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
7
22
|
class CidSchema extends core_js_1.Schema {
|
|
8
23
|
options;
|
|
9
24
|
constructor(options) {
|
|
@@ -18,6 +33,24 @@ class CidSchema extends core_js_1.Schema {
|
|
|
18
33
|
}
|
|
19
34
|
}
|
|
20
35
|
exports.CidSchema = CidSchema;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a CID schema for validating Content Identifiers.
|
|
38
|
+
*
|
|
39
|
+
* CIDs are used throughout AT Protocol to reference content by its hash.
|
|
40
|
+
* This is commonly used for referencing blobs, commits, and other data.
|
|
41
|
+
*
|
|
42
|
+
* @param options - Optional configuration for CID validation
|
|
43
|
+
* @returns A new {@link CidSchema} instance
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* // Basic CID validation
|
|
48
|
+
* const cidSchema = l.cid()
|
|
49
|
+
*
|
|
50
|
+
* // Validate a CID from a blob reference
|
|
51
|
+
* const result = cidSchema.validate(blobRef.ref)
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
21
54
|
exports.cid = (0, memoize_js_1.memoizedOptions)(function (options) {
|
|
22
55
|
return new CidSchema(options);
|
|
23
56
|
});
|
package/dist/schema/cid.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cid.js","sourceRoot":"","sources":["../../src/schema/cid.ts"],"names":[],"mappings":";;;AAAA,gDAAgF;AAChF,wCAAsD;AACtD,mDAAoD;
|
|
1
|
+
{"version":3,"file":"cid.js","sourceRoot":"","sources":["../../src/schema/cid.ts"],"names":[],"mappings":";;;AAAA,gDAAgF;AAChF,wCAAsD;AACtD,mDAAoD;AAWpD;;;;;;;;;;;;;;GAcG;AACH,MAAa,SAEX,SAAQ,gBAAiC;IACpB;IAArB,YAAqB,OAAkB;QACrC,KAAK,EAAE,CAAA;QADY,YAAO,GAAP,OAAO,CAAW;IAEvC,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,CAAC,IAAA,gBAAK,EAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;YAChC,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;QAC3C,CAAC;QAED,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC3B,CAAC;CACF;AAdD,8BAcC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACU,QAAA,GAAG,GAAiB,IAAA,4BAAe,EAAC,UAE/C,OAAW;IACX,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC,CAAA;AAC/B,CAAC,CAAC,CAAA","sourcesContent":["import { CheckCidOptions, Cid, InferCheckedCid, isCid } from '@atproto/lex-data'\nimport { Schema, ValidationContext } from '../core.js'\nimport { memoizedOptions } from '../util/memoize.js'\n\nexport type { Cid }\n\n/**\n * Configuration options for CID schema validation.\n *\n * @see CheckCidOptions from @atproto/lex-data for available options\n */\nexport type CidSchemaOptions = CheckCidOptions\n\n/**\n * Schema for validating Content Identifiers (CIDs).\n *\n * CIDs are self-describing content-addressed identifiers used in AT Protocol\n * to reference data by its cryptographic hash. This schema validates that\n * the input is a valid CID object.\n *\n * @template TOptions - The configuration options type\n *\n * @example\n * ```ts\n * const schema = new CidSchema()\n * const result = schema.validate(someCid)\n * ```\n */\nexport class CidSchema<\n const TOptions extends CidSchemaOptions = { flavor: undefined },\n> extends Schema<InferCheckedCid<TOptions>> {\n constructor(readonly options?: TOptions) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (!isCid(input, this.options)) {\n return ctx.issueInvalidType(input, 'cid')\n }\n\n return ctx.success(input)\n }\n}\n\n/**\n * Creates a CID schema for validating Content Identifiers.\n *\n * CIDs are used throughout AT Protocol to reference content by its hash.\n * This is commonly used for referencing blobs, commits, and other data.\n *\n * @param options - Optional configuration for CID validation\n * @returns A new {@link CidSchema} instance\n *\n * @example\n * ```ts\n * // Basic CID validation\n * const cidSchema = l.cid()\n *\n * // Validate a CID from a blob reference\n * const result = cidSchema.validate(blobRef.ref)\n * ```\n */\nexport const cid = /*#__PURE__*/ memoizedOptions(function <\n O extends CidSchemaOptions = NonNullable<unknown>,\n>(options?: O) {\n return new CidSchema(options)\n})\n"]}
|
package/dist/schema/custom.d.ts
CHANGED
|
@@ -1,15 +1,80 @@
|
|
|
1
1
|
import { Issue, PropertyKey, Schema, ValidationContext } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Context object provided to custom assertion functions.
|
|
4
|
+
*
|
|
5
|
+
* @property path - Current validation path as an array of property keys
|
|
6
|
+
* @property addIssue - Function to add additional validation issues
|
|
7
|
+
*/
|
|
2
8
|
export type CustomAssertionContext = {
|
|
3
9
|
path: PropertyKey[];
|
|
4
10
|
addIssue(issue: Issue): void;
|
|
5
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* Type guard function for custom schema validation.
|
|
14
|
+
*
|
|
15
|
+
* @template TValue - The type to validate/narrow to
|
|
16
|
+
*/
|
|
6
17
|
export type CustomAssertion<TValue> = (this: null, input: unknown, ctx: CustomAssertionContext) => input is TValue;
|
|
7
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Schema with a custom validation function.
|
|
20
|
+
*
|
|
21
|
+
* Allows defining completely custom validation logic using a type guard
|
|
22
|
+
* assertion function. The function receives the input and validation context,
|
|
23
|
+
* and must return whether the input is valid.
|
|
24
|
+
*
|
|
25
|
+
* @template TValue - The validated output type
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* const schema = new CustomSchema(
|
|
30
|
+
* (input): input is Date => input instanceof Date,
|
|
31
|
+
* 'Expected a Date instance'
|
|
32
|
+
* )
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare class CustomSchema<out TValue = unknown> extends Schema<TValue> {
|
|
8
36
|
private readonly assertion;
|
|
9
37
|
private readonly message;
|
|
10
38
|
private readonly path?;
|
|
11
39
|
constructor(assertion: CustomAssertion<TValue>, message: string, path?: (PropertyKey | readonly PropertyKey[]) | undefined);
|
|
12
40
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<TValue>;
|
|
13
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Creates a custom schema with a user-defined validation function.
|
|
44
|
+
*
|
|
45
|
+
* Use this when the built-in schemas don't cover your validation needs.
|
|
46
|
+
* The assertion function must be a type guard that narrows the input type.
|
|
47
|
+
*
|
|
48
|
+
* @param assertion - Type guard function that validates the input
|
|
49
|
+
* @param message - Error message when validation fails
|
|
50
|
+
* @param path - Optional path to associate with validation errors
|
|
51
|
+
* @returns A new {@link CustomSchema} instance
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* // Validate Date instances
|
|
56
|
+
* const dateSchema = l.custom(
|
|
57
|
+
* (input): input is Date => input instanceof Date && !isNaN(input.getTime()),
|
|
58
|
+
* 'Expected a valid Date'
|
|
59
|
+
* )
|
|
60
|
+
*
|
|
61
|
+
* // Validate specific object shape
|
|
62
|
+
* const pointSchema = l.custom(
|
|
63
|
+
* (input): input is { x: number; y: number } =>
|
|
64
|
+
* typeof input === 'object' &&
|
|
65
|
+
* input !== null &&
|
|
66
|
+
* typeof (input as any).x === 'number' &&
|
|
67
|
+
* typeof (input as any).y === 'number',
|
|
68
|
+
* 'Expected a point with x and y coordinates'
|
|
69
|
+
* )
|
|
70
|
+
*
|
|
71
|
+
* // With custom path
|
|
72
|
+
* const validConfig = l.custom(
|
|
73
|
+
* (input): input is Config => validateConfig(input),
|
|
74
|
+
* 'Invalid configuration',
|
|
75
|
+
* ['config']
|
|
76
|
+
* )
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
14
79
|
export declare function custom<TValue>(assertion: CustomAssertion<TValue>, message: string, path?: PropertyKey | readonly PropertyKey[]): CustomSchema<TValue>;
|
|
15
80
|
//# sourceMappingURL=custom.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom.d.ts","sourceRoot":"","sources":["../../src/schema/custom.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EAEL,WAAW,EACX,MAAM,EACN,iBAAiB,EAClB,MAAM,YAAY,CAAA;AAEnB,MAAM,MAAM,sBAAsB,GAAG;IACnC,IAAI,EAAE,WAAW,EAAE,CAAA;IACnB,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAA;CAC7B,CAAA;AAED,MAAM,MAAM,eAAe,CAAC,MAAM,IAAI,CACpC,IAAI,EAAE,IAAI,EACV,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,sBAAsB,KACxB,KAAK,IAAI,MAAM,CAAA;AAEpB,qBAAa,YAAY,CAAC,
|
|
1
|
+
{"version":3,"file":"custom.d.ts","sourceRoot":"","sources":["../../src/schema/custom.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EAEL,WAAW,EACX,MAAM,EACN,iBAAiB,EAClB,MAAM,YAAY,CAAA;AAEnB;;;;;GAKG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,IAAI,EAAE,WAAW,EAAE,CAAA;IACnB,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAA;CAC7B,CAAA;AAED;;;;GAIG;AACH,MAAM,MAAM,eAAe,CAAC,MAAM,IAAI,CACpC,IAAI,EAAE,IAAI,EACV,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,sBAAsB,KACxB,KAAK,IAAI,MAAM,CAAA;AAEpB;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,YAAY,CAAC,GAAG,CAAC,MAAM,GAAG,OAAO,CAAE,SAAQ,MAAM,CAAC,MAAM,CAAC;IAElE,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC;gBAFL,SAAS,EAAE,eAAe,CAAC,MAAM,CAAC,EAClC,OAAO,EAAE,MAAM,EACf,IAAI,CAAC,GAAE,WAAW,GAAG,SAAS,WAAW,EAAE,aAAA;IAK9D,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAOzD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAEH,wBAAgB,MAAM,CAAC,MAAM,EAC3B,SAAS,EAAE,eAAe,CAAC,MAAM,CAAC,EAClC,OAAO,EAAE,MAAM,EACf,IAAI,CAAC,EAAE,WAAW,GAAG,SAAS,WAAW,EAAE,wBAG5C"}
|
package/dist/schema/custom.js
CHANGED
|
@@ -3,6 +3,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.CustomSchema = void 0;
|
|
4
4
|
exports.custom = custom;
|
|
5
5
|
const core_js_1 = require("../core.js");
|
|
6
|
+
/**
|
|
7
|
+
* Schema with a custom validation function.
|
|
8
|
+
*
|
|
9
|
+
* Allows defining completely custom validation logic using a type guard
|
|
10
|
+
* assertion function. The function receives the input and validation context,
|
|
11
|
+
* and must return whether the input is valid.
|
|
12
|
+
*
|
|
13
|
+
* @template TValue - The validated output type
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const schema = new CustomSchema(
|
|
18
|
+
* (input): input is Date => input instanceof Date,
|
|
19
|
+
* 'Expected a Date instance'
|
|
20
|
+
* )
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
6
23
|
class CustomSchema extends core_js_1.Schema {
|
|
7
24
|
assertion;
|
|
8
25
|
message;
|
|
@@ -22,6 +39,43 @@ class CustomSchema extends core_js_1.Schema {
|
|
|
22
39
|
}
|
|
23
40
|
}
|
|
24
41
|
exports.CustomSchema = CustomSchema;
|
|
42
|
+
/**
|
|
43
|
+
* Creates a custom schema with a user-defined validation function.
|
|
44
|
+
*
|
|
45
|
+
* Use this when the built-in schemas don't cover your validation needs.
|
|
46
|
+
* The assertion function must be a type guard that narrows the input type.
|
|
47
|
+
*
|
|
48
|
+
* @param assertion - Type guard function that validates the input
|
|
49
|
+
* @param message - Error message when validation fails
|
|
50
|
+
* @param path - Optional path to associate with validation errors
|
|
51
|
+
* @returns A new {@link CustomSchema} instance
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* // Validate Date instances
|
|
56
|
+
* const dateSchema = l.custom(
|
|
57
|
+
* (input): input is Date => input instanceof Date && !isNaN(input.getTime()),
|
|
58
|
+
* 'Expected a valid Date'
|
|
59
|
+
* )
|
|
60
|
+
*
|
|
61
|
+
* // Validate specific object shape
|
|
62
|
+
* const pointSchema = l.custom(
|
|
63
|
+
* (input): input is { x: number; y: number } =>
|
|
64
|
+
* typeof input === 'object' &&
|
|
65
|
+
* input !== null &&
|
|
66
|
+
* typeof (input as any).x === 'number' &&
|
|
67
|
+
* typeof (input as any).y === 'number',
|
|
68
|
+
* 'Expected a point with x and y coordinates'
|
|
69
|
+
* )
|
|
70
|
+
*
|
|
71
|
+
* // With custom path
|
|
72
|
+
* const validConfig = l.custom(
|
|
73
|
+
* (input): input is Config => validateConfig(input),
|
|
74
|
+
* 'Invalid configuration',
|
|
75
|
+
* ['config']
|
|
76
|
+
* )
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
25
79
|
/*@__NO_SIDE_EFFECTS__*/
|
|
26
80
|
function custom(assertion, message, path) {
|
|
27
81
|
return new CustomSchema(assertion, message, path);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom.js","sourceRoot":"","sources":["../../src/schema/custom.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"custom.js","sourceRoot":"","sources":["../../src/schema/custom.ts"],"names":[],"mappings":";;;AAuGA,wBAMC;AA7GD,wCAMmB;AAwBnB;;;;;;;;;;;;;;;;GAgBG;AACH,MAAa,YAAmC,SAAQ,gBAAc;IAEjD;IACA;IACA;IAHnB,YACmB,SAAkC,EAClC,OAAe,EACf,IAA2C;QAE5D,KAAK,EAAE,CAAA;QAJU,cAAS,GAAT,SAAS,CAAyB;QAClC,YAAO,GAAP,OAAO,CAAQ;QACf,SAAI,GAAJ,IAAI,CAAuC;IAG9D,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,GAAG,CAAC,EAAE,CAAC;YAC3C,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;YACtC,OAAO,GAAG,CAAC,KAAK,CAAC,IAAI,qBAAW,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAA;QAC9D,CAAC;QACD,OAAO,GAAG,CAAC,OAAO,CAAC,KAAe,CAAC,CAAA;IACrC,CAAC;CACF;AAhBD,oCAgBC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAwB;AACxB,SAAgB,MAAM,CACpB,SAAkC,EAClC,OAAe,EACf,IAA2C;IAE3C,OAAO,IAAI,YAAY,CAAS,SAAS,EAAE,OAAO,EAAE,IAAI,CAAC,CAAA;AAC3D,CAAC","sourcesContent":["import {\n Issue,\n IssueCustom,\n PropertyKey,\n Schema,\n ValidationContext,\n} from '../core.js'\n\n/**\n * Context object provided to custom assertion functions.\n *\n * @property path - Current validation path as an array of property keys\n * @property addIssue - Function to add additional validation issues\n */\nexport type CustomAssertionContext = {\n path: PropertyKey[]\n addIssue(issue: Issue): void\n}\n\n/**\n * Type guard function for custom schema validation.\n *\n * @template TValue - The type to validate/narrow to\n */\nexport type CustomAssertion<TValue> = (\n this: null,\n input: unknown,\n ctx: CustomAssertionContext,\n) => input is TValue\n\n/**\n * Schema with a custom validation function.\n *\n * Allows defining completely custom validation logic using a type guard\n * assertion function. The function receives the input and validation context,\n * and must return whether the input is valid.\n *\n * @template TValue - The validated output type\n *\n * @example\n * ```ts\n * const schema = new CustomSchema(\n * (input): input is Date => input instanceof Date,\n * 'Expected a Date instance'\n * )\n * ```\n */\nexport class CustomSchema<out TValue = unknown> extends Schema<TValue> {\n constructor(\n private readonly assertion: CustomAssertion<TValue>,\n private readonly message: string,\n private readonly path?: PropertyKey | readonly PropertyKey[],\n ) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (!this.assertion.call(null, input, ctx)) {\n const path = ctx.concatPath(this.path)\n return ctx.issue(new IssueCustom(path, input, this.message))\n }\n return ctx.success(input as TValue)\n }\n}\n\n/**\n * Creates a custom schema with a user-defined validation function.\n *\n * Use this when the built-in schemas don't cover your validation needs.\n * The assertion function must be a type guard that narrows the input type.\n *\n * @param assertion - Type guard function that validates the input\n * @param message - Error message when validation fails\n * @param path - Optional path to associate with validation errors\n * @returns A new {@link CustomSchema} instance\n *\n * @example\n * ```ts\n * // Validate Date instances\n * const dateSchema = l.custom(\n * (input): input is Date => input instanceof Date && !isNaN(input.getTime()),\n * 'Expected a valid Date'\n * )\n *\n * // Validate specific object shape\n * const pointSchema = l.custom(\n * (input): input is { x: number; y: number } =>\n * typeof input === 'object' &&\n * input !== null &&\n * typeof (input as any).x === 'number' &&\n * typeof (input as any).y === 'number',\n * 'Expected a point with x and y coordinates'\n * )\n *\n * // With custom path\n * const validConfig = l.custom(\n * (input): input is Config => validateConfig(input),\n * 'Invalid configuration',\n * ['config']\n * )\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function custom<TValue>(\n assertion: CustomAssertion<TValue>,\n message: string,\n path?: PropertyKey | readonly PropertyKey[],\n) {\n return new CustomSchema<TValue>(assertion, message, path)\n}\n"]}
|
package/dist/schema/dict.d.ts
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
import { InferInput, InferOutput, Schema, ValidationContext, Validator } from '../core.js';
|
|
2
2
|
/**
|
|
3
|
+
* Schema for validating dictionary/map-like objects with dynamic keys.
|
|
4
|
+
*
|
|
5
|
+
* Unlike `ObjectSchema` which validates a fixed set of properties, `DictSchema`
|
|
6
|
+
* validates objects where any string key is allowed, with both keys and values
|
|
7
|
+
* validated against their respective schemas.
|
|
8
|
+
*
|
|
3
9
|
* @note There is no dictionary in Lexicon schemas. This is a custom extension
|
|
4
10
|
* to allow map-like objects when using the lex library programmatically (i.e.
|
|
5
11
|
* not code generated from a lexicon schema).
|
|
12
|
+
*
|
|
13
|
+
* @template TKey - The validator type for dictionary keys (must validate strings)
|
|
14
|
+
* @template TValue - The validator type for dictionary values
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const schema = new DictSchema(l.string(), l.integer())
|
|
19
|
+
* const result = schema.validate({ a: 1, b: 2, c: 3 })
|
|
20
|
+
* ```
|
|
6
21
|
*/
|
|
7
22
|
export declare class DictSchema<const TKey extends Validator<string> = any, const TValue extends Validator = any> extends Schema<Record<InferInput<TKey>, InferInput<TValue>>, Record<InferInput<TKey>, InferOutput<TValue>>> {
|
|
8
23
|
readonly keySchema: TKey;
|
|
@@ -14,5 +29,34 @@ export declare class DictSchema<const TKey extends Validator<string> = any, cons
|
|
|
14
29
|
};
|
|
15
30
|
}): import("../core.js").ValidationResult<Record<string, unknown>>;
|
|
16
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates a dictionary schema for validating map-like objects.
|
|
34
|
+
*
|
|
35
|
+
* Validates objects where all keys match the key schema and all values
|
|
36
|
+
* match the value schema. Useful for dynamic key-value mappings.
|
|
37
|
+
*
|
|
38
|
+
* @param key - Schema to validate each key (must be a string validator)
|
|
39
|
+
* @param value - Schema to validate each value
|
|
40
|
+
* @returns A new {@link DictSchema} instance
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* // String to number mapping
|
|
45
|
+
* const scoresSchema = l.dict(l.string(), l.integer())
|
|
46
|
+
* scoresSchema.parse({ alice: 100, bob: 85 })
|
|
47
|
+
*
|
|
48
|
+
* // Constrained keys
|
|
49
|
+
* const langSchema = l.dict(
|
|
50
|
+
* l.string({ minLength: 2, maxLength: 5 }), // Language codes
|
|
51
|
+
* l.string() // Translations
|
|
52
|
+
* )
|
|
53
|
+
*
|
|
54
|
+
* // Complex values
|
|
55
|
+
* const usersById = l.dict(
|
|
56
|
+
* l.string({ format: 'did' }),
|
|
57
|
+
* l.object({ name: l.string(), age: l.integer() })
|
|
58
|
+
* )
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
17
61
|
export declare function dict<const TKey extends Validator<string>, const TValue extends Validator>(key: TKey, value: TValue): DictSchema<TKey, TValue>;
|
|
18
62
|
//# sourceMappingURL=dict.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dict.d.ts","sourceRoot":"","sources":["../../src/schema/dict.ts"],"names":[],"mappings":"AACA,OAAO,EACL,UAAU,EACV,WAAW,EACX,MAAM,EACN,iBAAiB,EACjB,SAAS,EACV,MAAM,YAAY,CAAA;AAEnB
|
|
1
|
+
{"version":3,"file":"dict.d.ts","sourceRoot":"","sources":["../../src/schema/dict.ts"],"names":[],"mappings":"AACA,OAAO,EACL,UAAU,EACV,WAAW,EACX,MAAM,EACN,iBAAiB,EACjB,SAAS,EACV,MAAM,YAAY,CAAA;AAEnB;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,UAAU,CACrB,KAAK,CAAC,IAAI,SAAS,SAAS,CAAC,MAAM,CAAC,GAAG,GAAG,EAC1C,KAAK,CAAC,MAAM,SAAS,SAAS,GAAG,GAAG,CACpC,SAAQ,MAAM,CACd,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC,EAC5C,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,CAC9C;IAEG,QAAQ,CAAC,SAAS,EAAE,IAAI;IACxB,QAAQ,CAAC,WAAW,EAAE,MAAM;gBADnB,SAAS,EAAE,IAAI,EACf,WAAW,EAAE,MAAM;IAK9B,iBAAiB,CACf,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,iBAAiB,EACtB,OAAO,CAAC,EAAE;QAAE,WAAW,CAAC,EAAE;YAAE,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;SAAE,CAAA;KAAE;CAoC1D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,wBAAgB,IAAI,CAClB,KAAK,CAAC,IAAI,SAAS,SAAS,CAAC,MAAM,CAAC,EACpC,KAAK,CAAC,MAAM,SAAS,SAAS,EAC9B,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,4BAEzB"}
|