Skip to content

Commit

Permalink
Add is-json-value
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jul 24, 2022
1 parent 0a90e71 commit 4dfba63
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
38 changes: 30 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"indent-string": "^5.0.0",
"inquirer": "^9.0.2",
"is-interactive": "^2.0.0",
"is-json-value": "^1.0.1",
"is-plain-obj": "^4.1.0",
"is-unicode-supported": "^1.2.0",
"js-yaml": "^4.0.0",
Expand Down
17 changes: 5 additions & 12 deletions src/config/normalize/validate/json.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { isDeepStrictEqual } from 'util'
import isJsonValue from 'is-json-value'

// Validate that a value is JSON-compatible
export const validateJson = function (value) {
if (!isJson(value)) {
throw new Error(
'must only contain strings, numbers, booleans, nulls, arrays or plain objects.',
)
}
}
const warnings = isJsonValue(value)

const isJson = function (value) {
try {
return isDeepStrictEqual(JSON.parse(JSON.stringify(value)), value)
} catch {
return false
if (warnings.length !== 0) {
const [{ message }] = warnings
throw new Error(`must be valid JSON.\n${message}`)
}
}

0 comments on commit 4dfba63

Please sign in to comment.