Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add convenience model validator #126

Merged
merged 1 commit into from
Sep 11, 2023
Merged

Conversation

danielgtaylor
Copy link
Owner

@danielgtaylor danielgtaylor commented Sep 8, 2023

Adds a basic convenience utility for validating data against model schemas outside of the normal request/response flow.

Fixes #121.

@codecov
Copy link

codecov bot commented Sep 8, 2023

Codecov Report

Patch coverage is 100.00% of modified lines.

Files Changed Coverage
validate.go 100.00%

📢 Thoughts on this report? Let us know!.

@nickajacks1
Copy link
Contributor

Thanks, this is pretty much what I was looking for!

One other thing I realized yesterday (a bit unrelated) is that Schema and Registry have a cyclic dependency of sorts.
For example, in order to resolve a Schema that is or contains refs, it needs access to the registry.
Actually, every place in the code when a Ref schema is created, we also have access to the referenced schema object itself, so
a way to circumvent this would be to have a Schema that is a ref also hold either a pointer to the resolved ref or a function that returns the resolved schema.

For example:

type SchemaRef struct {
	Path   string  `yaml:"$ref,omitempty"`
	Schema *Schema `yaml:"-"`
}

type Schema struct {
	// ...
	Ref SchemaRef `yaml:",inline"`
	// ...
}

This would mean it is no longer necessary to pass the Registry object to the Validate function.
In my opinion, that could simplify the schema+registry internals ever so slightly, but I'm perfectly happy with what's here! Thanks again.

@danielgtaylor
Copy link
Owner Author

Thanks, that's an interesting approach to references and I like that schemas would contain all the information you need for e.g. validation. I'm going to merge this PR and will take a look at implementing your idea to see how it changes the code & interface because I can't visualize exactly how it may improve things without diving into the actual changes.

@danielgtaylor danielgtaylor merged commit fb679af into main Sep 11, 2023
3 checks passed
@danielgtaylor danielgtaylor deleted the model-validator branch September 11, 2023 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validating JSON outside of a request
2 participants