@predictorsdk/client 0.1.1 → 0.2.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/README.md +1 -1
- package/dist/Client.d.ts +20 -0
- package/dist/Client.js +164 -7
- package/dist/api/client/requests/GetBinanceCryptoPricesRequest.d.ts +18 -0
- package/dist/api/client/requests/GetBinanceCryptoPricesRequest.js +2 -0
- package/dist/api/client/requests/GetSportsMatchingMarketsRequest.d.ts +4 -4
- package/dist/api/client/requests/index.d.ts +1 -0
- package/dist/api/errors/BadGatewayError.d.ts +6 -0
- package/dist/api/errors/BadGatewayError.js +17 -0
- package/dist/api/errors/index.d.ts +1 -0
- package/dist/api/errors/index.js +1 -0
- package/dist/api/types/CryptoPriceItem.d.ts +8 -0
- package/dist/api/types/CryptoPriceItem.js +2 -0
- package/dist/api/types/CryptoPricesResponse.d.ts +8 -0
- package/dist/api/types/CryptoPricesResponse.js +2 -0
- package/dist/api/types/ErrorResponse.d.ts +3 -1
- package/dist/api/types/PlatformMarket.d.ts +8 -16
- package/dist/api/types/PlatformMarket.js +1 -9
- package/dist/api/types/PlatformMarketPlatform.d.ts +7 -0
- package/dist/api/types/PlatformMarketPlatform.js +7 -0
- package/dist/api/types/index.d.ts +3 -0
- package/dist/api/types/index.js +3 -0
- package/dist/core/auth/BasicAuth.d.ts +2 -2
- package/dist/core/auth/BasicAuth.js +6 -1
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +1 -0
- package/dist/core/schemas/Schema.d.ts +88 -0
- package/dist/core/schemas/Schema.js +22 -0
- package/dist/core/schemas/builders/bigint/bigint.d.ts +2 -0
- package/dist/core/schemas/builders/bigint/bigint.js +53 -0
- package/dist/core/schemas/builders/bigint/index.d.ts +1 -0
- package/dist/core/schemas/builders/bigint/index.js +1 -0
- package/dist/core/schemas/builders/date/date.d.ts +2 -0
- package/dist/core/schemas/builders/date/date.js +62 -0
- package/dist/core/schemas/builders/date/index.d.ts +1 -0
- package/dist/core/schemas/builders/date/index.js +1 -0
- package/dist/core/schemas/builders/enum/enum.d.ts +2 -0
- package/dist/core/schemas/builders/enum/enum.js +35 -0
- package/dist/core/schemas/builders/enum/index.d.ts +1 -0
- package/dist/core/schemas/builders/enum/index.js +1 -0
- package/dist/core/schemas/builders/index.d.ts +14 -0
- package/dist/core/schemas/builders/index.js +14 -0
- package/dist/core/schemas/builders/lazy/index.d.ts +3 -0
- package/dist/core/schemas/builders/lazy/index.js +2 -0
- package/dist/core/schemas/builders/lazy/lazy.d.ts +5 -0
- package/dist/core/schemas/builders/lazy/lazy.js +22 -0
- package/dist/core/schemas/builders/lazy/lazyObject.d.ts +3 -0
- package/dist/core/schemas/builders/lazy/lazyObject.js +17 -0
- package/dist/core/schemas/builders/list/index.d.ts +1 -0
- package/dist/core/schemas/builders/list/index.js +1 -0
- package/dist/core/schemas/builders/list/list.d.ts +2 -0
- package/dist/core/schemas/builders/list/list.js +49 -0
- package/dist/core/schemas/builders/literals/booleanLiteral.d.ts +2 -0
- package/dist/core/schemas/builders/literals/booleanLiteral.js +25 -0
- package/dist/core/schemas/builders/literals/index.d.ts +2 -0
- package/dist/core/schemas/builders/literals/index.js +2 -0
- package/dist/core/schemas/builders/literals/stringLiteral.d.ts +2 -0
- package/dist/core/schemas/builders/literals/stringLiteral.js +25 -0
- package/dist/core/schemas/builders/object/index.d.ts +6 -0
- package/dist/core/schemas/builders/object/index.js +3 -0
- package/dist/core/schemas/builders/object/object.d.ts +3 -0
- package/dist/core/schemas/builders/object/object.js +381 -0
- package/dist/core/schemas/builders/object/objectWithoutOptionalProperties.d.ts +6 -0
- package/dist/core/schemas/builders/object/objectWithoutOptionalProperties.js +4 -0
- package/dist/core/schemas/builders/object/property.d.ts +8 -0
- package/dist/core/schemas/builders/object/property.js +11 -0
- package/dist/core/schemas/builders/object/types.d.ts +31 -0
- package/dist/core/schemas/builders/object/types.js +1 -0
- package/dist/core/schemas/builders/object-like/getObjectLikeUtils.d.ts +9 -0
- package/dist/core/schemas/builders/object-like/getObjectLikeUtils.js +62 -0
- package/dist/core/schemas/builders/object-like/index.d.ts +2 -0
- package/dist/core/schemas/builders/object-like/index.js +1 -0
- package/dist/core/schemas/builders/object-like/types.d.ts +7 -0
- package/dist/core/schemas/builders/object-like/types.js +1 -0
- package/dist/core/schemas/builders/primitives/any.d.ts +2 -0
- package/dist/core/schemas/builders/primitives/any.js +6 -0
- package/dist/core/schemas/builders/primitives/boolean.d.ts +2 -0
- package/dist/core/schemas/builders/primitives/boolean.js +22 -0
- package/dist/core/schemas/builders/primitives/index.d.ts +6 -0
- package/dist/core/schemas/builders/primitives/index.js +6 -0
- package/dist/core/schemas/builders/primitives/never.d.ts +2 -0
- package/dist/core/schemas/builders/primitives/never.js +11 -0
- package/dist/core/schemas/builders/primitives/number.d.ts +2 -0
- package/dist/core/schemas/builders/primitives/number.js +22 -0
- package/dist/core/schemas/builders/primitives/string.d.ts +2 -0
- package/dist/core/schemas/builders/primitives/string.js +22 -0
- package/dist/core/schemas/builders/primitives/unknown.d.ts +2 -0
- package/dist/core/schemas/builders/primitives/unknown.js +3 -0
- package/dist/core/schemas/builders/record/index.d.ts +2 -0
- package/dist/core/schemas/builders/record/index.js +1 -0
- package/dist/core/schemas/builders/record/record.d.ts +4 -0
- package/dist/core/schemas/builders/record/record.js +133 -0
- package/dist/core/schemas/builders/record/types.d.ts +6 -0
- package/dist/core/schemas/builders/record/types.js +1 -0
- package/dist/core/schemas/builders/schema-utils/JsonError.d.ts +5 -0
- package/dist/core/schemas/builders/schema-utils/JsonError.js +9 -0
- package/dist/core/schemas/builders/schema-utils/ParseError.d.ts +5 -0
- package/dist/core/schemas/builders/schema-utils/ParseError.js +9 -0
- package/dist/core/schemas/builders/schema-utils/getSchemaUtils.d.ts +21 -0
- package/dist/core/schemas/builders/schema-utils/getSchemaUtils.js +150 -0
- package/dist/core/schemas/builders/schema-utils/index.d.ts +4 -0
- package/dist/core/schemas/builders/schema-utils/index.js +3 -0
- package/dist/core/schemas/builders/schema-utils/stringifyValidationErrors.d.ts +2 -0
- package/dist/core/schemas/builders/schema-utils/stringifyValidationErrors.js +6 -0
- package/dist/core/schemas/builders/set/index.d.ts +1 -0
- package/dist/core/schemas/builders/set/index.js +1 -0
- package/dist/core/schemas/builders/set/set.d.ts +2 -0
- package/dist/core/schemas/builders/set/set.js +42 -0
- package/dist/core/schemas/builders/undiscriminated-union/index.d.ts +2 -0
- package/dist/core/schemas/builders/undiscriminated-union/index.js +1 -0
- package/dist/core/schemas/builders/undiscriminated-union/types.d.ts +4 -0
- package/dist/core/schemas/builders/undiscriminated-union/types.js +1 -0
- package/dist/core/schemas/builders/undiscriminated-union/undiscriminatedUnion.d.ts +3 -0
- package/dist/core/schemas/builders/undiscriminated-union/undiscriminatedUnion.js +39 -0
- package/dist/core/schemas/builders/union/discriminant.d.ts +5 -0
- package/dist/core/schemas/builders/union/discriminant.js +6 -0
- package/dist/core/schemas/builders/union/index.d.ts +4 -0
- package/dist/core/schemas/builders/union/index.js +2 -0
- package/dist/core/schemas/builders/union/types.d.ts +13 -0
- package/dist/core/schemas/builders/union/types.js +1 -0
- package/dist/core/schemas/builders/union/union.d.ts +4 -0
- package/dist/core/schemas/builders/union/union.js +127 -0
- package/dist/core/schemas/index.d.ts +2 -0
- package/dist/core/schemas/index.js +1 -0
- package/dist/core/schemas/utils/MaybePromise.d.ts +1 -0
- package/dist/core/schemas/utils/MaybePromise.js +1 -0
- package/dist/core/schemas/utils/addQuestionMarksToNullableProperties.d.ts +7 -0
- package/dist/core/schemas/utils/addQuestionMarksToNullableProperties.js +1 -0
- package/dist/core/schemas/utils/createIdentitySchemaCreator.d.ts +2 -0
- package/dist/core/schemas/utils/createIdentitySchemaCreator.js +15 -0
- package/dist/core/schemas/utils/entries.d.ts +1 -0
- package/dist/core/schemas/utils/entries.js +3 -0
- package/dist/core/schemas/utils/filterObject.d.ts +1 -0
- package/dist/core/schemas/utils/filterObject.js +10 -0
- package/dist/core/schemas/utils/getErrorMessageForIncorrectType.d.ts +1 -0
- package/dist/core/schemas/utils/getErrorMessageForIncorrectType.js +24 -0
- package/dist/core/schemas/utils/isPlainObject.d.ts +1 -0
- package/dist/core/schemas/utils/isPlainObject.js +12 -0
- package/dist/core/schemas/utils/keys.d.ts +1 -0
- package/dist/core/schemas/utils/keys.js +3 -0
- package/dist/core/schemas/utils/maybeSkipValidation.d.ts +2 -0
- package/dist/core/schemas/utils/maybeSkipValidation.js +28 -0
- package/dist/core/schemas/utils/partition.d.ts +1 -0
- package/dist/core/schemas/utils/partition.js +12 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/serialization/index.d.ts +1 -0
- package/dist/serialization/index.js +1 -0
- package/dist/serialization/types/CryptoPriceItem.d.ts +11 -0
- package/dist/serialization/types/CryptoPriceItem.js +7 -0
- package/dist/serialization/types/CryptoPricesResponse.d.ts +12 -0
- package/dist/serialization/types/CryptoPricesResponse.js +8 -0
- package/dist/serialization/types/ErrorResponse.d.ts +11 -0
- package/dist/serialization/types/ErrorResponse.js +7 -0
- package/dist/serialization/types/PlatformMarket.d.ts +16 -0
- package/dist/serialization/types/PlatformMarket.js +12 -0
- package/dist/serialization/types/PlatformMarketPlatform.d.ts +7 -0
- package/dist/serialization/types/PlatformMarketPlatform.js +3 -0
- package/dist/serialization/types/SportsMatchingResponse.d.ts +10 -0
- package/dist/serialization/types/SportsMatchingResponse.js +6 -0
- package/dist/serialization/types/index.d.ts +6 -0
- package/dist/serialization/types/index.js +6 -0
- package/package.json +14 -3
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { SchemaType } from "../../Schema.js";
|
|
2
|
+
import { createIdentitySchemaCreator } from "../../utils/createIdentitySchemaCreator.js";
|
|
3
|
+
import { getErrorMessageForIncorrectType } from "../../utils/getErrorMessageForIncorrectType.js";
|
|
4
|
+
export const string = createIdentitySchemaCreator(SchemaType.STRING, (value, { breadcrumbsPrefix = [] } = {}) => {
|
|
5
|
+
if (typeof value === "string") {
|
|
6
|
+
return {
|
|
7
|
+
ok: true,
|
|
8
|
+
value,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
return {
|
|
13
|
+
ok: false,
|
|
14
|
+
errors: [
|
|
15
|
+
{
|
|
16
|
+
path: breadcrumbsPrefix,
|
|
17
|
+
message: getErrorMessageForIncorrectType(value, "string"),
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { partialRecord, record } from "./record.js";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type Schema } from "../../Schema.js";
|
|
2
|
+
import type { PartialRecordSchema, RecordSchema } from "./types.js";
|
|
3
|
+
export declare function record<RawKey extends string | number, RawValue, ParsedValue, ParsedKey extends string | number>(keySchema: Schema<RawKey, ParsedKey>, valueSchema: Schema<RawValue, ParsedValue>): RecordSchema<RawKey, RawValue, ParsedKey, ParsedValue>;
|
|
4
|
+
export declare function partialRecord<RawKey extends string | number, RawValue, ParsedValue, ParsedKey extends string | number>(keySchema: Schema<RawKey, ParsedKey>, valueSchema: Schema<RawValue, ParsedValue>): PartialRecordSchema<RawKey, RawValue, ParsedKey, ParsedValue>;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { SchemaType } from "../../Schema.js";
|
|
2
|
+
import { getErrorMessageForIncorrectType } from "../../utils/getErrorMessageForIncorrectType.js";
|
|
3
|
+
import { isPlainObject } from "../../utils/isPlainObject.js";
|
|
4
|
+
import { maybeSkipValidation } from "../../utils/maybeSkipValidation.js";
|
|
5
|
+
import { getSchemaUtils } from "../schema-utils/index.js";
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
7
|
+
const _hasOwn = Object.prototype.hasOwnProperty;
|
|
8
|
+
export function record(keySchema, valueSchema) {
|
|
9
|
+
const baseSchema = {
|
|
10
|
+
parse: (raw, opts) => {
|
|
11
|
+
return validateAndTransformRecord({
|
|
12
|
+
value: raw,
|
|
13
|
+
isKeyNumeric: keySchema.getType() === SchemaType.NUMBER,
|
|
14
|
+
transformKey: (key) => keySchema.parse(key, {
|
|
15
|
+
...opts,
|
|
16
|
+
breadcrumbsPrefix: [...(opts?.breadcrumbsPrefix ?? []), `${key} (key)`],
|
|
17
|
+
}),
|
|
18
|
+
transformValue: (value, key) => valueSchema.parse(value, {
|
|
19
|
+
...opts,
|
|
20
|
+
breadcrumbsPrefix: [...(opts?.breadcrumbsPrefix ?? []), `${key}`],
|
|
21
|
+
}),
|
|
22
|
+
breadcrumbsPrefix: opts?.breadcrumbsPrefix,
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
json: (parsed, opts) => {
|
|
26
|
+
return validateAndTransformRecord({
|
|
27
|
+
value: parsed,
|
|
28
|
+
isKeyNumeric: keySchema.getType() === SchemaType.NUMBER,
|
|
29
|
+
transformKey: (key) => keySchema.json(key, {
|
|
30
|
+
...opts,
|
|
31
|
+
breadcrumbsPrefix: [...(opts?.breadcrumbsPrefix ?? []), `${key} (key)`],
|
|
32
|
+
}),
|
|
33
|
+
transformValue: (value, key) => valueSchema.json(value, {
|
|
34
|
+
...opts,
|
|
35
|
+
breadcrumbsPrefix: [...(opts?.breadcrumbsPrefix ?? []), `${key}`],
|
|
36
|
+
}),
|
|
37
|
+
breadcrumbsPrefix: opts?.breadcrumbsPrefix,
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
getType: () => SchemaType.RECORD,
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
...maybeSkipValidation(baseSchema),
|
|
44
|
+
...getSchemaUtils(baseSchema),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export function partialRecord(keySchema, valueSchema) {
|
|
48
|
+
const baseSchema = {
|
|
49
|
+
parse: (raw, opts) => {
|
|
50
|
+
return validateAndTransformRecord({
|
|
51
|
+
value: raw,
|
|
52
|
+
isKeyNumeric: keySchema.getType() === SchemaType.NUMBER,
|
|
53
|
+
transformKey: (key) => keySchema.parse(key, {
|
|
54
|
+
...opts,
|
|
55
|
+
breadcrumbsPrefix: [...(opts?.breadcrumbsPrefix ?? []), `${key} (key)`],
|
|
56
|
+
}),
|
|
57
|
+
transformValue: (value, key) => valueSchema.parse(value, {
|
|
58
|
+
...opts,
|
|
59
|
+
breadcrumbsPrefix: [...(opts?.breadcrumbsPrefix ?? []), `${key}`],
|
|
60
|
+
}),
|
|
61
|
+
breadcrumbsPrefix: opts?.breadcrumbsPrefix,
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
json: (parsed, opts) => {
|
|
65
|
+
return validateAndTransformRecord({
|
|
66
|
+
value: parsed,
|
|
67
|
+
isKeyNumeric: keySchema.getType() === SchemaType.NUMBER,
|
|
68
|
+
transformKey: (key) => keySchema.json(key, {
|
|
69
|
+
...opts,
|
|
70
|
+
breadcrumbsPrefix: [...(opts?.breadcrumbsPrefix ?? []), `${key} (key)`],
|
|
71
|
+
}),
|
|
72
|
+
transformValue: (value, key) => valueSchema.json(value, {
|
|
73
|
+
...opts,
|
|
74
|
+
breadcrumbsPrefix: [...(opts?.breadcrumbsPrefix ?? []), `${key}`],
|
|
75
|
+
}),
|
|
76
|
+
breadcrumbsPrefix: opts?.breadcrumbsPrefix,
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
getType: () => SchemaType.RECORD,
|
|
80
|
+
};
|
|
81
|
+
return {
|
|
82
|
+
...maybeSkipValidation(baseSchema),
|
|
83
|
+
...getSchemaUtils(baseSchema),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function validateAndTransformRecord({ value, isKeyNumeric, transformKey, transformValue, breadcrumbsPrefix = [], }) {
|
|
87
|
+
if (!isPlainObject(value)) {
|
|
88
|
+
return {
|
|
89
|
+
ok: false,
|
|
90
|
+
errors: [
|
|
91
|
+
{
|
|
92
|
+
path: breadcrumbsPrefix,
|
|
93
|
+
message: getErrorMessageForIncorrectType(value, "object"),
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const result = {};
|
|
99
|
+
const errors = [];
|
|
100
|
+
for (const stringKey in value) {
|
|
101
|
+
if (!_hasOwn.call(value, stringKey)) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const entryValue = value[stringKey];
|
|
105
|
+
if (entryValue === undefined) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
let key = stringKey;
|
|
109
|
+
if (isKeyNumeric) {
|
|
110
|
+
const numberKey = stringKey.length > 0 ? Number(stringKey) : NaN;
|
|
111
|
+
if (!Number.isNaN(numberKey)) {
|
|
112
|
+
key = numberKey;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const transformedKey = transformKey(key);
|
|
116
|
+
const transformedValue = transformValue(entryValue, key);
|
|
117
|
+
if (transformedKey.ok && transformedValue.ok) {
|
|
118
|
+
result[transformedKey.value] = transformedValue.value;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
if (!transformedKey.ok) {
|
|
122
|
+
errors.push(...transformedKey.errors);
|
|
123
|
+
}
|
|
124
|
+
if (!transformedValue.ok) {
|
|
125
|
+
errors.push(...transformedValue.errors);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (errors.length === 0) {
|
|
130
|
+
return { ok: true, value: result };
|
|
131
|
+
}
|
|
132
|
+
return { ok: false, errors };
|
|
133
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { BaseSchema } from "../../Schema.js";
|
|
2
|
+
import type { SchemaUtils } from "../schema-utils/index.js";
|
|
3
|
+
export type RecordSchema<RawKey extends string | number, RawValue, ParsedKey extends string | number, ParsedValue> = BaseRecordSchema<RawKey, RawValue, ParsedKey, ParsedValue> & SchemaUtils<Record<RawKey, RawValue>, Record<ParsedKey, ParsedValue>>;
|
|
4
|
+
export type BaseRecordSchema<RawKey extends string | number, RawValue, ParsedKey extends string | number, ParsedValue> = BaseSchema<Record<RawKey, RawValue>, Record<ParsedKey, ParsedValue>>;
|
|
5
|
+
export type PartialRecordSchema<RawKey extends string | number, RawValue, ParsedKey extends string | number, ParsedValue> = BasePartialRecordSchema<RawKey, RawValue, ParsedKey, ParsedValue> & SchemaUtils<Record<RawKey, RawValue>, Partial<Record<ParsedKey, ParsedValue>>>;
|
|
6
|
+
export type BasePartialRecordSchema<RawKey extends string | number, RawValue, ParsedKey extends string | number, ParsedValue> = BaseSchema<Record<RawKey, RawValue>, Partial<Record<ParsedKey, ParsedValue>>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { stringifyValidationError } from "./stringifyValidationErrors.js";
|
|
2
|
+
export class JsonError extends Error {
|
|
3
|
+
errors;
|
|
4
|
+
constructor(errors) {
|
|
5
|
+
super(errors.map(stringifyValidationError).join("; "));
|
|
6
|
+
this.errors = errors;
|
|
7
|
+
Object.setPrototypeOf(this, JsonError.prototype);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { stringifyValidationError } from "./stringifyValidationErrors.js";
|
|
2
|
+
export class ParseError extends Error {
|
|
3
|
+
errors;
|
|
4
|
+
constructor(errors) {
|
|
5
|
+
super(errors.map(stringifyValidationError).join("; "));
|
|
6
|
+
this.errors = errors;
|
|
7
|
+
Object.setPrototypeOf(this, ParseError.prototype);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type BaseSchema, type Schema, type SchemaOptions } from "../../Schema.js";
|
|
2
|
+
export interface SchemaUtils<Raw, Parsed> {
|
|
3
|
+
nullable: () => Schema<Raw | null, Parsed | null>;
|
|
4
|
+
optional: () => Schema<Raw | null | undefined, Parsed | undefined>;
|
|
5
|
+
optionalNullable: () => Schema<Raw | null | undefined, Parsed | null | undefined>;
|
|
6
|
+
transform: <Transformed>(transformer: SchemaTransformer<Parsed, Transformed>) => Schema<Raw, Transformed>;
|
|
7
|
+
parseOrThrow: (raw: unknown, opts?: SchemaOptions) => Parsed;
|
|
8
|
+
jsonOrThrow: (raw: unknown, opts?: SchemaOptions) => Raw;
|
|
9
|
+
}
|
|
10
|
+
export interface SchemaTransformer<Parsed, Transformed> {
|
|
11
|
+
transform: (parsed: Parsed) => Transformed;
|
|
12
|
+
untransform: (transformed: any) => Parsed;
|
|
13
|
+
}
|
|
14
|
+
export declare function getSchemaUtils<Raw, Parsed>(schema: BaseSchema<Raw, Parsed>): SchemaUtils<Raw, Parsed>;
|
|
15
|
+
/**
|
|
16
|
+
* schema utils are defined in one file to resolve issues with circular imports
|
|
17
|
+
*/
|
|
18
|
+
export declare function nullable<Raw, Parsed>(schema: BaseSchema<Raw, Parsed>): Schema<Raw | null, Parsed | null>;
|
|
19
|
+
export declare function optional<Raw, Parsed>(schema: BaseSchema<Raw, Parsed>): Schema<Raw | null | undefined, Parsed | undefined>;
|
|
20
|
+
export declare function optionalNullable<Raw, Parsed>(schema: BaseSchema<Raw, Parsed>): Schema<Raw | null | undefined, Parsed | null | undefined>;
|
|
21
|
+
export declare function transform<Raw, Parsed, Transformed>(schema: BaseSchema<Raw, Parsed>, transformer: SchemaTransformer<Parsed, Transformed>): Schema<Raw, Transformed>;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { SchemaType } from "../../Schema.js";
|
|
2
|
+
import { JsonError } from "./JsonError.js";
|
|
3
|
+
import { ParseError } from "./ParseError.js";
|
|
4
|
+
export function getSchemaUtils(schema) {
|
|
5
|
+
return {
|
|
6
|
+
nullable: () => nullable(schema),
|
|
7
|
+
optional: () => optional(schema),
|
|
8
|
+
optionalNullable: () => optionalNullable(schema),
|
|
9
|
+
transform: (transformer) => transform(schema, transformer),
|
|
10
|
+
parseOrThrow: (raw, opts) => {
|
|
11
|
+
const parsed = schema.parse(raw, opts);
|
|
12
|
+
if (parsed.ok) {
|
|
13
|
+
return parsed.value;
|
|
14
|
+
}
|
|
15
|
+
throw new ParseError(parsed.errors);
|
|
16
|
+
},
|
|
17
|
+
jsonOrThrow: (parsed, opts) => {
|
|
18
|
+
const raw = schema.json(parsed, opts);
|
|
19
|
+
if (raw.ok) {
|
|
20
|
+
return raw.value;
|
|
21
|
+
}
|
|
22
|
+
throw new JsonError(raw.errors);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* schema utils are defined in one file to resolve issues with circular imports
|
|
28
|
+
*/
|
|
29
|
+
export function nullable(schema) {
|
|
30
|
+
const baseSchema = {
|
|
31
|
+
parse: (raw, opts) => {
|
|
32
|
+
if (raw == null) {
|
|
33
|
+
return {
|
|
34
|
+
ok: true,
|
|
35
|
+
value: null,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return schema.parse(raw, opts);
|
|
39
|
+
},
|
|
40
|
+
json: (parsed, opts) => {
|
|
41
|
+
if (parsed == null) {
|
|
42
|
+
return {
|
|
43
|
+
ok: true,
|
|
44
|
+
value: null,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
return schema.json(parsed, opts);
|
|
48
|
+
},
|
|
49
|
+
getType: () => SchemaType.NULLABLE,
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
...baseSchema,
|
|
53
|
+
...getSchemaUtils(baseSchema),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function optional(schema) {
|
|
57
|
+
const baseSchema = {
|
|
58
|
+
parse: (raw, opts) => {
|
|
59
|
+
if (raw == null) {
|
|
60
|
+
return {
|
|
61
|
+
ok: true,
|
|
62
|
+
value: undefined,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return schema.parse(raw, opts);
|
|
66
|
+
},
|
|
67
|
+
json: (parsed, opts) => {
|
|
68
|
+
if (opts?.omitUndefined && parsed === undefined) {
|
|
69
|
+
return {
|
|
70
|
+
ok: true,
|
|
71
|
+
value: undefined,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (parsed == null) {
|
|
75
|
+
return {
|
|
76
|
+
ok: true,
|
|
77
|
+
value: null,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
return schema.json(parsed, opts);
|
|
81
|
+
},
|
|
82
|
+
getType: () => SchemaType.OPTIONAL,
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
...baseSchema,
|
|
86
|
+
...getSchemaUtils(baseSchema),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
export function optionalNullable(schema) {
|
|
90
|
+
const baseSchema = {
|
|
91
|
+
parse: (raw, opts) => {
|
|
92
|
+
if (raw === undefined) {
|
|
93
|
+
return {
|
|
94
|
+
ok: true,
|
|
95
|
+
value: undefined,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
if (raw === null) {
|
|
99
|
+
return {
|
|
100
|
+
ok: true,
|
|
101
|
+
value: null,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
return schema.parse(raw, opts);
|
|
105
|
+
},
|
|
106
|
+
json: (parsed, opts) => {
|
|
107
|
+
if (parsed === undefined) {
|
|
108
|
+
return {
|
|
109
|
+
ok: true,
|
|
110
|
+
value: undefined,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
if (parsed === null) {
|
|
114
|
+
return {
|
|
115
|
+
ok: true,
|
|
116
|
+
value: null,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
return schema.json(parsed, opts);
|
|
120
|
+
},
|
|
121
|
+
getType: () => SchemaType.OPTIONAL_NULLABLE,
|
|
122
|
+
};
|
|
123
|
+
return {
|
|
124
|
+
...baseSchema,
|
|
125
|
+
...getSchemaUtils(baseSchema),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
export function transform(schema, transformer) {
|
|
129
|
+
const baseSchema = {
|
|
130
|
+
parse: (raw, opts) => {
|
|
131
|
+
const parsed = schema.parse(raw, opts);
|
|
132
|
+
if (!parsed.ok) {
|
|
133
|
+
return parsed;
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
ok: true,
|
|
137
|
+
value: transformer.transform(parsed.value),
|
|
138
|
+
};
|
|
139
|
+
},
|
|
140
|
+
json: (transformed, opts) => {
|
|
141
|
+
const parsed = transformer.untransform(transformed);
|
|
142
|
+
return schema.json(parsed, opts);
|
|
143
|
+
},
|
|
144
|
+
getType: () => schema.getType(),
|
|
145
|
+
};
|
|
146
|
+
return {
|
|
147
|
+
...baseSchema,
|
|
148
|
+
...getSchemaUtils(baseSchema),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { set } from "./set.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { set } from "./set.js";
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { SchemaType } from "../../Schema.js";
|
|
2
|
+
import { getErrorMessageForIncorrectType } from "../../utils/getErrorMessageForIncorrectType.js";
|
|
3
|
+
import { maybeSkipValidation } from "../../utils/maybeSkipValidation.js";
|
|
4
|
+
import { list } from "../list/index.js";
|
|
5
|
+
import { getSchemaUtils } from "../schema-utils/index.js";
|
|
6
|
+
export function set(schema) {
|
|
7
|
+
const listSchema = list(schema);
|
|
8
|
+
const baseSchema = {
|
|
9
|
+
parse: (raw, opts) => {
|
|
10
|
+
const parsedList = listSchema.parse(raw, opts);
|
|
11
|
+
if (parsedList.ok) {
|
|
12
|
+
return {
|
|
13
|
+
ok: true,
|
|
14
|
+
value: new Set(parsedList.value),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return parsedList;
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
json: (parsed, opts) => {
|
|
22
|
+
if (!(parsed instanceof Set)) {
|
|
23
|
+
return {
|
|
24
|
+
ok: false,
|
|
25
|
+
errors: [
|
|
26
|
+
{
|
|
27
|
+
path: opts?.breadcrumbsPrefix ?? [],
|
|
28
|
+
message: getErrorMessageForIncorrectType(parsed, "Set"),
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const jsonList = listSchema.json([...parsed], opts);
|
|
34
|
+
return jsonList;
|
|
35
|
+
},
|
|
36
|
+
getType: () => SchemaType.SET,
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
...maybeSkipValidation(baseSchema),
|
|
40
|
+
...getSchemaUtils(baseSchema),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { undiscriminatedUnion } from "./undiscriminatedUnion.js";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { inferParsed, inferRaw, Schema } from "../../Schema.js";
|
|
2
|
+
export type UndiscriminatedUnionSchema<Schemas extends [...Schema[]]> = Schema<inferRawUnidiscriminatedUnionSchema<Schemas>, inferParsedUnidiscriminatedUnionSchema<Schemas>>;
|
|
3
|
+
export type inferRawUnidiscriminatedUnionSchema<Schemas extends [...Schema[]]> = inferRaw<Schemas[number]>;
|
|
4
|
+
export type inferParsedUnidiscriminatedUnionSchema<Schemas extends [...Schema[]]> = inferParsed<Schemas[number]>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type Schema } from "../../Schema.js";
|
|
2
|
+
import type { inferParsedUnidiscriminatedUnionSchema, inferRawUnidiscriminatedUnionSchema } from "./types.js";
|
|
3
|
+
export declare function undiscriminatedUnion<Schemas extends [Schema<any, any>, ...Schema<any, any>[]]>(schemas: Schemas): Schema<inferRawUnidiscriminatedUnionSchema<Schemas>, inferParsedUnidiscriminatedUnionSchema<Schemas>>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { SchemaType, } from "../../Schema.js";
|
|
2
|
+
import { maybeSkipValidation } from "../../utils/maybeSkipValidation.js";
|
|
3
|
+
import { getSchemaUtils } from "../schema-utils/index.js";
|
|
4
|
+
export function undiscriminatedUnion(schemas) {
|
|
5
|
+
const baseSchema = {
|
|
6
|
+
parse: (raw, opts) => {
|
|
7
|
+
return validateAndTransformUndiscriminatedUnion((schema, opts) => schema.parse(raw, opts), schemas, opts);
|
|
8
|
+
},
|
|
9
|
+
json: (parsed, opts) => {
|
|
10
|
+
return validateAndTransformUndiscriminatedUnion((schema, opts) => schema.json(parsed, opts), schemas, opts);
|
|
11
|
+
},
|
|
12
|
+
getType: () => SchemaType.UNDISCRIMINATED_UNION,
|
|
13
|
+
};
|
|
14
|
+
return {
|
|
15
|
+
...maybeSkipValidation(baseSchema),
|
|
16
|
+
...getSchemaUtils(baseSchema),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function validateAndTransformUndiscriminatedUnion(transform, schemas, opts) {
|
|
20
|
+
const errors = [];
|
|
21
|
+
for (const [index, schema] of schemas.entries()) {
|
|
22
|
+
const transformed = transform(schema, { ...opts, skipValidation: false });
|
|
23
|
+
if (transformed.ok) {
|
|
24
|
+
return transformed;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
for (const error of transformed.errors) {
|
|
28
|
+
errors.push({
|
|
29
|
+
path: error.path,
|
|
30
|
+
message: `[Variant ${index}] ${error.message}`,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
ok: false,
|
|
37
|
+
errors,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function discriminant<RawDiscriminant extends string, ParsedDiscriminant extends string>(parsedDiscriminant: ParsedDiscriminant, rawDiscriminant: RawDiscriminant): Discriminant<RawDiscriminant, ParsedDiscriminant>;
|
|
2
|
+
export interface Discriminant<RawDiscriminant extends string, ParsedDiscriminant extends string> {
|
|
3
|
+
parsedDiscriminant: ParsedDiscriminant;
|
|
4
|
+
rawDiscriminant: RawDiscriminant;
|
|
5
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { Discriminant } from "./discriminant.js";
|
|
2
|
+
export { discriminant } from "./discriminant.js";
|
|
3
|
+
export type { inferParsedDiscriminant, inferParsedUnion, inferRawDiscriminant, inferRawUnion, UnionSubtypes, } from "./types.js";
|
|
4
|
+
export { union } from "./union.js";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { inferParsedObject, inferRawObject, ObjectSchema } from "../object/index.js";
|
|
2
|
+
import type { Discriminant } from "./discriminant.js";
|
|
3
|
+
export type UnionSubtypes<DiscriminantValues extends string | number | symbol> = {
|
|
4
|
+
[K in DiscriminantValues]: ObjectSchema<any, any>;
|
|
5
|
+
};
|
|
6
|
+
export type inferRawUnion<D extends string | Discriminant<any, any>, U extends UnionSubtypes<keyof U>> = {
|
|
7
|
+
[K in keyof U]: Record<inferRawDiscriminant<D>, K> & inferRawObject<U[K]>;
|
|
8
|
+
}[keyof U];
|
|
9
|
+
export type inferParsedUnion<D extends string | Discriminant<any, any>, U extends UnionSubtypes<keyof U>> = {
|
|
10
|
+
[K in keyof U]: Record<inferParsedDiscriminant<D>, K> & inferParsedObject<U[K]>;
|
|
11
|
+
}[keyof U];
|
|
12
|
+
export type inferRawDiscriminant<D extends string | Discriminant<any, any>> = D extends string ? D : D extends Discriminant<infer Raw, any> ? Raw : never;
|
|
13
|
+
export type inferParsedDiscriminant<D extends string | Discriminant<any, any>> = D extends string ? D : D extends Discriminant<any, infer Parsed> ? Parsed : never;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type ObjectLikeSchema } from "../object-like/index.js";
|
|
2
|
+
import type { Discriminant } from "./discriminant.js";
|
|
3
|
+
import type { inferParsedUnion, inferRawUnion, UnionSubtypes } from "./types.js";
|
|
4
|
+
export declare function union<D extends string | Discriminant<any, any>, U extends UnionSubtypes<any>>(discriminant: D, union: U): ObjectLikeSchema<inferRawUnion<D, U>, inferParsedUnion<D, U>>;
|