> ## Documentation Index
> Fetch the complete documentation index at: https://docs.projectreal.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# json.validate

> 通过 parse 一个 complete JSON value 来验证 JSON string。success 时只返回 true。parse failure 时返回 false 以及 parser error details。

<div className="real-function-page">
  ```lua theme={null}
  json.validate(json: string, options?: DecodeOptions): boolean | string | nil | number | nil
  ```

  ## 参数

  <ParamField path={"json"} type={"string"} required>
    要 validate 的 JSON string。function 使用 string type checking 读取此 argument。
  </ParamField>

  <ParamField path={"options"} type={"DecodeOptions"}>
    Optional decode options。boolean 设置 useNull。table 可以设置 useNull 和 maxDepth。maxDepth 会 clamp 到 1 到 4096。
  </ParamField>

  ## 返回值

  <ResponseField name={"valid"} type={"boolean"}>
    当一个 complete JSON value parsing 成功时为 true。当 parsing 在 protected parse block 中失败时为 false。
  </ResponseField>

  <ResponseField name={"errorMessage"} type={"string | nil"}>
    parse failure 时的 Error message。message 使用 json.validate 作为 prefix，并包含 near-position suffix。此 return 只在 failure 时存在。
  </ResponseField>

  <ResponseField name={"errorPosition"} type={"number | nil"}>
    parse failure 时来自 parser 的 Parser byte position。此 return 只在 failure 时存在。
  </ResponseField>

  ## 类型

  ### DecodeOptions

  控制 JSON 解析行为。

  | Name       | Type      | Required | 说明                                 |
  | ---------- | --------- | -------- | ---------------------------------- |
  | `useNull`  | `boolean` | No       | 使用库的 null 标记而不是 nil 来表示 JSON null。 |
  | `maxDepth` | `integer` | No       | 设置最大嵌套深度，范围为 1 到 4096。             |

  ## 示例

  检查 string 是否正好包含一个 complete JSON value。

  ```lua theme={null}
  print(json.validate('{"value":42}'))

  local valid, message, position = json.validate('{"value":42 trailing')

  if not valid then
      warn(message, position)
  end
  ```
</div>
