typebox 1.0.16 → 1.0.17
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/build/type/types/base.d.mts +59 -15
- package/build/type/types/base.mjs +17 -5
- package/package.json +1 -1
|
@@ -1,26 +1,70 @@
|
|
|
1
1
|
import { type TSchema } from './schema.mjs';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
3
|
+
/** The Standard Schema properties. */
|
|
4
|
+
readonly '~standard': StandardSchemaV1.Props<Input, Output>;
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
6
|
+
declare namespace StandardSchemaV1 {
|
|
7
|
+
/** The Standard Schema properties interface. */
|
|
8
|
+
export interface Props<Input = unknown, Output = Input> {
|
|
9
|
+
/** The version number of the standard. */
|
|
10
|
+
readonly version: 1;
|
|
11
|
+
/** The vendor name of the schema library. */
|
|
12
|
+
readonly vendor: string;
|
|
13
|
+
/** Validates unknown input values. */
|
|
14
|
+
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
|
|
15
|
+
/** Inferred types associated with the schema. */
|
|
16
|
+
readonly types?: Types<Input, Output> | undefined;
|
|
17
|
+
}
|
|
18
|
+
/** The result interface of the validate function. */
|
|
19
|
+
export type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
20
|
+
/** The result interface if validation succeeds. */
|
|
21
|
+
export interface SuccessResult<Output> {
|
|
22
|
+
/** The typed output value. */
|
|
23
|
+
readonly value: Output;
|
|
24
|
+
/** The non-existent issues. */
|
|
25
|
+
readonly issues?: undefined;
|
|
26
|
+
}
|
|
27
|
+
/** The result interface if validation fails. */
|
|
28
|
+
export interface FailureResult {
|
|
29
|
+
/** The issues of failed validation. */
|
|
30
|
+
readonly issues: ReadonlyArray<Issue>;
|
|
31
|
+
}
|
|
32
|
+
/** The issue interface of the failure output. */
|
|
33
|
+
export interface Issue {
|
|
34
|
+
/** The error message of the issue. */
|
|
35
|
+
readonly message: string;
|
|
36
|
+
/** The path of the issue, if any. */
|
|
37
|
+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
38
|
+
}
|
|
39
|
+
/** The path segment interface of the issue. */
|
|
40
|
+
export interface PathSegment {
|
|
41
|
+
/** The key representing a path segment. */
|
|
42
|
+
readonly key: PropertyKey;
|
|
43
|
+
}
|
|
44
|
+
/** The Standard Schema types interface. */
|
|
45
|
+
export interface Types<Input = unknown, Output = Input> {
|
|
46
|
+
/** The input type of the schema. */
|
|
47
|
+
readonly input: Input;
|
|
48
|
+
/** The output type of the schema. */
|
|
49
|
+
readonly output: Output;
|
|
50
|
+
}
|
|
51
|
+
/** Infers the input type of a Standard Schema. */
|
|
52
|
+
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['input'];
|
|
53
|
+
/** Infers the output type of a Standard Schema. */
|
|
54
|
+
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['output'];
|
|
55
|
+
export {};
|
|
16
56
|
}
|
|
17
57
|
export declare class BaseNotImplemented extends Error {
|
|
18
|
-
|
|
58
|
+
readonly cause: {
|
|
59
|
+
type: Base;
|
|
60
|
+
method: string;
|
|
61
|
+
};
|
|
62
|
+
constructor(type: Base, method: string);
|
|
19
63
|
}
|
|
20
64
|
/** Base class for creating extension types. */
|
|
21
65
|
export declare class Base<Value extends unknown = unknown> implements TSchema {
|
|
22
66
|
readonly '~kind': 'Base';
|
|
23
|
-
readonly '~standard':
|
|
67
|
+
readonly '~standard': StandardSchemaV1.Props<Value>;
|
|
24
68
|
constructor();
|
|
25
69
|
/** Checks a value or returns false if invalid */
|
|
26
70
|
Check(value: unknown): value is Value;
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { IsKind } from './schema.mjs';
|
|
3
|
+
// --------------------------------------------------------
|
|
4
|
+
// Standard Schema Factory
|
|
5
|
+
// --------------------------------------------------------
|
|
3
6
|
function Value(value) {
|
|
4
7
|
return { value };
|
|
5
8
|
}
|
|
6
9
|
function Issues(issues) {
|
|
10
|
+
// We cannot guarantee that the caller will pass an object with an
|
|
11
|
+
// error message, but it is generally implied. Additionally, we do
|
|
12
|
+
// not want StandardSchema interfaces proliferating throughout the
|
|
13
|
+
// codebase; they must remain contained within this module only.
|
|
7
14
|
return { issues };
|
|
8
15
|
}
|
|
9
16
|
// ------------------------------------------------------------------------------------
|
|
@@ -24,9 +31,14 @@ class StandardValidatorV1 {
|
|
|
24
31
|
// BaseError
|
|
25
32
|
// ------------------------------------------------------------------
|
|
26
33
|
export class BaseNotImplemented extends Error {
|
|
27
|
-
constructor(type,
|
|
28
|
-
|
|
29
|
-
|
|
34
|
+
constructor(type, method) {
|
|
35
|
+
super(`Base type does not implement the '${method}' function`);
|
|
36
|
+
Object.defineProperty(this, 'cause', {
|
|
37
|
+
value: { type, method },
|
|
38
|
+
writable: false,
|
|
39
|
+
configurable: false,
|
|
40
|
+
enumerable: false
|
|
41
|
+
});
|
|
30
42
|
}
|
|
31
43
|
}
|
|
32
44
|
// ------------------------------------------------------------------
|
|
@@ -35,13 +47,13 @@ export class BaseNotImplemented extends Error {
|
|
|
35
47
|
/** Base class for creating extension types. */
|
|
36
48
|
export class Base {
|
|
37
49
|
constructor() {
|
|
38
|
-
const validator = new StandardValidatorV1(value => this.Check(value), value => this.Errors(value));
|
|
50
|
+
const validator = new StandardValidatorV1((value) => this.Check(value), (value) => this.Errors(value));
|
|
39
51
|
const configuration = {
|
|
40
52
|
writable: false,
|
|
41
53
|
configurable: false,
|
|
42
54
|
enumerable: false
|
|
43
55
|
};
|
|
44
|
-
Object.defineProperty(this, '~kind', { ...configuration, value: 'Base'
|
|
56
|
+
Object.defineProperty(this, '~kind', { ...configuration, value: 'Base' });
|
|
45
57
|
Object.defineProperty(this, '~standard', { ...configuration, value: validator });
|
|
46
58
|
}
|
|
47
59
|
/** Checks a value or returns false if invalid */
|