Skip to content

Locale dependent error in addRangeCheck() function #95

Description

@michael0710

The generate validator function generates lua code as string object which is then used to validate JSON data.
If the given JSON schema contains a numeric value with range limitation, the function addRangeCheck(...) is called.
As this function uses string.format to generate if statements, the range limit which is given as type number will convert the number to a string. Depending on he active locale, the number is represented with either a comma or a full stop as a decimal separator. If a comma is used as decimal separator, the generated lua code is invalid.

See:

local function addRangeCheck(ctx, op, reference, msg)

A possible fix would be to set a different locale temporarily to make sure a full stop is used as decimal separator, e.g.:

local function addRangeCheck(ctx, op, reference, msg)
  active_locale = os.setlocale(nil)
  os.setlocale("C")
  ctx:stmt(sformat('  if %s %s %s then', ctx:param(1), op, reference))
  ctx:stmt(sformat('    return false, %s("expected %%s to be %s %s", %s)',
                    ctx:libfunc('string.format'), msg, reference, ctx:param(1)))
  ctx:stmt(        '  end')
  os.setlocale(active_locale)
end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions