networknt / react-schema-form

react form based on json schema for form generation and validation
MIT License
358 stars 96 forks source link

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

Open innomerphey opened 11 months ago

innomerphey commented 11 months ago

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.