func()

in code/go/internal/yamlschema/schema_loader.go [45:77]


func (l *yamlReferenceLoader) LoadJSON() (any, error) {
	parsed, err := url.Parse(l.source)
	if err != nil {
		return nil, fmt.Errorf("parsing source failed (source: %s): %w", l.source, err)
	}
	resourcePath := strings.TrimPrefix(parsed.Path, "/")

	itemSchemaData, err := fs.ReadFile(l.fs, resourcePath)
	if err != nil {
		return nil, fmt.Errorf("reading schema file failed: %w", err)
	}
	if len(itemSchemaData) == 0 {
		return nil, errors.New("schema file is empty")
	}

	var schema itemSchemaSpec
	err = yaml.Unmarshal(itemSchemaData, &schema)
	if err != nil {
		return nil, fmt.Errorf("schema unmarshalling failed (path: %s): %w", l.source, err)
	}

	// fixJSONNumbers ensures that the numbers in the resulting spec are of type `json.Number`, that is
	// what the gojsonschema library expects. Without this, gojsonschema complains about some integer types
	// not being integers.
	// TODO: This shouldn't probably be needed, we could try to fix gojsonschema to accept other integers, or
	// look for a YAML parser that can be customized to use `json.Number`.
	schema.Spec, err = fixJSONNumbers(schema.Spec)
	if err != nil {
		return nil, fmt.Errorf("fixing numbers in parsed schema failed (path %s): %w", l.source, err)
	}

	return schema.resolve(l.version)
}