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

feature: allow resources to be added from a map directly. #137

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,16 @@ func NewCompiler() *Compiler {
//
// Note that url must not have fragment
func (c *Compiler) AddResource(url string, r io.Reader) error {
res, err := newResource(url, r)
doc, err := unmarshal(r)
if err != nil {
return fmt.Errorf("jsonschema: invalid json %s: %v", url, err)
}
return c.AddResourceJSON(url, doc)
}

// AddResourceJSON adds in-memory resource from given json value.
func (c *Compiler) AddResourceJSON(url string, doc interface{}) error {
res, err := newResource(url, doc)
if err != nil {
return err
}
Expand Down
7 changes: 2 additions & 5 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ func (r *resource) String() string {
return r.url + r.floc
}

func newResource(url string, r io.Reader) (*resource, error) {
func newResource(url string, doc interface{}) (*resource, error) {
var err error
if strings.IndexByte(url, '#') != -1 {
panic(fmt.Sprintf("BUG: newResource(%q)", url))
}
doc, err := unmarshal(r)
if err != nil {
return nil, fmt.Errorf("jsonschema: invalid json %s: %v", url, err)
}
url, err = toAbs(url)
if err != nil {
return nil, err
Expand Down