networknt / networknt/react-schema-form

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

Open
#255 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
356
Forks
94
Avg merge
19h 36m
Merged PRs (30d)
4

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate the stdFormObj method in the repository and inspect how schema.minimum is transferred to the form object. Verify behavior for a minimum of zero, including the exclusiveMinimum condition, and confirm that f.minimum is assigned correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.