json/no-unsafe-values
Disallow JSON values that are unsafe for interchange
JSON puts no limit on how large or how precise a number is, but most programs read numbers into 64-bit floats. A number that doesn't fit silently changes when the file is read. This rule reports:
- a number that's too large and becomes
Infinity - a number that's too small and becomes
0 - an integer outside the safe range, above 9007199254740991 or below -9007199254740991, which may be rounded to a nearby integer
- a subnormal number, which has fewer digits of precision than it's written with
- a string with a lone surrogate, which can't be encoded as UTF-8
Configure#
Off by default. It needs an ESLint config that says which files are in the language:
eslint.config.js
import json from "@eslint/json";
export default [
{
files: ["**/*.json"],
plugins: { json },
language: "json/json",
rules: { "json/no-unsafe-values": "error" },
},
];It also works with json/jsonc and json/json5.
Examples#
example.json4 problems
{
"large": 1e400The number 1e400 will evaluate to Infinity.,
"small": 1e-400The number 1e-400 will evaluate to zero.,
"id": 9007199254740993The integer 9007199254740993 is outside the safe integer range.,
"subnormal": 2.2250738585072009e-308Unexpected subnormal number 2.2250738585072009e-308 found, which may cause interoperability issues.
}example.json1 problem
{
"emoji": "\ud83d"Lone surrogate \ud83d found.
}example.jsonNo problems
{
"large": 1e300,
"zero": 0.0,
"id": "9007199254740993",
"emoji": "\ud83d\ude00"
}