标签: json-schema-validator

JSON模式中“ $ id”属性的用法

我正在使用JSON模式验证数据。

我认为使用保留关键字$ id可能会使我的模式出现错误。该字段的目的是指定另一个平台上该属性的REMOTE ID。因此,这就是“来源ID”。

您能告诉我$ id是什么吗,如果我犯了一个严重错误并且此值需要更改。因为在文档中我找到了这个定义:

如果存在,则此关键字的值必须为字符串,并且必须表示有效的URI引用[RFC3986]。这个值应该被规范化,并且不应该是一个空的片段<#>或一个空的字符串<>。

json jsonschema json-schema-validator

6
推荐指数
2
解决办法
4361
查看次数

如何使用 json 模式验证器验证可为空类型?

我正在使用play-json-schema-validator并希望使用 scala 设置集成测试以检查 API 的 JSON 响应模式。

响应的某些字段可以为空,我想对此进行验证。所以某些字段可以是字符串或空值,但它永远不能是数字。

它的操场上玩耍我想验证一个对象数组,每个对象的name属性是字符串或空值。

我想出了这个模式:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Product set",
  "type": "array",
  "items": {
    "title": "Product",
    "type": "object",
    "properties": {
      "name": {
        "type": ["string", null]
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然而,尽管它验证了字符串和 null 大小写,但我现在得到了数字的误报。我期待这个 json 出现错误,但它验证了:

[
  {
    "name": "Red anger"
  },
  {
    "name": null
  },
  {
    "name": 13
  }
]
Run Code Online (Sandbox Code Playgroud)

如何使用模式验证器将类型的字段声明为可为空?

null json scala playframework json-schema-validator

6
推荐指数
1
解决办法
6705
查看次数

Json 架构验证错误

我在验证 JSON 时遇到一些错误。我无法理解这些错误,任何人都可以帮忙解释一下。

{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Modified JSON Schema draft v4 that includes the optional '$ref' and 'format'",
"definitions": {
    "schemaArray": {
        "type": "array",
        "minItems": 1,
        "items": { "$ref": "#" }
    },
    "positiveInteger": {
        "type": "integer",
        "minimum": 0
    },
    "positiveIntegerDefault0": {
        "allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
    },
    "simpleTypes": {
        "enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
    },
    "stringArray": {
        "type": "array",
        "items": { "type": "string" },
        "minItems": 1,
        "uniqueItems": true …
Run Code Online (Sandbox Code Playgroud)

validation schema json jsonschema json-schema-validator

6
推荐指数
1
解决办法
4万
查看次数

针对 swagger 文件验证 json 有效负载 - json-schema-validator

我正在尝试针对包含服务协议的 swagger 文件验证 json 有效负载。我正在使用 json-schema-validator(2.1.7) 库来实现这一点,但目前它没有针对指定的模式或最小/最大长度进行验证。

Java代码:

public void validateJsonData(final String jsonData) throws IOException, ProcessingException {

    ClassLoader classLoader = getClass().getClassLoader();
    File jsonSchemaFile = new File (classLoader.getResource("coachingStatusUpdate.json").getFile());

    String jsonSchema = new String(Files.readAllBytes(jsonSchemaFile.toPath()));

    final JsonNode dataNode = JsonLoader.fromString(jsonData);
    final JsonNode schemaNode = JsonLoader.fromString(jsonSchema);

    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    JsonValidator jsonValidator = factory.getValidator();

    ProcessingReport report = jsonValidator.validate(schemaNode, dataNode);
    System.out.println(report);
    if (!report.toString().contains("success")) {
        throw new ProcessingException (
                report.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在发送的消息

{
  "a": "b",
  "c": "d",
  "e": -1,
  "f": "2018-10-30",
  "g": "string" } …
Run Code Online (Sandbox Code Playgroud)

java json jsonschema swagger json-schema-validator

6
推荐指数
2
解决办法
8204
查看次数

如何让 AJV (v5.0.0) 解析 $schema?

使用 AJV 编译 JSON 模式时,我无法在 JSON 模式的顶部解析这一行: "$schema": "node_modules/ajv/lib/refs/json-schema-draft-04#"

我也尝试过这条线工作,但无济于事: "$schema": " http://json-schema.org/draft-04/schema# "

(以及上述的许多其他排列。)

无论我输入什么,AJV 都会说:“错误:没有带有密钥或引用的模式...”

这个房产到底需要什么?

谢谢(顺便说一句,AJV 很棒,谢谢。)

json-schema-validator ajv

6
推荐指数
1
解决办法
1217
查看次数

在基于另一个架构对象的 json 架构上使用条件语句

我有一个 json 对象,如:

{
  "session": {
    "session_id": "A",
    "start_timestamp": 1535619633301
  },
  "sdk": {
    "name": "android",
    "version": "21"
  }
}
Run Code Online (Sandbox Code Playgroud)

sdk name可以是android or ios。并且session_id基于name fieldin sdk json。我写了一个json schemausing 条件语句(使用草案 7)如下:

但它以一种意想不到的方式工作:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/Base",
  "definitions": {
    "Base": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "session": {
          "$ref": "#/definitions/Session"
        },
        "sdk": {
          "$ref": "#/definitions/SDK"
        }
      },
      "title": "Base"
    },
    "Session": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "start_timestamp": { …
Run Code Online (Sandbox Code Playgroud)

schema json jsonschema json-schema-validator

6
推荐指数
1
解决办法
2665
查看次数

如何在 AWS::Serverless::Api 的 AWS SAM 模板中添加请求验证器?

我正在尝试使用 AWS SAM 将请求验证器资源链接到 SAM 模板中的无服务器 API。我已经创建了请求验证器并在其 RestApiId 中引用了 API,但验证器没有被设置为 AWS 控制台中的 API 默认验证器选项。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
    Discription for the template
Globals:
  Function:
    Timeout: 30

Resources:
  MyAPI:
    Type: AWS::Serverless::Api
    Properties:
      Name: MyAPI
      StageName: prod
      Auth:
        DefaultAuthorizer: MyAuthorizer
        Authorizers:
            MyAuthorizer:
              FunctionPayloadType: REQUEST
              FunctionArn: here goes the function Arn
              Identity:
                Context:
                  - identity.sourceIp
                ReauthorizeEvery: 60
      Models:
        RequestModel:
          $schema: 'http://json-schema.org/draft-04/mySchema#'
          type: object
          properties:
            Format:
              type: string
            Name:
              type: string
              minLength: 3
            Id:
              type: string
          required:
            - Format
            - Id

  RequestValidator: …
Run Code Online (Sandbox Code Playgroud)

amazon-web-services json-schema-validator aws-api-gateway aws-sam

6
推荐指数
1
解决办法
1651
查看次数

应如何引用 JSON 对象符合的 JSON 架构?

还有另一个与我在这里问的问题类似的问题(您可以在符合 JSON 模式的 JSON 文档上指定模式 URI 吗?),该问题被标记为(如何引用顶级数组的 json 模式)的重复项),但我对这个问题有细微的变化。

虽然 JSON 模式定义 ( https://json-schema.org/ )中似乎没有任何内容,但人们是否遵循关于在 JSON 对象/文档中指示它符合哪个 JSON 模式的最佳实践到(或应该符合)?

在 JSON 对象/文档中使用 $schema 标签引用架构是否错误?看起来引用它所遵循的模式也是“版本”JSON 对象/文档的好方法。

json jsonschema json-schema-validator

6
推荐指数
1
解决办法
1445
查看次数

带有 Fastify 的 ajv-errors 插件给出错误,无法找到模块 'ajv/dist/compile/codegen'

为了提供一些背景知识,我尝试为 AJV JSON 模式验证添加正确的模式验证错误消息格式(以粉饰验证错误)。我正在使用 Fastify 中间件。我的目的是根据我的功能要求将默认架构错误验证消息包装到我自己的消息中,以使其用户友好。

\n

现在,当我使用 Fastify 时,我将其添加为我的插件的一部分,如下所示:

\n
const fastify = require('fastify')({\n    ajv: {\n        customOptions: { allErrors: true, jsonPointers: true },\n        plugins: [\n            require('ajv-merge-patch'),\n            require('ajv-errors'),\n        ]\n    },\n    requestIdHeader: 'x-service-request-id',\n    requestIdLogLabel: 'requestId',\n    genReqId: function (req) { return random.generate(10) }\n});\n
Run Code Online (Sandbox Code Playgroud)\n

我在用

\n
\n

"ajv-errors": "^3.0.0"\n"ajv-merge-patch": "^4.1.0",\n(两者都是来自 npm 的最新版本)

\n
\n

现在我在纱线启动中收到此错误:

\n
\xce\xbb yarn start\nyarn run v1.22.10\nwarning ..\\..\\..\\..\\package.json: No license field\n$ node src/server.js\nnode:internal/modules/cjs/loader:930\n  throw err;\n  ^\n\nError: Cannot find module 'ajv/dist/compile/codegen'\nRequire stack:\n- service-infra\\persistance\\node_modules\\ajv-errors\\dist\\index.js\n- service-infra\\persistance\\src\\server.js\n    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:927:15)\n    at Function.Module._load …
Run Code Online (Sandbox Code Playgroud)

json-schema-validator ajv fastify

6
推荐指数
1
解决办法
2万
查看次数

Ajv:错误:严格模式:未知关键字:“$schema”

使用 ajv v8.6.3 Nodejs 和 TypeScript

尝试解析 JSON 但我得到了这个Error: strict mode: unknown keyword: "$schema"

有人对此有所了解吗?

export const mySchema ={
    "$schema": "http://json-schema.org/draft-07/schema#",
    "properties": {
        "names": {
            "items": {
                "type": "number"
            },
            "type": "array"
        }
    },
    "type": "object"
}


const ajv = new Ajv()

ajv.addMetaSchema(draft7MetaSchema)

ajv.compileParser(mySchema)
Run Code Online (Sandbox Code Playgroud)

json node.js json-schema-validator ajv

6
推荐指数
0
解决办法
1383
查看次数