Skip to content

Commit 51b90a4

Browse files
committed
S.null -> S.nullAsOption
1 parent 547e4ae commit 51b90a4

29 files changed

+107
-82
lines changed

IDEAS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- Reworked error code and added `S.Error.classify` to turn error into a variant of all possible error codes
1717
- All errors thrown in transform/refine are wrapped in `SuryError`
1818
- TS: Updated `S.Error` type to use variants instead of code property
19+
- ReScript: `S.null` -> `S.nullAsOption`
1920

2021
### TS
2122

@@ -40,6 +41,8 @@
4041

4142
TODO:
4243

44+
Test null<> in ppx
45+
4346
```
4447
// Test that refinement works correctly with reverse
4548

docs/rescript-usage.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- [`Option.getOr`](#optiongetor)
2121
- [`Option.getOrWith`](#optiongetorwith)
2222
- [`null`](#null)
23+
- [`nullAsOption`](#nullasoption)
2324
- [`nullable`](#nullable)
2425
- [`nullableAsOption`](#nullableasoption)
2526
- [`unit`](#unit)
@@ -403,20 +404,35 @@ Also you can use `Option.getOrWith` for lazy evaluation of the default value.
403404

404405
### **`null`**
405406

406-
`S.t<'value> => S.t<option<'value>>`
407+
`S.t<'value> => S.t<null<'value>>`
407408

408409
```rescript
409410
let schema = S.null(S.string)
410411
412+
"Hello World!"->S.parseOrThrow(schema)
413+
// Value("Hello World!")
414+
%raw(`null`)->S.parseOrThrow(schema)
415+
// Null
416+
```
417+
418+
The `S.null` schema represents a data of a specific type that might be null.
419+
420+
### **`nullAsOption`**
421+
422+
`S.t<'value> => S.t<option<'value>>`
423+
424+
```rescript
425+
let schema = S.nullAsOption(S.string)
426+
411427
"Hello World!"->S.parseOrThrow(schema)
412428
// Some("Hello World!")
413429
%raw(`null`)->S.parseOrThrow(schema)
414430
// None
415431
```
416432

417-
The `S.null` schema represents a data of a specific type that might be null.
433+
The `S.nullAsOption` schema represents a data of a specific type that might be null.
418434

419-
> 🧠 Since `S.null` transforms value into `option` type, you can use `Option.getOr`/`Option.getOrWith` for it as well.
435+
> 🧠 Since `S.nullAsOption` transforms value into `option` type, you can use `Option.getOr`/`Option.getOrWith` for it as well.
420436
421437
### **`nullable`**
422438

@@ -907,7 +923,7 @@ The `S.list` schema represents an array of data of a specific type which is tran
907923
```rescript
908924
let schema = S.unnest(S.schema(s => {
909925
id: s.matches(S.string),
910-
name: s.matches(S.null(S.string)),
926+
name: s.matches(S.nullAsOption(S.string)),
911927
deleted: s.matches(S.bool),
912928
}))
913929
@@ -1366,7 +1382,7 @@ And you can configure compiled function `typeValidation` with the following opti
13661382
`(S.t<'value>) => S.t<'value>`
13671383

13681384
```rescript
1369-
S.null(S.string)->S.reverse
1385+
S.nullAsOption(S.string)->S.reverse
13701386
// S.option(S.string)
13711387
```
13721388

