Skip to content

Commit

Permalink
setters don't throw
Browse files Browse the repository at this point in the history
  • Loading branch information
gregsdennis committed Feb 11, 2024
1 parent 71cc95a commit 197ba39
Showing 1 changed file with 1 addition and 10 deletions.
11 changes: 1 addition & 10 deletions _posts/2024/2024-02-11-dropping-codegen.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,7 @@ To show this, consider the following schema.
}
```

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.

This means that the setter for `foo` would also contain logic similar to

```c#
if (value < 0)
throw new ArgumentException(nameof(value), "Value must be greater than 0");

_foo = value;
```
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.

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.

Expand Down

0 comments on commit 197ba39

Please sign in to comment.