noTopLevelLiterals
此内容尚不支持你的语言。
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/noTopLevelLiterals - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
json/top-level-interop
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noTopLevelLiterals": "error" } } }}Description
Section titled “Description”Require the JSON top-level value to be an array or object.
The JSON specification technically allows any JSON value (object, array, string, number, boolean, or null) to be used as the top-level element of a JSON document. However, some older JSON parsers, especially those created before RFC 7158/4627 was fully adopted, only support objects or arrays as the root element.
Additionally, some security practices (such as those preventing JSON hijacking attacks) rely on the assumption that the top-level value is an object or array. Using an object or array at the top level also provides better extensibility for your data structures over time.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”"just a string"code-block.json:1:1 lint/nursery/noTopLevelLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected the top-level value to be an array or object.
> 1 │ “just a string”
│ ^^^^^^^^^^^^^^^
2 │
ℹ Some JSON parsers only support objects or arrays as the root element. Wrap your value in an array or object to ensure compatibility.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
42code-block.json:1:1 lint/nursery/noTopLevelLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected the top-level value to be an array or object.
> 1 │ 42
│ ^^
2 │
ℹ Some JSON parsers only support objects or arrays as the root element. Wrap your value in an array or object to ensure compatibility.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
truecode-block.json:1:1 lint/nursery/noTopLevelLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected the top-level value to be an array or object.
> 1 │ true
│ ^^^^
2 │
ℹ Some JSON parsers only support objects or arrays as the root element. Wrap your value in an array or object to ensure compatibility.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
nullcode-block.json:1:1 lint/nursery/noTopLevelLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected the top-level value to be an array or object.
> 1 │ null
│ ^^^^
2 │
ℹ Some JSON parsers only support objects or arrays as the root element. Wrap your value in an array or object to ensure compatibility.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
{ "property": "value", "otherProperty": 123}["element", "anotherElement"]{}[]Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.