简体   繁体   English

验证在 Typescript 中具有动态密钥的 Json 架构

[英]Validate Json Schema which have Dynamic Keys in Typescript

I have a folder 'schemas' which contains different JSON files to store different schemas.我有一个文件夹“模式”,其中包含不同的 JSON 文件来存储不同的模式。

For example,例如,

/schemas/apple-schema.json /schemas/apple-schema.json

{
  "$schema": "http://json-schema.org/draft-06/schema",
  "type": "object",
  "properties": {
    "apple_name": {
      "type": "string"
    },
    "id": {
      "type": "integer"
    },
    "apple_weight": {
      "type": "number"
    },
    "timestamp": {
      "type": "string",
      "format": "date-time"
    },
    "required": ["id"]
  }
}

/schemas/mango-schema.json /schemas/mango-schema.json

{
  "$schema": "http://json-schema.org/draft-06/schema",
  "type": "object",
  "properties": {
    "mango_name": {
      "type": "string"
    },
    "id": {
      "type": "integer"
    },
    "mango_mature": {
      "type": "number"
    },
    "mango_age": {
      "type": "number"
    },
    "mango_timestamp": {
      "type": "string",
      "format": "date-time"
    },
    "required": ["id"]
  }
}

Different schemas have different keys.不同的模式有不同的键。 What I want to validate is below:我要验证的内容如下:

  1. Keys (eg apple_name, id, timestamp, mango_name, mango_mature, mango_age and etc) among all schemas are following the same naming convention (lowercase with an underscore: 'xxx' or 'xxx_yyy').所有模式中的键(例如 apple_name、id、timestamp、mango_name、mango_mature、mango_age 等)遵循相同的命名约定(带有下划线的小写字母:'xxx' 或 'xxx_yyy')。

  2. Any key whose name contains 'timestamp' should be in format 'date-time'名称中包含“时间戳”的任何键都应采用“日期时间”格式

  3. Any schema should exist key 'id'.任何模式都应该存在键“id”。 (Key 'id' is required for all schemas) (所有模式都需要键“id”)

Is it possible to write a unit test which imports all JSON schemas and handle the validations?是否可以编写一个单元测试来导入所有 JSON 模式并处理验证?

You need a JSON Schema Validator like Ajv to do the schema validation at runtime instead of the features of TypeScript.您需要像Ajv这样的 JSON Schema Validator 来在运行时进行模式验证,而不是使用 TypeScript 的功能。

You may want to write some type definitions for these fruits schema to help you coding under the type safety situation.您可能想为这些水果模式编写一些类型定义,以帮助您在类型安全的情况下进行编码。

TypeScript provides static type checking, which stripped away at compile-time and do not exist at runtime. TypeScript 提供 static 类型检查,它在编译时被剥离并且在运行时不存在。 So you can't check the type at runtime.所以你不能在运行时检查类型。


According to your requirement:根据您的要求:

  1. Validating name convention验证名称约定

You can do it by a regular expression.您可以通过正则表达式来完成。

  1. Any schema should exist key 'id'任何模式都应该存在键“id”

Here is something TypeScript can offer you:这是 TypeScript 可以为您提供的:

interface Schema {
  id: number;
}

interface AppleSchema extends Schema {
  apple_name: string,
  apple_weight: number,
  // rest properties...
}

interface MongoSchema extends Schema {
  mango_name: string,
  mango_mature: number,
  // rest properties...
}

// enjoy the power of TypeScript
export function testApple(apple: AppleSchema) {
  console.log(apple.id); // now you can access apple.id, apple.apple_name, apple.apple_weight ...
}

// even more
export function findFruit<T extends Schema>(fruits: T[], id: number) {
    return fruits.find(fruit => fruit.id === id)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM