move schema package out of internal

This commit is contained in:
Joel Wetzell
2026-09-10 10:24:33 -05:00
parent 3217fa233e
commit 20aee7e50b
8 changed files with 2 additions and 2 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ import (
"github.com/jwetzell/showbridge-go/config"
"github.com/jwetzell/showbridge-go/internal/common"
"github.com/jwetzell/showbridge-go/internal/schema"
"github.com/jwetzell/showbridge-go/schema"
)
type ApiServer struct {
-29
View File
@@ -1,29 +0,0 @@
package schema
import (
"encoding/json"
"github.com/google/jsonschema-go/jsonschema"
)
var ApiConfigSchema = jsonschema.Schema{
ID: "https://showbridge.io/api.schema.json",
Type: "object",
Properties: map[string]*jsonschema.Schema{
"enabled": {
Type: "boolean",
Description: "Whether the API server is enabled",
Default: json.RawMessage(`false`),
},
"port": {
Type: "integer",
Description: "Port for the API server to listen on",
Minimum: jsonschema.Ptr[float64](1024),
Maximum: jsonschema.Ptr[float64](65535),
Default: json.RawMessage(`8080`),
},
},
Required: []string{"port"},
Default: json.RawMessage(`{"enabled": false, "port": 8080}`),
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
}
-40
View File
@@ -1,40 +0,0 @@
package schema
import (
"github.com/google/jsonschema-go/jsonschema"
)
var ConfigSchema = jsonschema.Schema{
Schema: "https://json-schema.org/draft/2020-12/schema",
ID: "https://showbridge.io/config.schema.json",
Title: "Config",
Description: "showbridge configuration",
Type: "object",
Properties: map[string]*jsonschema.Schema{
"api": &ApiConfigSchema,
"modules": {
Ref: "https://showbridge.io/modules.schema.json",
},
"routes": {
Ref: "https://showbridge.io/routes.schema.json",
},
},
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
}
func ApplyDefaults(cfg *map[string]any) error {
resolvedSchema, err := GetResolvedConfigSchema()
if err != nil {
return err
}
return resolvedSchema.ApplyDefaults(cfg)
}
func ValidateConfig(cfg map[string]any) error {
resolvedSchema, err := GetResolvedConfigSchema()
if err != nil {
return err
}
return resolvedSchema.Validate(cfg)
}
-56
View File
@@ -1,56 +0,0 @@
package schema
import (
"encoding/json"
"github.com/google/jsonschema-go/jsonschema"
"github.com/jwetzell/showbridge-go/internal/module"
)
func GetModulesSchema() *jsonschema.Schema {
schema := &jsonschema.Schema{
Schema: "https://json-schema.org/draft/2020-12/schema",
ID: "https://showbridge.io/modules.schema.json",
Title: "Modules",
Description: "module configurations",
Type: "array",
Default: json.RawMessage(`[]`),
}
moduleDefinitionSchemas := []*jsonschema.Schema{}
for _, mod := range module.GetModuleRegistrations() {
moduleSchema := &jsonschema.Schema{
ID: mod.Type,
Type: "object",
Properties: map[string]*jsonschema.Schema{
"id": {
Type: "string",
MinLength: new(1),
},
"type": {
Const: jsonschema.Ptr[any](mod.Type),
},
},
Required: []string{"id", "type"},
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
}
if mod.Title != "" {
moduleSchema.Title = mod.Title
}
if mod.Description != "" {
moduleSchema.Description = mod.Description
}
if mod.ParamsSchema != nil {
moduleSchema.Properties["params"] = mod.ParamsSchema
if len(mod.ParamsSchema.Required) > 0 {
moduleSchema.Required = append(moduleSchema.Required, "params")
}
}
moduleDefinitionSchemas = append(moduleDefinitionSchemas, moduleSchema)
}
schema.Items = &jsonschema.Schema{
OneOf: moduleDefinitionSchemas,
}
return schema
}
-56
View File
@@ -1,56 +0,0 @@
package schema
import (
"encoding/json"
"github.com/google/jsonschema-go/jsonschema"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func GetProcessorsSchema() *jsonschema.Schema {
schema := &jsonschema.Schema{
Schema: "https://json-schema.org/draft/2020-12/schema",
ID: "https://showbridge.io/processors.schema.json",
Title: "Processors",
Description: "processor configurations",
Type: "array",
Default: json.RawMessage(`[]`),
}
processorDefinitionSchemas := []*jsonschema.Schema{}
for _, proc := range processor.GetProcessorRegistrations() {
processorSchema := &jsonschema.Schema{
ID: proc.Type,
Type: "object",
Properties: map[string]*jsonschema.Schema{
"id": {
Type: "string",
MinLength: new(1),
},
"type": {
Const: jsonschema.Ptr[any](proc.Type),
},
},
Required: []string{"id", "type"},
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
}
if proc.Title != "" {
processorSchema.Title = proc.Title
}
if proc.Description != "" {
processorSchema.Description = proc.Description
}
if proc.ParamsSchema != nil {
processorSchema.Properties["params"] = proc.ParamsSchema
if len(proc.ParamsSchema.Required) > 0 {
processorSchema.Required = append(processorSchema.Required, "params")
}
}
processorDefinitionSchemas = append(processorDefinitionSchemas, processorSchema)
}
schema.Items = &jsonschema.Schema{
OneOf: processorDefinitionSchemas,
}
return schema
}
-34
View File
@@ -1,34 +0,0 @@
package schema
import (
"encoding/json"
"github.com/google/jsonschema-go/jsonschema"
)
var RoutesConfigSchema = jsonschema.Schema{
Schema: "https://json-schema.org/draft/2020-12/schema",
ID: "https://showbridge.io/routes.schema.json",
Title: "Routes",
Description: "route configurations",
Type: "array",
Items: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"id": {
Type: "string",
MinLength: new(1),
},
"input": {
Type: "string",
MinLength: new(1),
},
"processors": {
Ref: "https://showbridge.io/processors.schema.json",
},
},
Required: []string{"id", "input"},
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
},
Default: json.RawMessage(`[]`),
}
-26
View File
@@ -1,26 +0,0 @@
package schema
import (
"fmt"
"net/url"
"github.com/google/jsonschema-go/jsonschema"
)
func GetResolvedConfigSchema() (*jsonschema.Resolved, error) {
return ConfigSchema.Resolve(&jsonschema.ResolveOptions{
Loader: func(uri *url.URL) (*jsonschema.Schema, error) {
switch uri.String() {
case "https://showbridge.io/modules.schema.json":
return GetModulesSchema(), nil
case "https://showbridge.io/processors.schema.json":
return GetProcessorsSchema(), nil
case "https://showbridge.io/routes.schema.json":
return &RoutesConfigSchema, nil
default:
return nil, fmt.Errorf("unknown schema reference: %s", uri.String())
}
},
ValidateDefaults: true,
})
}