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

Validation: property fields marked as required are not checked #1407

Open
barneyferry opened this issue Feb 19, 2025 · 2 comments
Open

Validation: property fields marked as required are not checked #1407

barneyferry opened this issue Feb 19, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@barneyferry
Copy link

What version of ogen are you using?

v1.10.0

Can this issue be reproduced with the latest version?

Yes

What did you do?

Enable client/request/validation

Write a schema with a request object with a required property.

Test:
  required: [name]
  properties:
    name:
      type: string
    description:
      type: string
    data_type:
      type: string
      enum: [one, two, three]

What did you expect to see?

A validator function that checks for name being present in oas_validators_gen.go

What did you see instead?

No such validator was generated. I know validators are working as I am seeing some code being generated for validating the values of enum types but nothing for required fields.

func (s *Test) Validate() error {
	if s == nil {
		return validate.ErrNilPointer
	}

	var failures []validate.FieldError
	if err := func() error {
		if err := s.DataType.Validate(); err != nil {
			return err
		}
		return nil
	}(); err != nil {
		failures = append(failures, validate.FieldError{
			Name:  "data_type",
			Error: err,
		})
	}
	if len(failures) > 0 {
		return &validate.Error{Fields: failures}
	}
	return nil
}
@barneyferry barneyferry added the bug Something isn't working label Feb 19, 2025
@tdakkota
Copy link
Member

name wolud be present in resulting JSON as empty string. If you want name to be present AND be non-empty, you should add minLength: 1 validator.

@barneyferry
Copy link
Author

The problem is, when I specify required: [name] I would expect that if I sent a payload to the server that did not contain a name field at all it should return a 400 bad request. I can fix this with a minLength which works for my use-case, but other validators I've used rely on the required field only, if I am happy to accept "" as an input as long as the field is there.

I'll use minLength for now as this project as few schemas but a larger product I work on that has a very large hand-written schema I am considering moving from oapi-codegen to ogen and this would require specifying minLength on around a thousand fields that already have required specified to keep it consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants