Skip to content

Commit 197ba39

Browse files
committed
setters don't throw
1 parent 71cc95a commit 197ba39

File tree

1 file changed

+1
-10
lines changed

1 file changed

+1
-10
lines changed

_posts/2024/2024-02-11-dropping-codegen.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,7 @@ To show this, consider the following schema.
3030
}
3131
```
3232

33-
As one would expect, the library would generate a class with a single property: `int foo`. But more than mere auto-properties, it generates extra code in the setter to ensure that the model stays within the constraints expressed in the schema, even at runtime.
34-
35-
This means that the setter for `foo` would also contain logic similar to
36-
37-
```c#
38-
if (value < 0)
39-
throw new ArgumentException(nameof(value), "Value must be greater than 0");
40-
41-
_foo = value;
42-
```
33+
As one would expect, the library would generate a class with a single property: `int Foo`. But it also generates an `.IsValid()` method that contains all of the validation logic. So if you set `model.Foo = -1`, the `.IsValid()` method will return false.
4334

4435
However _Corvus.JsonSchema_ has another trick up its sleeve. But before we get into that, it will help to have some understanding of how _System.Text.Json_'s `JsonElement` works.
4536

0 commit comments

Comments
 (0)