scramble icon indicating copy to clipboard operation
scramble copied to clipboard

File properties min/max values incorrectly display as characters

Open willvincent opened this issue 9 months ago • 4 comments

Given this validation rule:

[
  'logo' => ['required', 'file', 'mimes:jpg,jpeg,png,gif', 'max:4096'],
]

It can be reasonably assumed that generated docs should display the max value as 4096KB or 4MB but the output shows as 4096 characters:

Image

willvincent avatar Mar 09 '25 20:03 willvincent

Great catch!

Do you have ideas how the proper JSON schema should look like to describe the file size? I couldn't find the answer

romalytvynenko avatar Mar 10 '25 18:03 romalytvynenko

Good question. Looking at the stoplight docs, it would seem there isn't a better option than the maxLength value that's already being populated. Maybe this is a stoplight issue. 🤷‍♂️

willvincent avatar Mar 10 '25 21:03 willvincent

Digging into their json-schema-viewer repo, it looks like these are the supported validation formatters:

const numberValidationFormatters: Record<string, (value: unknown) => string> = {
  minimum: value => `>= ${value}`,
  exclusiveMinimum: value => `> ${value}`,
  minItems: value => `>= ${value} items`,
  minLength: value => `>= ${value} characters`,
  maximum: value => `<= ${value}`,
  exclusiveMaximum: value => `< ${value}`,
  maxItems: value => `<= ${value} items`,
  maxLength: value => `<= ${value} characters`,
};

So, I guess it should be maximum instead of maxLength ? I wonder if the value has to be a number, or if it could be written as like "maximum": "4MB"

willvincent avatar Mar 10 '25 21:03 willvincent

maximum doesn't render at all.

I think it would probably be best to omit the min/max validation rules for file validation. Best scenario would be to include any mime type detail and file size limits in a description

While the OpenAPI spec supports defining mime types for file uploads stoplight doesn't render them, as far as I can tell, nor does it render maximum as above might be expected..

So, yeah, given a validation rule like this:

'logo' => ['required', 'file', 'mimes:jpg,jpeg,png,gif', 'max:4096']

I think the best case scenario would be to render something like this:

"logo": {
    "type": "string",
    "format": "binary",
    "contentMediaType": "application\/octet-stream",
    "description": "Files must be jpg,jpeg,png,gif <= 4096 KB"
},

Not sure how reasonable that is, the quickest solution is to simply omit minLength and maxLength on a file/image validation's output

willvincent avatar Mar 10 '25 21:03 willvincent