这是indexloc提供的服务,不要输入任何密码
Skip to content

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
@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

3 participants