Skip to content

Commit

Permalink
Document custom tags handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Sep 29, 2023
1 parent 9eb11d1 commit 2251096
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,36 @@ fmt.Println("Nested:", string(j))
// "type":"object"
// }
```

### Custom Tags For Schema Definitions

If you're using additional libraries for validation, like for example
[`go-playground/validator`](https://github.com/go-playground/validator), you may want to infer validation rules into
documented JSON schema.

```go
type My struct {
Foo *string `json:"foo" validate:"required"`
}
```

Normally, `validate:"required"` is not recognized, and you'd need to add `required:"true"` to have the rule exported to
JSON schema.

However, it is possible to extend reflection with custom processing with `InterceptProp` option.

```go
s, err := r.Reflect(My{}, jsonschema.InterceptProp(func(params jsonschema.InterceptPropParams) error {
if !params.Processed {
return nil
}

if v, ok := params.Field.Tag.Lookup("validate"); ok {
if strings.Contains(v, "required") {
params.ParentSchema.Required = append(params.ParentSchema.Required, params.Name)
}
}

return nil
}))
```

0 comments on commit 2251096

Please sign in to comment.