1
1
import { getClassTypeFromInstance } from '@deepkit/core' ;
2
2
import { ReceiveType , resolveReceiveType } from './reflection/reflection.js' ;
3
- import { getPartialSerializeFunction , getSerializeFunction , NamingStrategy , SerializationOptions , serializer , Serializer , TemplateRegistry } from './serializer.js' ;
3
+ import {
4
+ getPartialSerializeFunction ,
5
+ getSerializeFunction ,
6
+ NamingStrategy ,
7
+ SerializationOptions ,
8
+ SerializeFunction ,
9
+ serializer ,
10
+ Serializer ,
11
+ TemplateRegistry
12
+ } from './serializer.js' ;
4
13
import { JSONPartial , JSONSingle } from './utils.js' ;
5
14
import { typeInfer } from './reflection/processor.js' ;
6
15
import { assert } from './typeguard.js' ;
@@ -22,17 +31,19 @@ export function cast<T>(data: JSONPartial<T> | unknown, options?: SerializationO
22
31
}
23
32
24
33
/**
25
- * Casts/coerces a given data structure to the target data type.
26
- *
27
- * Same as deserialize().
34
+ * Same as cast but returns a ready to use function. Used to improve performance.
28
35
*/
29
- export function castFunction < T > ( options ?: SerializationOptions , serializerToUse : Serializer = serializer , namingStrategy ?: NamingStrategy , type ?: ReceiveType < T > ) : ( data : JSONPartial < T > | unknown ) => T {
36
+ export function castFunction < T > ( serializerToUse : Serializer = serializer , namingStrategy ?: NamingStrategy , type ?: ReceiveType < T > ) : ( data : JSONPartial < T > | unknown , options ?: SerializationOptions ) => T {
30
37
const fn = getSerializeFunction ( resolveReceiveType ( type ) , serializerToUse . deserializeRegistry , namingStrategy ) ;
31
- return ( data : JSONPartial < T > | unknown ) => fn ( data , options ) ;
38
+ return ( data : JSONPartial < T > | unknown , options ?: SerializationOptions ) => {
39
+ const item = fn ( data , options ) ;
40
+ assert ( item , undefined , type ) ;
41
+ return item ;
42
+ }
32
43
}
33
44
34
45
/**
35
- * Deserialize given data structure from JSON data objects to JavaScript objects.
46
+ * Deserialize given data structure from JSON data objects to JavaScript objects, without running any validators .
36
47
*
37
48
* Types that are already correct will be used as-is.
38
49
*
@@ -44,7 +55,7 @@ export function castFunction<T>(options?: SerializationOptions, serializerToUse:
44
55
* const data = deserialize<Data>({created: '2009-02-13T23:31:30.123Z'});
45
56
* //data is {created: Date(2009-02-13T23:31:30.123Z)}
46
57
*
47
- * @throws ValidationError when serialization or validation fails.
58
+ * @throws ValidationError when deserialization fails.
48
59
* ```
49
60
*/
50
61
export function deserialize < T > ( data : JSONPartial < T > | unknown , options ?: SerializationOptions , serializerToUse : Serializer = serializer , namingStrategy ?: NamingStrategy , type ?: ReceiveType < T > ) : T {
@@ -55,7 +66,7 @@ export function deserialize<T>(data: JSONPartial<T> | unknown, options?: Seriali
55
66
/**
56
67
* Same as deserialize but returns a ready to use function. Used to improve performance.
57
68
*/
58
- export function deserializeFunction < T > ( options ?: SerializationOptions , serializerToUse : Serializer = serializer , namingStrategy ?: NamingStrategy , type ?: ReceiveType < T > ) : ( data : JSONPartial < T > | unknown ) => T {
69
+ export function deserializeFunction < T > ( serializerToUse : Serializer = serializer , namingStrategy ?: NamingStrategy , type ?: ReceiveType < T > ) : SerializeFunction < any , T > {
59
70
return getSerializeFunction ( resolveReceiveType ( type ) , serializerToUse . deserializeRegistry , namingStrategy ) ;
60
71
}
61
72
@@ -175,7 +186,7 @@ export function serialize<T>(data: T, options?: SerializationOptions, serializer
175
186
/**
176
187
* Same as serialize but returns a ready to use function. Used to improve performance.
177
188
*/
178
- export function serializeFunction < T > ( options ?: SerializationOptions , serializerToUse : Serializer = serializer , namingStrategy ?: NamingStrategy , type ?: ReceiveType < T > ) : ( data : T ) => any {
189
+ export function serializeFunction < T > ( serializerToUse : Serializer = serializer , namingStrategy ?: NamingStrategy , type ?: ReceiveType < T > ) : SerializeFunction < T > {
179
190
return getSerializeFunction ( resolveReceiveType ( type ) , serializerToUse . serializeRegistry , namingStrategy ) ;
180
191
}
181
192
@@ -193,6 +204,8 @@ export function cloneClass<T>(target: T, options?: SerializationOptions): T {
193
204
/**
194
205
* Tries to deserialize given data as T, and throws an error if it's not possible or validation after conversion fails.
195
206
*
207
+ * @deprecated use cast() instead
208
+ *
196
209
* @throws ValidationError when serialization or validation fails.
197
210
*/
198
211
export function validatedDeserialize < T > ( data : any , options ?: SerializationOptions , serializerToUse : Serializer = serializer , namingStrategy ?: NamingStrategy , type ?: ReceiveType < T > ) {
0 commit comments