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

Rule Type schema validation: change library and apply defaults #4953

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

JAORMX
Copy link
Contributor

@JAORMX JAORMX commented Nov 12, 2024

Summary

This changes the underlying library to something that's actually
maintained: github.com/santhosh-tekuri/jsonschema/v6

It also adds the feature of applying defaults if they were defined in
the JSON schema.

Closes #1657

Change Type

Mark the type of change your PR introduces:

  • Bug fix (resolves an issue without affecting existing features)
  • Feature (adds new functionality without breaking changes)
  • Breaking change (may impact existing functionalities or require documentation updates)
  • Documentation (updates or additions to documentation)
  • Refactoring or test improvements (no bug fixes or new functionality)

Testing

Added a relevant unit test.

Review Checklist:

  • Reviewed my own code for quality and clarity.
  • Added comments to complex or tricky code sections.
  • Updated any affected documentation.
  • Included tests that validate the fix or feature.
  • Checked that related changes are merged.

@JAORMX JAORMX requested a review from a team as a code owner November 12, 2024 15:06
@JAORMX JAORMX force-pushed the refactor-validate-and-defaults branch from 8089c5f to 56b5a4f Compare November 12, 2024 15:13
@coveralls
Copy link

coveralls commented Nov 12, 2024

Coverage Status

coverage: 54.758% (+0.003%) from 54.755%
when pulling a4c37a1 on JAORMX:refactor-validate-and-defaults
into c1c0358 on mindersec:main.

This changes the underlying library to something that's actually
maintained: github.com/santhosh-tekuri/jsonschema/v6

It also adds the feature of applying defaults if they were defined in
the JSON schema.

Signed-off-by: Juan Antonio Osorio <[email protected]>
@JAORMX JAORMX force-pushed the refactor-validate-and-defaults branch from 1e16bb5 to a4c37a1 Compare November 13, 2024 07:51
Copy link
Contributor

@eleftherias eleftherias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to call applyDefaults separately instead of making it part of both validation functions?
Since ValidateRuleDef and ValidateParams are already separate, I expected ApplyDefaults to be separate as well.

@JAORMX
Copy link
Contributor Author

JAORMX commented Nov 13, 2024

Would it make sense to call applyDefaults separately instead of making it part of both validation functions? Since ValidateRuleDef and ValidateParams are already separate, I expected ApplyDefaults to be separate as well.

That might make sense, I just wanted to simplify the API by doing both validation and applying defaults. Else the implementor gets a more verbose setup.

if rt.Def.ParamSchema != nil {
paramSchemaLoader := gojsonschema.NewGoLoader(rt.Def.ParamSchema)
paramSchema, err = gojsonschema.NewSchema(paramSchemaLoader)
paramSchema, err = compileSchema(rt.GetDef().GetParamSchema().AsMap())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can compileSchema return a nil schema if the ParamSchema is empty?

Copy link
Member

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:

paramSchema, err := compileSchema(rt.GetDef().GetParamSchema())
if err != nil {
  ...
}

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)
Copy link
Member

Choose a reason for hiding this comment

The 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 fmt.Errorf("invalid json schema: %s", strings.TrimSpace(strings.Join(problems, "\n")))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why TrimSpace here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

return fmt.Errorf("invalid json schema: %s", strings.TrimSpace(strings.Join(problems, "\n")))
}

// applyDefaults recursively applies default values from the schema to the object.
func applyDefaults(schema *jsonschema.Schema, obj map[string]any) {
Copy link
Member

Choose a reason for hiding this comment

The 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. 😞

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No library I found has one 😢

@evankanderson
Copy link
Member

Would it make sense to call applyDefaults separately instead of making it part of both validation functions? Since ValidateRuleDef and ValidateParams are already separate, I expected ApplyDefaults to be separate as well.

That might make sense, I just wanted to simplify the API by doing both validation and applying defaults. Else the implementor gets a more verbose setup.

It feels like we should do applyDefaults first and then validate, but it should be safe to call applyDefaults as part of validation. The one weird thing is that you would expect validation to be non-mutating, but applyDefaults is clearly mutating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support default values in rule_types
4 participants