mirror of
https://github.com/navidrome/navidrome.git
synced 2026-03-04 06:35:52 +00:00
* feat(plugins): add JSONForms schema for plugin configuration Signed-off-by: Deluan <deluan@navidrome.org> * feat: enhance error handling by formatting validation errors with field names Signed-off-by: Deluan <deluan@navidrome.org> * feat: enforce required fields in config validation and improve error handling Signed-off-by: Deluan <deluan@navidrome.org> * format JS code Signed-off-by: Deluan <deluan@navidrome.org> * feat: add config schema validation and enhance manifest structure Signed-off-by: Deluan <deluan@navidrome.org> * feat: refactor plugin config parsing and add unit tests Signed-off-by: Deluan <deluan@navidrome.org> * feat: add config validation error message in Portuguese * feat: enhance AlwaysExpandedArrayLayout with description support and improve array control testing Signed-off-by: Deluan <deluan@navidrome.org> * feat: update Discord Rust plugin configuration to use JSONForm for user tokens and enhance schema validation Signed-off-by: Deluan <deluan@navidrome.org> * fix: resolve React Hooks linting issues in plugin UI components * Apply suggestions from code review Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * format code Signed-off-by: Deluan <deluan@navidrome.org> * feat: migrate schema validation to use santhosh-tekuri/jsonschema and improve error formatting Signed-off-by: Deluan <deluan@navidrome.org> * address PR comments Signed-off-by: Deluan <deluan@navidrome.org> * fix flaky test Signed-off-by: Deluan <deluan@navidrome.org> * feat: enhance array layout and configuration handling with AJV defaults Signed-off-by: Deluan <deluan@navidrome.org> * feat: implement custom tester to exclude enum arrays from AlwaysExpandedArrayLayout Signed-off-by: Deluan <deluan@navidrome.org> * feat: add error boundary for schema rendering and improve error messages Signed-off-by: Deluan <deluan@navidrome.org> * feat: refine non-enum array control logic by utilizing JSONForms schema resolution Signed-off-by: Deluan <deluan@navidrome.org> * feat: add error styling to ToggleEnabledSwitch for disabled state Signed-off-by: Deluan <deluan@navidrome.org> * feat: adjust label positioning and styling in SchemaConfigEditor for improved layout Signed-off-by: Deluan <deluan@navidrome.org> * feat: implement outlined input controls renderers to replace custom fragile CSS Signed-off-by: Deluan <deluan@navidrome.org> * feat: remove margin from last form control inside array items for better spacing Signed-off-by: Deluan <deluan@navidrome.org> * feat: enhance AJV error handling to transform required errors for field-level validation Signed-off-by: Deluan <deluan@navidrome.org> * feat: set default value for User Tokens in manifest.json to improve user experience Signed-off-by: Deluan <deluan@navidrome.org> * format Signed-off-by: Deluan <deluan@navidrome.org> * feat: add margin to outlined input controls for improved spacing Signed-off-by: Deluan <deluan@navidrome.org> * feat: remove redundant margin rule for last form control in array items Signed-off-by: Deluan <deluan@navidrome.org> * feat: adjust font size of label elements in SchemaConfigEditor for improved readability Signed-off-by: Deluan <deluan@navidrome.org> --------- Signed-off-by: Deluan <deluan@navidrome.org> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
87 lines
2.5 KiB
Go
87 lines
2.5 KiB
Go
package internal
|
|
|
|
import (
|
|
_ "embed"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/santhosh-tekuri/jsonschema/v6"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// XTP JSONSchema specification, from
|
|
// https://raw.githubusercontent.com/dylibso/xtp-bindgen/5090518dd86ba5e734dc225a33066ecc0ed2e12d/plugin/schema.json
|
|
//
|
|
//go:embed xtp_schema.json
|
|
var xtpSchemaJSON string
|
|
|
|
// ValidateXTPSchema validates that the generated schema conforms to the XTP JSONSchema specification.
|
|
// Returns nil if valid, or an error with validation details if invalid.
|
|
func ValidateXTPSchema(generatedSchema []byte) error {
|
|
// Parse the YAML schema to JSON for validation
|
|
var schemaDoc map[string]any
|
|
if err := yaml.Unmarshal(generatedSchema, &schemaDoc); err != nil {
|
|
return fmt.Errorf("failed to parse generated schema as YAML: %w", err)
|
|
}
|
|
|
|
// Parse the XTP schema JSON
|
|
var xtpSchema any
|
|
if err := json.Unmarshal([]byte(xtpSchemaJSON), &xtpSchema); err != nil {
|
|
return fmt.Errorf("failed to parse XTP schema: %w", err)
|
|
}
|
|
|
|
// Compile the XTP schema
|
|
compiler := jsonschema.NewCompiler()
|
|
if err := compiler.AddResource("xtp-schema.json", xtpSchema); err != nil {
|
|
return fmt.Errorf("failed to add XTP schema resource: %w", err)
|
|
}
|
|
|
|
schema, err := compiler.Compile("xtp-schema.json")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to compile XTP schema: %w", err)
|
|
}
|
|
|
|
// Validate the generated schema against XTP schema
|
|
if err := schema.Validate(schemaDoc); err != nil {
|
|
return fmt.Errorf("schema validation errors:\n%s", formatValidationErrors(err))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// formatValidationErrors formats jsonschema validation errors into readable strings.
|
|
func formatValidationErrors(err error) string {
|
|
var validationErr *jsonschema.ValidationError
|
|
if !errors.As(err, &validationErr) {
|
|
return fmt.Sprintf("- %s", err.Error())
|
|
}
|
|
|
|
var errs []string
|
|
collectValidationErrors(validationErr, &errs)
|
|
|
|
if len(errs) == 0 {
|
|
return fmt.Sprintf("- %s", validationErr.Error())
|
|
}
|
|
return strings.Join(errs, "\n")
|
|
}
|
|
|
|
// collectValidationErrors recursively collects leaf validation errors.
|
|
func collectValidationErrors(err *jsonschema.ValidationError, errs *[]string) {
|
|
if len(err.Causes) > 0 {
|
|
for _, cause := range err.Causes {
|
|
collectValidationErrors(cause, errs)
|
|
}
|
|
return
|
|
}
|
|
|
|
// Leaf error - format with location if available
|
|
msg := err.Error()
|
|
if len(err.InstanceLocation) > 0 {
|
|
location := strings.Join(err.InstanceLocation, "/")
|
|
msg = fmt.Sprintf("%s: %s", location, msg)
|
|
}
|
|
*errs = append(*errs, fmt.Sprintf("- %s", msg))
|
|
}
|