1- export const identifiers = {
1+ export const types = {
22 string : "string" ,
33 number : "number" ,
4+ array : "array" ,
45} as const ;
56
6- export const read = {
7- string : ( name : string ) => ( {
8- type : identifiers . string ,
7+ function readString ( name : string ) {
8+ return {
9+ type : types . string ,
10+ name,
11+ } ;
12+ }
13+
14+ function readNumber ( name : string ) {
15+ return {
16+ type : types . number ,
917 name,
10- } ) ,
11- number : ( name : string ) => ( {
12- type : identifiers . number ,
18+ } ;
19+ }
20+
21+ function readArray ( name : string , itemType : typeof types . string | typeof types . number ) {
22+ return {
23+ type : types . array ,
1324 name,
14- } ) ,
25+ itemType,
26+ } ;
27+ }
28+
29+ export const read = {
30+ string : readString ,
31+ number : readNumber ,
32+ array : readArray ,
1533} ;
1634
17- export type SchemaMessage = ReturnType < typeof read . string | typeof read . number > ;
35+ export type SchemaMessagePrimitive = ReturnType <
36+ typeof readString | typeof readNumber
37+ > ;
38+ export type SchemaMessage = ReturnType <
39+ typeof readString | typeof readNumber | typeof readArray
40+ > ;
1841export type SchemaReply = string | number ;
1942// export type SchemaGenerator<Result> = Generator<ReturnType<typeof read.string>, Result, string> | Generator<ReturnType<typeof read.number>, Result, number>;
2043export type SchemaGenerator < Result > = Generator <
@@ -24,7 +47,7 @@ export type SchemaGenerator<Result> = Generator<
2447> ;
2548
2649export function parseJSON < Result > (
27- source : Record < string , any > ,
50+ source : unknown ,
2851 generator : ( ) => SchemaGenerator < Result >
2952) {
3053 const gen = generator ( ) ;
@@ -38,13 +61,21 @@ export function parseJSON<Result>(
3861
3962 reply = undefined ;
4063
41- if ( result . value . type === identifiers . string ) {
42- if ( typeof source [ result . value . name ] === "string" ) {
43- reply = source [ result . value . name ] ;
44- }
45- } else if ( result . value . type === identifiers . number ) {
46- if ( typeof source [ result . value . name ] === "number" ) {
47- reply = source [ result . value . name ] ;
64+ if ( source instanceof Object && result . value . name in source ) {
65+ if ( result . value . type === types . string ) {
66+ if ( typeof ( source as any ) [ result . value . name ] === "string" ) {
67+ reply = ( source as any ) [ result . value . name ] ;
68+ }
69+ } else if ( result . value . type === types . number ) {
70+ if ( typeof ( source as any ) [ result . value . name ] === "number" ) {
71+ reply = ( source as any ) [ result . value . name ] ;
72+ }
73+ } else if ( result . value . type === types . array ) {
74+ const itemType = result . value . itemType ;
75+ if ( Array . isArray ( ( source as any ) [ result . value . name ] ) ) {
76+ const sanitizedItems = ( source as any ) [ result . value . name ] . map ( ( x : any ) => typeof x === itemType ? x : null ) ;
77+ reply = sanitizedItems ;
78+ }
4879 }
4980 }
5081 }
@@ -65,11 +96,11 @@ export function parseFormData<Result>(
6596
6697 reply = undefined ;
6798
68- if ( result . value . type === identifiers . string ) {
99+ if ( result . value . type === types . string ) {
69100 if ( source . has ( result . value . name ) ) {
70101 reply = source . get ( result . value . name ) as string ;
71102 }
72- } else if ( result . value . type === identifiers . number ) {
103+ } else if ( result . value . type === types . number ) {
73104 if ( source . has ( result . value . name ) ) {
74105 reply = parseFloat ( source . get ( result . value . name ) as string ) ;
75106 }
0 commit comments