-
Notifications
You must be signed in to change notification settings - Fork 41
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
Rule Type schema validation: change library and apply defaults #4953
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import ( | |
"fmt" | ||
"strings" | ||
|
||
"github.com/xeipuuv/gojsonschema" | ||
"github.com/santhosh-tekuri/jsonschema/v6" | ||
|
||
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" | ||
) | ||
|
@@ -18,32 +18,32 @@ type RuleValidator struct { | |
ruleTypeName string | ||
|
||
// schema is the schema that this rule type must conform to | ||
schema *gojsonschema.Schema | ||
schema *jsonschema.Schema | ||
// paramSchema is the schema that the parameters for this rule type must conform to | ||
paramSchema *gojsonschema.Schema | ||
paramSchema *jsonschema.Schema | ||
} | ||
|
||
// NewRuleValidator creates a new rule validator | ||
func NewRuleValidator(rt *minderv1.RuleType) (*RuleValidator, error) { | ||
// Load schemas | ||
schemaLoader := gojsonschema.NewGoLoader(rt.Def.RuleSchema) | ||
schema, err := gojsonschema.NewSchema(schemaLoader) | ||
// Create a new schema compiler | ||
// Compile the main rule schema | ||
mainSchema, err := compileSchema(rt.GetDef().GetRuleSchema().AsMap()) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot create json schema: %w", err) | ||
} | ||
|
||
var paramSchema *gojsonschema.Schema | ||
// Compile the parameter schema if it exists | ||
var paramSchema *jsonschema.Schema | ||
if rt.Def.ParamSchema != nil { | ||
paramSchemaLoader := gojsonschema.NewGoLoader(rt.Def.ParamSchema) | ||
paramSchema, err = gojsonschema.NewSchema(paramSchemaLoader) | ||
paramSchema, err = compileSchema(rt.GetDef().GetParamSchema().AsMap()) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot create json schema for params: %w", err) | ||
} | ||
} | ||
|
||
return &RuleValidator{ | ||
ruleTypeName: rt.Name, | ||
schema: schema, | ||
schema: mainSchema, | ||
paramSchema: paramSchema, | ||
}, nil | ||
} | ||
|
@@ -57,7 +57,7 @@ func (r *RuleValidator) ValidateRuleDefAgainstSchema(contextualProfile map[strin | |
Err: err.Error(), | ||
} | ||
} | ||
|
||
applyDefaults(r.schema, contextualProfile) | ||
return nil | ||
} | ||
|
||
|
@@ -67,36 +67,55 @@ func (r *RuleValidator) ValidateParamsAgainstSchema(params map[string]any) error | |
if r.paramSchema == nil { | ||
return nil | ||
} | ||
|
||
if err := validateAgainstSchema(r.paramSchema, params); err != nil { | ||
return &RuleValidationError{ | ||
RuleType: r.ruleTypeName, | ||
Err: err.Error(), | ||
} | ||
} | ||
|
||
applyDefaults(r.paramSchema, params) | ||
return nil | ||
} | ||
|
||
func validateAgainstSchema(schema *gojsonschema.Schema, obj map[string]any) error { | ||
documentLoader := gojsonschema.NewGoLoader(obj) | ||
result, err := schema.Validate(documentLoader) | ||
if err != nil { | ||
return fmt.Errorf("cannot validate json schema: %s", err) | ||
func compileSchema(schemaData interface{}) (*jsonschema.Schema, error) { | ||
compiler := jsonschema.NewCompiler() | ||
if err := compiler.AddResource("schema.json", schemaData); err != nil { | ||
return nil, fmt.Errorf("invalid schema: %w", err) | ||
} | ||
return compiler.Compile("schema.json") | ||
} | ||
|
||
if !result.Valid() { | ||
return buildValidationError(result.Errors()) | ||
func validateAgainstSchema(schema *jsonschema.Schema, obj map[string]any) error { | ||
if err := schema.Validate(obj); err != nil { | ||
return buildValidationError(err.(*jsonschema.ValidationError).Causes) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to worry that this error might have some other type? (And, does this need to be a separate method?) |
||
} | ||
|
||
return nil | ||
} | ||
|
||
func buildValidationError(errs []gojsonschema.ResultError) error { | ||
func buildValidationError(errs []*jsonschema.ValidationError) error { | ||
problems := make([]string, 0, len(errs)) | ||
for _, desc := range errs { | ||
problems = append(problems, desc.String()) | ||
problems = append(problems, desc.Error()) | ||
} | ||
|
||
return fmt.Errorf("invalid json schema: %s", strings.TrimSpace(strings.Join(problems, "\n"))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In all honesty, I don't remember. It may not be needed anymore. |
||
} | ||
|
||
// applyDefaults recursively applies default values from the schema to the object. | ||
func applyDefaults(schema *jsonschema.Schema, obj map[string]any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wish the library had this function, but it looks like it doesn't. 😞 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No library I found has one 😢 |
||
for key, def := range schema.Properties { | ||
// If the key does not exist in obj, apply the default value from the schema if present | ||
if _, exists := obj[key]; !exists && def.Default != nil { | ||
obj[key] = *def.Default | ||
} | ||
|
||
// If def has properties, apply defaults to the nested object | ||
if def.Properties != nil { | ||
o, ok := obj[key].(map[string]any) | ||
if !ok { | ||
// cannot apply defaults to non-object types | ||
continue | ||
} | ||
applyDefaults(def, o) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can
compileSchema
return anil
schema if theParamSchema
is empty?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In particular, that could make this: