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

Bug: Incorrect Handling of schema.minimum When Set to Zero in stdFormObj Method #255

Open
innomerphey opened this issue Oct 4, 2023 · 0 comments

Comments

@innomerphey
Copy link

innomerphey commented Oct 4, 2023

Description

There seems to be an issue with the stdFormObj method when handling cases where schema.minimum is set to zero. This bug might lead to undesired behaviors in scenarios where the zero minimum boundary is a valid or expected input for the schema.

Affected Method

Here is the method where the bug has been identified:

  options = options || {}
  const f =
    options.global && options.global.formDefaults
      ? cloneDeep(options.global.formDefaults)
      : {}
  if (options.global && options.global.supressPropertyTitles === true) {
    f.title = schema.title
  } else {
    f.title = schema.title || name
  }

  if (schema.description) {
    f.description = schema.description
  }
  if (options.required === true || schema.required === true) {
    f.required = true
  }
  if (schema.maxLength) {
    f.maxlength = schema.maxLength
  }
  if (schema.minLength) {
    f.minlength = schema.minLength
  }
  if (schema.readOnly || schema.readonly) {
    f.readonly = true
  }
  if (schema.minimum) {
    f.minimum = schema.minimum + (schema.exclusiveMinimum ? 1 : 0)
  }
  if (schema.maximum) {
    f.maximum = schema.maximum - (schema.exclusiveMaximum ? 1 : 0)
  }

  // Non standard attributes (DONT USE DEPRECATED)
  // If you must set stuff like this in the schema use the x-schema-form attribute
  if (schema.validationMessage) {
    f.validationMessage = schema.validationMessage
  }
  if (schema.enumNames) {
    f.titleMap = canonicalTitleMap(schema.enumNames, schema.enum)
  }
  f.schema = schema

  return f
}

Expected Behavior

When schema.minimum is set to zero, the f.minimum should correctly be assigned the value of zero, respecting the schema.exclusiveMinimum condition if provided.

Current Behavior

Due to the conditional check if (schema.minimum), the logic inside the if block will not execute if schema.minimum is zero since zero is a falsy value in JavaScript. Therefore, f.minimum will not be set correctly in these cases.

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

No branches or pull requests

1 participant