Skip to content

Commit 4f9ad6f

Browse files
committed
[IO-363] add binary and base64 IO
1 parent d56cdfe commit 4f9ad6f

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

src/language/typescript/common/bundled/utils.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const utilsRef = fromString('#/utils/utils');
77

88
const utils = `
99
import { either } from 'fp-ts/lib/Either';
10-
import { Type, failure, success, string as tstring } from 'io-ts';
10+
import { Type, type, TypeOf, failure, success, string as tstring, literal } from 'io-ts';
1111
1212
export const DateFromISODateStringIO = new Type<Date, string, unknown>(
1313
'DateFromISODateString',
@@ -24,6 +24,35 @@ const utils = `
2424
.toString()
2525
.padStart(2, '0')}\`,
2626
);
27+
28+
export type Base64 = TypeOf<typeof Base64IO>;
29+
30+
export const Base64IO = type({
31+
string: tstring,
32+
format: literal('base64'),
33+
});
34+
35+
export const Base64FromStringIO = new Type<Base64, string, unknown>(
36+
'Base64FromString',
37+
(u): u is Base64 => Base64IO.is(u),
38+
(u, c) => either.chain(tstring.validate(u, c), string => success({ string, format: 'base64' })),
39+
a => a.string,
40+
);
41+
42+
export type Binary = TypeOf<typeof BinaryIO>;
43+
44+
export const BinaryIO = type({
45+
string: tstring,
46+
format: literal('binary'),
47+
});
48+
49+
export const BinaryFromStringIO = new Type<Binary, string, unknown>(
50+
'BinaryFromString',
51+
(u): u is Binary => BinaryIO.is(u),
52+
(u, c) => either.chain(tstring.validate(u, c), string => success({ string, format: 'binary' })),
53+
a => a.string,
54+
);
55+
2756
`;
2857

2958
export const utilsFile = pipe(

src/language/typescript/common/data/serialized-type.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,31 @@ export const getSerializedStringType = (from: Ref, format: Option<string>): Eith
109109
),
110110
);
111111
}
112-
case 'base64':
112+
case 'base64': {
113+
return some(
114+
serializedType(
115+
'Base64',
116+
'Base64FromStringIO',
117+
[
118+
serializedDependency('Base64FromStringIO', getRelativePath(from, utilsRef)),
119+
serializedDependency('Base64', getRelativePath(from, utilsRef)),
120+
],
121+
[],
122+
),
123+
);
124+
}
113125
case 'binary': {
114-
return some(SERIALIZED_UNKNOWN_TYPE);
126+
return some(
127+
serializedType(
128+
'Binary',
129+
'BinaryFromStringIO',
130+
[
131+
serializedDependency('BinaryFromStringIO', getRelativePath(from, utilsRef)),
132+
serializedDependency('Binary', getRelativePath(from, utilsRef)),
133+
],
134+
[],
135+
),
136+
);
115137
}
116138
}
117139
return none;

test/specs/3.0/file-and-text.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ paths:
103103
content:
104104
image/png:
105105
schema:
106-
$ref: '#/components/schemas/File'
107-
/image:
106+
type: string
107+
format: base64
108108
/audio:
109109
get:
110110
tags:
@@ -118,6 +118,19 @@ paths:
118118
image/png:
119119
schema:
120120
$ref: '#/components/schemas/File'
121+
/table:
122+
get:
123+
tags:
124+
- media
125+
summary: get table
126+
operationId: loadTable
127+
responses:
128+
200:
129+
description: succesfull operation
130+
content:
131+
text/csv:
132+
schema:
133+
$ref: '#/components/schemas/File'
121134
/upload:
122135
post:
123136
tags:

0 commit comments

Comments
 (0)