packages/e2e/src/ppx/Ppx_Example_test.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ test("@s.defaultWith", t => {
6868
@schema
6969
type null = @s.null option<string>
7070
test("@s.null", t => {
71-
t->assertEqualSchemas(nullSchema, S.null(S.string))
71+
t->assertEqualSchemas(nullSchema, S.nullAsOption(S.string))
7272
})
7373

7474
@schema
7575
type nullWithDefault = @s.null @s.default("Unknown") string
7676
test("@s.null with @s.default", t => {
77-
t->assertEqualSchemas(nullWithDefaultSchema, S.null(S.string)->S.Option.getOr("Unknown"))
77+
t->assertEqualSchemas(nullWithDefaultSchema, S.nullAsOption(S.string)->S.Option.getOr("Unknown"))
7878
})
7979

8080
@schema

packages/e2e/src/ppx/Ppx_General_test.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type stringWithDefaultNullAndMatches = @s.default("Foo") @s.null @s.matches(S.st
4646
test("Creates schema with default null using @s.matches", t => {
4747
t->assertEqualSchemas(
4848
stringWithDefaultNullAndMatchesSchema,
49-
S.null(S.string->S.url)->S.Option.getOr("Foo"),
49+
S.nullAsOption(S.string->S.url)->S.Option.getOr("Foo"),
5050
)
5151
})
5252

packages/e2e/src/ppx/Ppx_Primitive_test.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ test("Custom optional string schema", t => {
153153
// The incompatible parts: option<string> vs myNullOfString (defined as null<string>)
154154
// So use the code below instead
155155
@schema
156-
type myNullOfString = @s.matches(S.null(S.string)) option<string>
156+
type myNullOfString = @s.matches(S.nullAsOption(S.string)) option<string>
157157
test("Null of string schema", t => {
158-
t->assertEqualSchemas(myNullOfStringSchema, S.null(S.string))
158+
t->assertEqualSchemas(myNullOfStringSchema, S.nullAsOption(S.string))
159159
})

packages/e2e/src/ppx/Ppx_Record_test.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ test("Record schema with optional fields", t => {
6464

6565
@schema
6666
type recordWithNullableField = {
67-
subscription: @s.matches(S.option(S.null(S.string))) option<option<string>>,
67+
subscription: @s.matches(S.option(S.nullAsOption(S.string))) option<option<string>>,
6868
}
6969
test("Record schema with nullable field", t => {
7070
t->assertEqualSchemas(
7171
recordWithNullableFieldSchema,
7272
S.schema(s => {
73-
subscription: s.matches(S.option(S.null(S.string))),
73+
subscription: s.matches(S.option(S.nullAsOption(S.string))),
7474
}),
7575
)
7676
t->Assert.deepEqual(

packages/sury-ppx/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ let schema = S.string->S.url
133133

134134
**Applies to**: option type expressions
135135

136-
Tells to use `S.null` for the option schema constructor.
136+
Tells to use `S.nullAsOption` for the option schema constructor.
137137

138138
```rescript
139139
@schema
140140
type t = @s.null option<string>
141141
142142
// Generated by PPX ⬇️
143-
let schema = S.null(S.string)
143+
let schema = S.nullAsOption(S.string)
144144
```
145145

146146
### `@s.nullable`
@@ -178,7 +178,7 @@ It might also be used together with `@s.null`:
178178
type t = @s.null @s.default("Unknown") string
179179
180180
// Generated by PPX ⬇️
181-
let schema = S.null(S.string)->S.Option.getOr("Unknown")
181+
let schema = S.nullAsOption(S.string)->S.Option.getOr("Unknown")
182182
```
183183

184184
### `@s.defaultWith(unit => 'value)`

packages/sury-ppx/src/ppx/Structure.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ and generateCoreTypeSchemaExpression core_type =
148148
getAttributeByName ptyp_attributes "s.nullable" )
149149
with
150150
| Ok None, Ok None -> [%expr S.option]
151-
| Ok (Some _), Ok None -> [%expr S.null]
151+
| Ok (Some _), Ok None -> [%expr S.nullAsOption]
152152
| Ok None, Ok (Some _) -> [%expr S.nullableAsOption]
153153
| Ok (Some _), Ok (Some _) ->
154154
fail ptyp_loc

packages/sury/src/S.res.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ let option = Sury.option;
5656

5757
let $$null = Sury.$$null;
5858

59+
let nullAsOption = Sury.nullAsOption;
60+
5961
let nullable = Sury.nullable;
6062

6163
let nullableAsOption = Sury.nullableAsOption;
@@ -214,6 +216,7 @@ export {
214216
dict,
215217
option,
216218
$$null,
219+
nullAsOption,
217220
nullable,
218221
nullableAsOption,
219222
union,

packages/sury/src/S.resi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,9 @@ let list: t<'value> => t<list<'value>>
313313
let instance: unknown => t<unknown>
314314
let dict: t<'value> => t<dict<'value>>
315315
let option: t<'value> => t<option<'value>>
316-
let null: t<'value> => t<option<'value>>
317-
let nullable: t<'value> => t<Js.nullable<'value>>
316+
let null: t<'value> => t<null<'value>>
317+
let nullAsOption: t<'value> => t<option<'value>>
318+
let nullable: t<'value> => t<nullable<'value>>
318319
let nullableAsOption: t<'value> => t<option<'value>>
319320
let union: array<t<'value>> => t<'value>
320321
let enum: array<'value> => t<'value>

0 commit comments

Comments
 (0)