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

Receiving 'RangeError: invalid ISO 8601 string' via API for Numbers-only Strings #171

Open
obliqueRotationMethod opened this issue Sep 20, 2023 · 2 comments

Comments

@obliqueRotationMethod
Copy link

Hi,

running locally, I POST an pretty big JSON to the API. Some of my Strings in the JSON have Number-Characters only (userIDs).
When opening the link, console is showing a lot of 'RangeError: invalid ISO 8601 string: 12345' - warnings/ errors.
It takes up to one minute to open the link, npm service is running on 100% CPU usage

On the site, I see that the program interprets my Number-Strings as Date-Objects (string/datetime/rfc3339). Example: {"lastModifiedBy": "12345678"}

My actual date strings in there are ISO 8601-formated. Maybe a workaround could be to add a parameter to state the date format. To let the backend know which Strings to parse as dates and leave the rest as normal text?

@CharkeyQK
Copy link

same issue.

@CharkeyQK
Copy link

CharkeyQK commented May 17, 2024

ChatGPT helps:

import { inferType as originalInferType, JSONValueType, JSONIntType, JSONStringType } from '@jsonhero/json-infer-types';

export function inferType(value: unknown): JSONValueType {
  const result: JSONValueType = originalInferType(value) as JSONValueType;

  // check
  if (typeof value === 'number') {
    // if result is JSONStringType and format is datetime,change to JSONIntType
    if (result.name === 'string' && 'format' in result && result.format?.name === 'datetime') {
      return { name: 'int', value } as JSONIntType;
    }

    // return int directly
    return { name: 'int', value } as JSONIntType;
  }

  // another check
  if (result.name === 'string') {
    const stringValue = result.value as string;
    // if string looks like number,and should not as datetime
    if (/^\d+$/.test(stringValue) && result.format?.name === 'datetime') {
      return { name: 'int', value: parseInt(stringValue, 10) } as JSONIntType;
    }
  }

  return result;
}

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

2 participants