@coinbase-sample/prime-sdk-ts 0.6.3 → 0.6.4
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/dist/constants.js +1 -1
- package/dist/model/enumPrefixes.js +330 -0
- package/dist/paymentMethods/index.js +2 -1
- package/dist/shared/dynamicEnumValidation.js +184 -0
- package/dist/shared/dynamicEnumValidation.old.js +746 -0
- package/dist/shared/enumHelpers.js +219 -0
- package/dist/shared/enumRegistry.js +153 -0
- package/dist/shared/enumValidationCore.js +194 -0
- package/dist/shared/enumValidators.js +115 -0
- package/dist/shared/fieldMapping.js +242 -0
- package/dist/shared/serviceContext.js +157 -0
- package/dist/types/balances/types.d.ts +1 -1
- package/dist/types/constants.d.ts +1 -1
- package/dist/types/model/enumPrefixes.d.ts +206 -0
- package/dist/types/paymentMethods/types.d.ts +1 -0
- package/dist/types/shared/dynamicEnumValidation.d.ts +48 -0
- package/dist/types/shared/dynamicEnumValidation.old.d.ts +143 -0
- package/dist/types/shared/enumHelpers.d.ts +135 -0
- package/dist/types/shared/enumRegistry.d.ts +74 -0
- package/dist/types/shared/enumValidationCore.d.ts +68 -0
- package/dist/types/shared/enumValidators.d.ts +117 -0
- package/dist/types/shared/fieldMapping.d.ts +35 -0
- package/dist/types/shared/serviceContext.d.ts +46 -0
- package/package.json +2 -2
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2025-present Coinbase Global, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Dynamic enum validation system that automatically discovers and validates
|
|
18
|
+
* enum fields in request objects without manual service-specific configuration.
|
|
19
|
+
*
|
|
20
|
+
* This approach is more scalable and maintainable than manual validation rules.
|
|
21
|
+
*/
|
|
22
|
+
import { enumRegistry } from './enumRegistry';
|
|
23
|
+
import { DynamicEnumValidationError, DynamicValidationConfig, FieldTypeInfo } from './enumValidationCore';
|
|
24
|
+
/**
|
|
25
|
+
* Set global configuration for dynamic enum validation
|
|
26
|
+
*/
|
|
27
|
+
export declare function setDynamicValidationConfig(config: Partial<DynamicValidationConfig>): void;
|
|
28
|
+
/**
|
|
29
|
+
* Get current dynamic validation configuration
|
|
30
|
+
*/
|
|
31
|
+
export declare function getDynamicValidationConfig(): DynamicValidationConfig;
|
|
32
|
+
/**
|
|
33
|
+
* Main API: Validate and normalize request with type-safe generics
|
|
34
|
+
*/
|
|
35
|
+
export declare function dynamicValidateRequest<T>(request: T, config?: Partial<DynamicValidationConfig>): T;
|
|
36
|
+
/**
|
|
37
|
+
* Get information about discovered enums
|
|
38
|
+
*/
|
|
39
|
+
export declare function getEnumInfo(): {
|
|
40
|
+
totalEnums: number;
|
|
41
|
+
enumNames: string[];
|
|
42
|
+
enumDetails: Record<string, {
|
|
43
|
+
values: string[];
|
|
44
|
+
keys: string[];
|
|
45
|
+
}>;
|
|
46
|
+
};
|
|
47
|
+
export { DynamicEnumValidationError, DynamicValidationConfig, FieldTypeInfo };
|
|
48
|
+
export { enumRegistry };
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2025-present Coinbase Global, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Error thrown when a request contains invalid enum values
|
|
18
|
+
*/
|
|
19
|
+
export declare class DynamicEnumValidationError extends Error {
|
|
20
|
+
field: string;
|
|
21
|
+
value: string;
|
|
22
|
+
enumName: string;
|
|
23
|
+
validValues: string[];
|
|
24
|
+
validKeys?: string[] | undefined;
|
|
25
|
+
constructor(field: string, value: string, enumName: string, validValues: string[], validKeys?: string[] | undefined);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Registry of all available enums for dynamic lookup
|
|
29
|
+
*/
|
|
30
|
+
declare class EnumRegistry {
|
|
31
|
+
private enumMap;
|
|
32
|
+
private enumNameMap;
|
|
33
|
+
constructor();
|
|
34
|
+
/**
|
|
35
|
+
* Automatically register all enums from the model/enums directory
|
|
36
|
+
*/
|
|
37
|
+
private registerAllEnums;
|
|
38
|
+
/**
|
|
39
|
+
* Check if an object is a valid enum
|
|
40
|
+
*/
|
|
41
|
+
private isValidEnum;
|
|
42
|
+
/**
|
|
43
|
+
* Get enum by name
|
|
44
|
+
*/
|
|
45
|
+
getEnum(enumName: string): Record<string, string> | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Get enum name for a given enum object
|
|
48
|
+
*/
|
|
49
|
+
getEnumName(enumObject: Record<string, string>): string | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* Find enum that contains a specific value
|
|
52
|
+
*/
|
|
53
|
+
findEnumByValue(value: string): {
|
|
54
|
+
enum: Record<string, string>;
|
|
55
|
+
name: string;
|
|
56
|
+
} | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* Find enum that contains a specific key
|
|
59
|
+
*/
|
|
60
|
+
findEnumByKey(key: string): {
|
|
61
|
+
enum: Record<string, string>;
|
|
62
|
+
name: string;
|
|
63
|
+
} | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* Get all registered enum names
|
|
66
|
+
*/
|
|
67
|
+
getAllEnumNames(): string[];
|
|
68
|
+
/**
|
|
69
|
+
* Validate a value against a specific enum with prefix handling
|
|
70
|
+
*/
|
|
71
|
+
validateEnumValue(enumName: string, value: string): {
|
|
72
|
+
valid: boolean;
|
|
73
|
+
normalizedValue?: string;
|
|
74
|
+
error?: string;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
declare const enumRegistry: EnumRegistry;
|
|
78
|
+
/**
|
|
79
|
+
* Validates and normalizes enum fields in a request object
|
|
80
|
+
*/
|
|
81
|
+
export declare function validateRequestEnums(request: any, options?: {
|
|
82
|
+
strict?: boolean;
|
|
83
|
+
autoNormalize?: boolean;
|
|
84
|
+
}): {
|
|
85
|
+
validatedRequest: any;
|
|
86
|
+
errors: string[];
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Simplified validation function that throws on first error
|
|
90
|
+
*/
|
|
91
|
+
export declare function validateAndNormalizeRequest(request: any): any;
|
|
92
|
+
/**
|
|
93
|
+
* Configuration for dynamic validation
|
|
94
|
+
*/
|
|
95
|
+
export interface DynamicValidationConfig {
|
|
96
|
+
enabled?: boolean;
|
|
97
|
+
strict?: boolean;
|
|
98
|
+
autoNormalize?: boolean;
|
|
99
|
+
logWarnings?: boolean;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Set global dynamic validation configuration
|
|
103
|
+
*/
|
|
104
|
+
export declare function setDynamicValidationConfig(config: Partial<DynamicValidationConfig>): void;
|
|
105
|
+
/**
|
|
106
|
+
* Get current dynamic validation configuration
|
|
107
|
+
*/
|
|
108
|
+
export declare function getDynamicValidationConfig(): DynamicValidationConfig;
|
|
109
|
+
/**
|
|
110
|
+
* Type-safe dynamic enum validation function
|
|
111
|
+
*
|
|
112
|
+
* Automatically validates and normalizes enum values in request objects
|
|
113
|
+
* using the auto-generated prefix patterns from the OpenAPI specification.
|
|
114
|
+
*
|
|
115
|
+
* @param request - The request object to validate and normalize
|
|
116
|
+
* @param config - Optional validation configuration
|
|
117
|
+
* @returns The validated request with normalized enum values
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const validated = dynamicValidateRequest<CreateOrderRequest>({
|
|
122
|
+
* portfolioId: 'portfolio-123',
|
|
123
|
+
* side: 'buy', // → 'BUY'
|
|
124
|
+
* type: 'market' // → 'MARKET'
|
|
125
|
+
* });
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
export declare function dynamicValidateRequest<T>(request: T, config?: Partial<DynamicValidationConfig>): T;
|
|
129
|
+
/**
|
|
130
|
+
* Export the enum registry for advanced usage
|
|
131
|
+
*/
|
|
132
|
+
export { enumRegistry };
|
|
133
|
+
/**
|
|
134
|
+
* Utility function to get information about available enums
|
|
135
|
+
*/
|
|
136
|
+
export declare function getEnumInfo(): {
|
|
137
|
+
enumNames: string[];
|
|
138
|
+
totalEnums: number;
|
|
139
|
+
enumDetails: Record<string, {
|
|
140
|
+
values: string[];
|
|
141
|
+
keys: string[];
|
|
142
|
+
}>;
|
|
143
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2025-present Coinbase Global, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Enum transformation utilities for JavaScript developers.
|
|
18
|
+
* These helpers make it easier to work with enums by providing:
|
|
19
|
+
* - Case-insensitive enum lookups
|
|
20
|
+
* - CamelCase to SNAKE_CASE conversion
|
|
21
|
+
* - Enum validation with helpful error messages
|
|
22
|
+
* - Support for prefix-aware transformations
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Converts a string from various formats to the expected enum value format (SCREAMING_SNAKE_CASE)
|
|
26
|
+
*
|
|
27
|
+
* @param input - The input string in any case format
|
|
28
|
+
* @returns The normalized SCREAMING_SNAKE_CASE string
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* normalizeEnumValue('buy') // 'BUY'
|
|
32
|
+
* normalizeEnumValue('marketOrder') // 'MARKET_ORDER'
|
|
33
|
+
* normalizeEnumValue('STOP_LIMIT') // 'STOP_LIMIT'
|
|
34
|
+
* normalizeEnumValue('Order_Type_Market') // 'ORDER_TYPE_MARKET'
|
|
35
|
+
*/
|
|
36
|
+
export declare function normalizeEnumValue(input: string): string;
|
|
37
|
+
/**
|
|
38
|
+
* Safely gets an enum value by normalizing the input and checking if it exists
|
|
39
|
+
*
|
|
40
|
+
* @param enumObject - The enum object to search in
|
|
41
|
+
* @param input - The input value to normalize and find
|
|
42
|
+
* @param enumName - Optional enum name for better error messages
|
|
43
|
+
* @returns The matching enum value or undefined if not found
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* import { OrderSide } from '../model/enums/OrderSide';
|
|
47
|
+
*
|
|
48
|
+
* getEnumValue(OrderSide, 'buy') // OrderSide.Buy
|
|
49
|
+
* getEnumValue(OrderSide, 'BUY') // OrderSide.Buy
|
|
50
|
+
* getEnumValue(OrderSide, 'invalid') // undefined
|
|
51
|
+
*/
|
|
52
|
+
export declare function getEnumValue<T extends Record<string, string>>(enumObject: T, input: string, enumName?: string): T[keyof T] | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Gets an enum value with strict validation, throwing an error if not found
|
|
55
|
+
*
|
|
56
|
+
* @param enumObject - The enum object to search in
|
|
57
|
+
* @param input - The input value to normalize and find
|
|
58
|
+
* @param enumName - Optional enum name for better error messages
|
|
59
|
+
* @returns The matching enum value
|
|
60
|
+
* @throws Error if the enum value is not found
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* import { OrderSide } from '../model/enums/OrderSide';
|
|
64
|
+
*
|
|
65
|
+
* requireEnumValue(OrderSide, 'buy') // OrderSide.Buy
|
|
66
|
+
* requireEnumValue(OrderSide, 'invalid') // throws Error
|
|
67
|
+
*/
|
|
68
|
+
export declare function requireEnumValue<T extends Record<string, string>>(enumObject: T, input: string, enumName?: string): T[keyof T];
|
|
69
|
+
/**
|
|
70
|
+
* Checks if a string is a valid enum value (case-insensitive)
|
|
71
|
+
*
|
|
72
|
+
* @param enumObject - The enum object to check against
|
|
73
|
+
* @param input - The input value to validate
|
|
74
|
+
* @returns True if the input is a valid enum value
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* import { OrderSide } from '../model/enums/OrderSide';
|
|
78
|
+
*
|
|
79
|
+
* isValidEnumValue(OrderSide, 'buy') // true
|
|
80
|
+
* isValidEnumValue(OrderSide, 'BUY') // true
|
|
81
|
+
* isValidEnumValue(OrderSide, 'invalid') // false
|
|
82
|
+
*/
|
|
83
|
+
export declare function isValidEnumValue<T extends Record<string, string>>(enumObject: T, input: string): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Gets all possible enum values as an array
|
|
86
|
+
*
|
|
87
|
+
* @param enumObject - The enum object
|
|
88
|
+
* @returns Array of all enum values
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* import { OrderSide } from '../model/enums/OrderSide';
|
|
92
|
+
*
|
|
93
|
+
* getEnumValues(OrderSide) // ['BUY', 'SELL']
|
|
94
|
+
*/
|
|
95
|
+
export declare function getEnumValues<T extends Record<string, string>>(enumObject: T): T[keyof T][];
|
|
96
|
+
/**
|
|
97
|
+
* Gets all possible enum keys as an array
|
|
98
|
+
*
|
|
99
|
+
* @param enumObject - The enum object
|
|
100
|
+
* @returns Array of all enum keys
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* import { OrderSide } from '../model/enums/OrderSide';
|
|
104
|
+
*
|
|
105
|
+
* getEnumKeys(OrderSide) // ['Buy', 'Sell']
|
|
106
|
+
*/
|
|
107
|
+
export declare function getEnumKeys<T extends Record<string, string>>(enumObject: T): (keyof T)[];
|
|
108
|
+
/**
|
|
109
|
+
* Validates that a string has the correct prefix for enum values
|
|
110
|
+
* Useful for ensuring enum values follow expected patterns
|
|
111
|
+
*
|
|
112
|
+
* @param input - The enum value to check
|
|
113
|
+
* @param expectedPrefix - The expected prefix (e.g., 'ORDER_TYPE_', 'ACTIVITY_TYPE_')
|
|
114
|
+
* @param strict - If true, requires exact prefix match. If false, allows values without prefix
|
|
115
|
+
* @returns True if the prefix is correct
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* validateEnumPrefix('ORDER_TYPE_MARKET', 'ORDER_TYPE_') // true
|
|
119
|
+
* validateEnumPrefix('MARKET', 'ORDER_TYPE_', false) // true (non-strict)
|
|
120
|
+
* validateEnumPrefix('MARKET', 'ORDER_TYPE_', true) // false (strict)
|
|
121
|
+
*/
|
|
122
|
+
export declare function validateEnumPrefix(input: string, expectedPrefix: string, strict?: boolean): boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Transforms enum input to the correct format, optionally adding a prefix
|
|
125
|
+
*
|
|
126
|
+
* @param input - The input string
|
|
127
|
+
* @param prefix - Optional prefix to add if not present
|
|
128
|
+
* @returns The transformed enum value
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* transformEnumValue('market', 'ORDER_TYPE_') // 'ORDER_TYPE_MARKET'
|
|
132
|
+
* transformEnumValue('ORDER_TYPE_MARKET', 'ORDER_TYPE_') // 'ORDER_TYPE_MARKET'
|
|
133
|
+
* transformEnumValue('marketOrder') // 'MARKET_ORDER'
|
|
134
|
+
*/
|
|
135
|
+
export declare function transformEnumValue(input: string, prefix?: string): string;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2025-present Coinbase Global, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Registry for managing discovered enums and providing lookup capabilities
|
|
18
|
+
*/
|
|
19
|
+
export declare class EnumRegistry {
|
|
20
|
+
private enums;
|
|
21
|
+
private enumsByValue;
|
|
22
|
+
private enumsByKey;
|
|
23
|
+
constructor();
|
|
24
|
+
/**
|
|
25
|
+
* Automatically discover and register all enums from the model/enums directory
|
|
26
|
+
*/
|
|
27
|
+
private populateEnums;
|
|
28
|
+
/**
|
|
29
|
+
* Check if an object is a valid TypeScript enum
|
|
30
|
+
*/
|
|
31
|
+
private isValidEnum;
|
|
32
|
+
/**
|
|
33
|
+
* Index enum values and keys for fast lookup
|
|
34
|
+
*/
|
|
35
|
+
private indexEnumValues;
|
|
36
|
+
/**
|
|
37
|
+
* Get a specific enum by name
|
|
38
|
+
*/
|
|
39
|
+
getEnum(enumName: string): Record<string, string> | null;
|
|
40
|
+
/**
|
|
41
|
+
* Get all registered enum names
|
|
42
|
+
*/
|
|
43
|
+
getAllEnumNames(): string[];
|
|
44
|
+
/**
|
|
45
|
+
* Find enum by value (exact match)
|
|
46
|
+
*/
|
|
47
|
+
findEnumByValue(value: string): {
|
|
48
|
+
enum: Record<string, string>;
|
|
49
|
+
name: string;
|
|
50
|
+
} | null;
|
|
51
|
+
/**
|
|
52
|
+
* Find enum by key (exact match)
|
|
53
|
+
*/
|
|
54
|
+
findEnumByKey(key: string): {
|
|
55
|
+
enum: Record<string, string>;
|
|
56
|
+
name: string;
|
|
57
|
+
} | null;
|
|
58
|
+
/**
|
|
59
|
+
* Get statistics about discovered enums
|
|
60
|
+
*/
|
|
61
|
+
getStats(): {
|
|
62
|
+
totalEnums: number;
|
|
63
|
+
totalValues: number;
|
|
64
|
+
totalKeys: number;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Get detailed information about all enums
|
|
68
|
+
*/
|
|
69
|
+
getEnumDetails(): Record<string, {
|
|
70
|
+
values: string[];
|
|
71
|
+
keys: string[];
|
|
72
|
+
}>;
|
|
73
|
+
}
|
|
74
|
+
export declare const enumRegistry: EnumRegistry;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2025-present Coinbase Global, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Error thrown when a request contains invalid enum values
|
|
18
|
+
*/
|
|
19
|
+
export declare class DynamicEnumValidationError extends Error {
|
|
20
|
+
field: string;
|
|
21
|
+
value: string;
|
|
22
|
+
enumName: string;
|
|
23
|
+
validValues: string[];
|
|
24
|
+
validKeys?: string[] | undefined;
|
|
25
|
+
constructor(field: string, value: string, enumName: string, validValues: string[], validKeys?: string[] | undefined);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Configuration for dynamic validation behavior
|
|
29
|
+
*/
|
|
30
|
+
export interface DynamicValidationConfig {
|
|
31
|
+
enabled: boolean;
|
|
32
|
+
strict: boolean;
|
|
33
|
+
autoNormalize: boolean;
|
|
34
|
+
logWarnings: boolean;
|
|
35
|
+
serviceName?: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Information about a field that contains enum values
|
|
39
|
+
*/
|
|
40
|
+
export interface FieldTypeInfo {
|
|
41
|
+
fieldName: string;
|
|
42
|
+
enumName: string;
|
|
43
|
+
isOptional: boolean;
|
|
44
|
+
isArray: boolean;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Check if a value is valid for a specific enum (includes normalization)
|
|
48
|
+
*/
|
|
49
|
+
export declare function isValueValidForEnum(value: string, enumObject: Record<string, string>): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Detects the enum type for a field based on field name patterns and value analysis
|
|
52
|
+
*/
|
|
53
|
+
export declare function detectEnumType(fieldName: string, value: string, serviceName?: string): {
|
|
54
|
+
enum: Record<string, string>;
|
|
55
|
+
name: string;
|
|
56
|
+
} | null;
|
|
57
|
+
/**
|
|
58
|
+
* Analyzes a single field to determine if it's an enum
|
|
59
|
+
*/
|
|
60
|
+
export declare function analyzeField(fieldName: string, value: any, serviceName?: string): FieldTypeInfo | null;
|
|
61
|
+
/**
|
|
62
|
+
* Validate and normalize a single enum value
|
|
63
|
+
*/
|
|
64
|
+
export declare function validateEnumValue(value: string, enumObject: Record<string, string>, enumName: string, fieldName: string, config: DynamicValidationConfig): {
|
|
65
|
+
isValid: boolean;
|
|
66
|
+
normalizedValue?: string;
|
|
67
|
+
error?: string;
|
|
68
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2025-present Coinbase Global, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Convenient enum validators for common Prime API enums.
|
|
18
|
+
* These provide pre-configured validation for frequently used enum types.
|
|
19
|
+
*
|
|
20
|
+
* Note: Most users should use the dynamic validation system instead, which
|
|
21
|
+
* automatically handles all enums. These validators are useful for explicit
|
|
22
|
+
* control or when you need to validate individual enum values.
|
|
23
|
+
*/
|
|
24
|
+
import { normalizeEnumValue } from './enumHelpers';
|
|
25
|
+
import { OrderType } from '../model/enums/OrderType';
|
|
26
|
+
import { OrderSide } from '../model/enums/OrderSide';
|
|
27
|
+
import { OrderStatus } from '../model/enums/OrderStatus';
|
|
28
|
+
import { TransactionType } from '../model/enums/TransactionType';
|
|
29
|
+
import { TimeInForceType } from '../model/enums/TimeInForceType';
|
|
30
|
+
/**
|
|
31
|
+
* Order-related enum helpers
|
|
32
|
+
*/
|
|
33
|
+
export declare const OrderEnums: {
|
|
34
|
+
/**
|
|
35
|
+
* Validates and normalizes order type values
|
|
36
|
+
* Accepts: 'market', 'limit', 'twap', 'block', 'vwap', 'stopLimit', 'rfq' (case-insensitive)
|
|
37
|
+
*/
|
|
38
|
+
getOrderType: (input: string) => OrderType | undefined;
|
|
39
|
+
requireOrderType: (input: string) => OrderType;
|
|
40
|
+
isValidOrderType: (input: string) => boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Validates and normalizes order side values
|
|
43
|
+
* Accepts: 'buy', 'sell' (case-insensitive)
|
|
44
|
+
*/
|
|
45
|
+
getOrderSide: (input: string) => OrderSide | undefined;
|
|
46
|
+
requireOrderSide: (input: string) => OrderSide;
|
|
47
|
+
isValidOrderSide: (input: string) => boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Validates and normalizes order status values
|
|
50
|
+
* Accepts: 'open', 'filled', 'cancelled', 'expired', 'failed', 'pending' (case-insensitive)
|
|
51
|
+
*/
|
|
52
|
+
getOrderStatus: (input: string) => OrderStatus | undefined;
|
|
53
|
+
requireOrderStatus: (input: string) => OrderStatus;
|
|
54
|
+
isValidOrderStatus: (input: string) => boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Validates and normalizes time in force type values
|
|
57
|
+
* Accepts: 'good_until_cancelled', 'good_until_time', 'immediate_or_cancel', 'fill_or_kill' (case-insensitive)
|
|
58
|
+
*/
|
|
59
|
+
getTimeInForceType: (input: string) => TimeInForceType | undefined;
|
|
60
|
+
requireTimeInForceType: (input: string) => TimeInForceType;
|
|
61
|
+
isValidTimeInForceType: (input: string) => boolean;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Transaction-related enum helpers
|
|
65
|
+
*/
|
|
66
|
+
export declare const TransactionEnums: {
|
|
67
|
+
/**
|
|
68
|
+
* Validates and normalizes transaction type values
|
|
69
|
+
* Accepts various transaction types like 'deposit', 'withdrawal', 'internalDeposit', etc. (case-insensitive)
|
|
70
|
+
*/
|
|
71
|
+
getTransactionType: (input: string) => TransactionType | undefined;
|
|
72
|
+
requireTransactionType: (input: string) => TransactionType;
|
|
73
|
+
isValidTransactionType: (input: string) => boolean;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Utility for creating custom enum validators for any enum type
|
|
77
|
+
*
|
|
78
|
+
* @param enumObject - The enum object to create validators for
|
|
79
|
+
* @param enumName - Name of the enum for error messages
|
|
80
|
+
* @returns Object with validation methods
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* import { WalletType } from '../model/enums/WalletType';
|
|
84
|
+
*
|
|
85
|
+
* const WalletValidators = createEnumValidators(WalletType, 'WalletType');
|
|
86
|
+
* WalletValidators.get('trading'); // Gets WalletType value
|
|
87
|
+
* WalletValidators.require('custody'); // Gets WalletType value or throws
|
|
88
|
+
* WalletValidators.isValid('invalid'); // false
|
|
89
|
+
*/
|
|
90
|
+
export declare function createEnumValidators<T extends Record<string, string>>(enumObject: T, enumName: string): {
|
|
91
|
+
get: (input: string) => T[keyof T] | undefined;
|
|
92
|
+
require: (input: string) => T[keyof T];
|
|
93
|
+
isValid: (input: string) => boolean;
|
|
94
|
+
values: string[];
|
|
95
|
+
keys: string[];
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Convenience function to normalize any string to enum format
|
|
99
|
+
* Useful for debugging or preparing values for API calls
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* toEnumFormat('marketOrder') // 'MARKET_ORDER'
|
|
103
|
+
* toEnumFormat('buy') // 'BUY'
|
|
104
|
+
* toEnumFormat('stop-limit') // 'STOP_LIMIT'
|
|
105
|
+
*/
|
|
106
|
+
export { normalizeEnumValue as toEnumFormat };
|
|
107
|
+
/**
|
|
108
|
+
* @deprecated Use the dynamic validation system instead.
|
|
109
|
+
*
|
|
110
|
+
* Most users should prefer:
|
|
111
|
+
* ```javascript
|
|
112
|
+
* import { dynamicValidateRequest } from '@coinbase/prime-sdk';
|
|
113
|
+
* const validated = dynamicValidateRequest(request);
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* This provides automatic validation for all enum fields without manual configuration.
|
|
117
|
+
*/
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2025-present Coinbase Global, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Generate field name variations for plural/singular matching
|
|
18
|
+
*/
|
|
19
|
+
export declare function getFieldNameVariations(fieldName: string): string[];
|
|
20
|
+
/**
|
|
21
|
+
* Extract context clues from field names to resolve enum ambiguity
|
|
22
|
+
*/
|
|
23
|
+
export declare function extractContextClues(fieldName: string): string[];
|
|
24
|
+
/**
|
|
25
|
+
* Find enum with context awareness to resolve collisions
|
|
26
|
+
*/
|
|
27
|
+
export declare function findEnumWithContext(fieldName: string, allEnumNames: string[]): string | null;
|
|
28
|
+
/**
|
|
29
|
+
* Dynamically find enum name based on field name patterns
|
|
30
|
+
*/
|
|
31
|
+
export declare function findEnumByFieldName(fieldName: string): string | null;
|
|
32
|
+
/**
|
|
33
|
+
* Get direct field-to-enum mapping with service context and fallback to essential mappings
|
|
34
|
+
*/
|
|
35
|
+
export declare function getDirectEnumMapping(fieldName: string, serviceName?: string): string | null;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2025-present Coinbase Global, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Service context for enhanced enum field detection
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Service-specific enum mappings to resolve ambiguous field names
|
|
21
|
+
*/
|
|
22
|
+
export interface ServiceEnumContext {
|
|
23
|
+
serviceName: string;
|
|
24
|
+
fieldMappings: Record<string, string>;
|
|
25
|
+
description?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Registry of service contexts for accurate enum detection
|
|
29
|
+
*/
|
|
30
|
+
export declare const SERVICE_CONTEXTS: Record<string, ServiceEnumContext>;
|
|
31
|
+
/**
|
|
32
|
+
* Get service context by service name
|
|
33
|
+
*/
|
|
34
|
+
export declare function getServiceContext(serviceName: string): ServiceEnumContext | null;
|
|
35
|
+
/**
|
|
36
|
+
* Resolve field to enum using service context
|
|
37
|
+
*/
|
|
38
|
+
export declare function resolveFieldWithServiceContext(fieldName: string, serviceName?: string): string | null;
|
|
39
|
+
/**
|
|
40
|
+
* Get all available service contexts
|
|
41
|
+
*/
|
|
42
|
+
export declare function getAllServiceContexts(): Record<string, ServiceEnumContext>;
|
|
43
|
+
/**
|
|
44
|
+
* Check if a service has context defined
|
|
45
|
+
*/
|
|
46
|
+
export declare function hasServiceContext(serviceName: string): boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coinbase-sample/prime-sdk-ts",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.4",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/types/index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -67,6 +67,6 @@
|
|
|
67
67
|
"typescript": "^5.9.2"
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
|
-
"@coinbase-sample/core-ts": "^0.2.
|
|
70
|
+
"@coinbase-sample/core-ts": "^0.2.1"
|
|
71
71
|
}
|
|
72
72
|
}
|