Skip to content

Commit 3bb7d2e

Browse files
committed
Add title field support
1 parent 7de638e commit 3bb7d2e

10 files changed

+285
-170
lines changed

IDEAS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
- (rescript) Added `S.nullableAsOption` to replace `S.nullable` from V9
1010
- (ppx) Returned back `@s.nullable` attribute
1111
- (ppx) Added support for `Js.nullable` type
12+
- Add `title` field to schema and allow to set it via `S.meta`
1213

1314
### Final release fixes
1415

15-
- Add title to meta
1616
- Add `S.env` to support coercion for union items separately. Like `rescript-envsafe` used to do with `preprocess`
1717
- Make `S.record` accept two args
1818
- Update docs

packages/sury/src/S.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export type Schema<Output, Input = unknown> = {
131131
): Schema<O, I>;
132132

133133
readonly name?: string;
134+
readonly title?: string;
134135
readonly description?: string;
135136
readonly deprecated?: boolean;
136137
readonly examples?: Input[];
@@ -553,6 +554,7 @@ export function recursive<Output, Input = Output>(
553554

554555
export type Meta<Output> = {
555556
name?: string;
557+
title?: string;
556558
description?: string;
557559
deprecated?: boolean;
558560
examples?: Output[];

packages/sury/src/S.resi

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,20 @@ type tag =
4646
@tag("type")
4747
type rec t<'value> =
4848
private
49-
| @as("never") Never({name?: string, description?: string, deprecated?: bool})
49+
| @as("never") Never({name?: string, title?: string, description?: string, deprecated?: bool})
5050
| @as("unknown")
5151
Unknown({
5252
name?: string,
5353
description?: string,
54+
title?: string,
5455
deprecated?: bool,
5556
examples?: array<unknown>,
5657
})
5758
| @as("string")
5859
String({
5960
const?: string,
6061
name?: string,
62+
title?: string,
6163
description?: string,
6264
deprecated?: bool,
6365
examples?: array<string>,
@@ -67,6 +69,7 @@ type rec t<'value> =
6769
const?: float,
6870
format?: numberFormat,
6971
name?: string,
72+
title?: string,
7073
description?: string,
7174
deprecated?: bool,
7275
examples?: array<float>,
@@ -75,6 +78,7 @@ type rec t<'value> =
7578
BigInt({
7679
const?: bigint,
7780
name?: string,
81+
title?: string,
7882
description?: string,
7983
deprecated?: bool,
8084
examples?: array<bigint>,
@@ -83,6 +87,7 @@ type rec t<'value> =
8387
Boolean({
8488
const?: bool,
8589
name?: string,
90+
title?: string,
8691
description?: string,
8792
deprecated?: bool,
8893
examples?: array<bool>,
@@ -91,6 +96,7 @@ type rec t<'value> =
9196
Symbol({
9297
const?: Js.Types.symbol,
9398
name?: string,
99+
title?: string,
94100
description?: string,
95101
deprecated?: bool,
96102
examples?: array<Js.Types.symbol>,
@@ -99,21 +105,31 @@ type rec t<'value> =
99105
Null({
100106
const: Js.Types.null_val,
101107
name?: string,
108+
title?: string,
102109
description?: string,
103110
deprecated?: bool,
104111
})
105112
| @as("undefined")
106113
Undefined({
107114
const: unit,
108115
name?: string,
116+
title?: string,
117+
description?: string,
118+
deprecated?: bool,
119+
})
120+
| @as("nan")
121+
NaN({
122+
const: float,
123+
name?: string,
124+
title?: string,
109125
description?: string,
110126
deprecated?: bool,
111127
})
112-
| @as("nan") NaN({const: float, name?: string, description?: string, deprecated?: bool})
113128
| @as("function")
114129
Function({
115130
const?: Js.Types.function_val,
116131
name?: string,
132+
title?: string,
117133
description?: string,
118134
deprecated?: bool,
119135
examples?: array<Js.Types.function_val>,
@@ -123,6 +139,7 @@ type rec t<'value> =
123139
class: unknown,
124140
const?: Js.Types.obj_val,
125141
name?: string,
142+
title?: string,
126143
description?: string,
127144
deprecated?: bool,
128145
examples?: array<Js.Types.obj_val>,
@@ -133,6 +150,7 @@ type rec t<'value> =
133150
additionalItems: additionalItems,
134151
unnest?: bool,
135152
name?: string,
153+
title?: string,
136154
description?: string,
137155
deprecated?: bool,
138156
examples?: array<array<unknown>>,
@@ -143,6 +161,7 @@ type rec t<'value> =
143161
fields: dict<item>,
144162
additionalItems: additionalItems,
145163
name?: string,
164+
title?: string,
146165
description?: string,
147166
deprecated?: bool,
148167
examples?: array<dict<unknown>>,
@@ -152,20 +171,23 @@ type rec t<'value> =
152171
anyOf: array<t<unknown>>,
153172
has: has,
154173
name?: string,
174+
title?: string,
155175
description?: string,
156176
deprecated?: bool,
157177
examples?: array<unknown>,
158178
})
159179
| @as("json")
160180
JSON({
161181
name?: string,
182+
title?: string,
162183
description?: string,
163184
deprecated?: bool,
164185
examples?: array<Js.Json.t>,
165186
})
166187
and schema<'a> = t<'a>
167188
and meta<'value> = {
168189
name?: string,
190+
title?: string,
169191
description?: string,
170192
deprecated?: bool,
171193
examples?: array<'value>,
@@ -176,6 +198,7 @@ and untagged = private {
176198
const?: unknown,
177199
class?: unknown,
178200
name?: string,
201+
title?: string,
179202
description?: string,
180203
deprecated?: bool,
181204
examples?: array<unknown>,

packages/sury/src/Sury.res

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,20 @@ type additionalItemsMode = | @as("strip") Strip | @as("strict") Strict
250250
@tag("type")
251251
type rec t<'value> =
252252
private
253-
| @as("never") Never({name?: string, description?: string, deprecated?: bool})
253+
| @as("never") Never({name?: string, title?: string, description?: string, deprecated?: bool})
254254
| @as("unknown")
255255
Unknown({
256256
name?: string,
257257
description?: string,
258+
title?: string,
258259
deprecated?: bool,
259260
examples?: array<unknown>,
260261
})
261262
| @as("string")
262263
String({
263264
const?: string,
264265
name?: string,
266+
title?: string,
265267
description?: string,
266268
deprecated?: bool,
267269
examples?: array<string>,
@@ -271,6 +273,7 @@ type rec t<'value> =
271273
const?: float,
272274
format?: numberFormat,
273275
name?: string,
276+
title?: string,
274277
description?: string,
275278
deprecated?: bool,
276279
examples?: array<float>,
@@ -279,6 +282,7 @@ type rec t<'value> =
279282
BigInt({
280283
const?: bigint,
281284
name?: string,
285+
title?: string,
282286
description?: string,
283287
deprecated?: bool,
284288
examples?: array<bigint>,
@@ -287,6 +291,7 @@ type rec t<'value> =
287291
Boolean({
288292
const?: bool,
289293
name?: string,
294+
title?: string,
290295
description?: string,
291296
deprecated?: bool,
292297
examples?: array<bool>,
@@ -295,6 +300,7 @@ type rec t<'value> =
295300
Symbol({
296301
const?: Js.Types.symbol,
297302
name?: string,
303+
title?: string,
298304
description?: string,
299305
deprecated?: bool,
300306
examples?: array<Js.Types.symbol>,
@@ -303,21 +309,31 @@ type rec t<'value> =
303309
Null({
304310
const: Js.Types.null_val,
305311
name?: string,
312+
title?: string,
306313
description?: string,
307314
deprecated?: bool,
308315
})
309316
| @as("undefined")
310317
Undefined({
311318
const: unit,
312319
name?: string,
320+
title?: string,
321+
description?: string,
322+
deprecated?: bool,
323+
})
324+
| @as("nan")
325+
NaN({
326+
const: float,
327+
name?: string,
328+
title?: string,
313329
description?: string,
314330
deprecated?: bool,
315331
})
316-
| @as("nan") NaN({const: float, name?: string, description?: string, deprecated?: bool})
317332
| @as("function")
318333
Function({
319334
const?: Js.Types.function_val,
320335
name?: string,
336+
title?: string,
321337
description?: string,
322338
deprecated?: bool,
323339
examples?: array<Js.Types.function_val>,
@@ -327,6 +343,7 @@ type rec t<'value> =
327343
class: unknown,
328344
const?: Js.Types.obj_val,
329345
name?: string,
346+
title?: string,
330347
description?: string,
331348
deprecated?: bool,
332349
examples?: array<Js.Types.obj_val>,
@@ -337,6 +354,7 @@ type rec t<'value> =
337354
additionalItems: additionalItems,
338355
unnest?: bool,
339356
name?: string,
357+
title?: string,
340358
description?: string,
341359
deprecated?: bool,
342360
examples?: array<array<unknown>>,
@@ -347,6 +365,7 @@ type rec t<'value> =
347365
fields: dict<item>,
348366
additionalItems: additionalItems,
349367
name?: string,
368+
title?: string,
350369
description?: string,
351370
deprecated?: bool,
352371
examples?: array<dict<unknown>>,
@@ -356,13 +375,15 @@ type rec t<'value> =
356375
anyOf: array<t<unknown>>,
357376
has: has,
358377
name?: string,
378+
title?: string,
359379
description?: string,
360380
deprecated?: bool,
361381
examples?: array<unknown>,
362382
})
363383
| @as("json")
364384
JSON({
365385
name?: string,
386+
title?: string,
366387
description?: string,
367388
deprecated?: bool,
368389
examples?: array<Js.Json.t>,
@@ -378,6 +399,7 @@ and internal = {
378399
mutable const?: char, // use char to avoid Caml_option.some
379400
mutable class?: char, // use char to avoid Caml_option.some
380401
mutable name?: string,
402+
mutable title?: string,
381403
mutable description?: string,
382404
mutable deprecated?: bool,
383405
mutable examples?: array<unknown>,
@@ -402,6 +424,7 @@ and internal = {
402424
}
403425
and meta<'value> = {
404426
name?: string,
427+
title?: string,
405428
description?: string,
406429
deprecated?: bool,
407430
examples?: array<'value>,
@@ -412,6 +435,7 @@ and untagged = private {
412435
const?: unknown,
413436
class?: unknown,
414437
name?: string,
438+
title?: string,
415439
description?: string,
416440
deprecated?: bool,
417441
examples?: array<unknown>,
@@ -3163,6 +3187,11 @@ let meta = (schema: t<'value>, data: meta<'value>) => {
31633187
| Some(name) => mut.name = Some(name)
31643188
| None => ()
31653189
}
3190+
switch data.title {
3191+
| Some("") => mut.title = None
3192+
| Some(title) => mut.title = Some(title)
3193+
| None => ()
3194+
}
31663195
switch data.description {
31673196
| Some("") => mut.description = None
31683197
| Some(description) => mut.description = Some(description)
@@ -5185,6 +5214,11 @@ module RescriptJSONSchema = {
51855214
| _ => ()
51865215
}
51875216

5217+
switch schema->untag {
5218+
| {title: m} => jsonSchema.title = Some(m)
5219+
| _ => ()
5220+
}
5221+
51885222
switch schema->untag {
51895223
| {deprecated} => jsonSchema.deprecated = Some(deprecated)
51905224
| _ => ()
@@ -5474,8 +5508,9 @@ let rec fromJSONSchema = {
54745508
}
54755509

54765510
let schema = switch jsonSchema {
5477-
| {description: _} | {deprecated: _} | {examples: _} =>
5511+
| {description: _} | {deprecated: _} | {examples: _} | {title: _} =>
54785512
schema->meta({
5513+
title: ?jsonSchema.title,
54795514
description: ?jsonSchema.description,
54805515
deprecated: ?jsonSchema.deprecated,
54815516
examples: ?jsonSchema.examples,

0 commit comments

Comments
 (0)