gpt4 book ai didi

javascript - ajv - 确保给定的对象结构与自定义类型的结构相匹配

转载 作者:行者123 更新时间:2023-12-05 05:40:13 26 4
gpt4 key购买 nike

我将 ajv 与 TypeScript 一起使用,并且有一个自定义类型 MyCustomType。创建验证架构时,我想确保特定属性的类型为 MyCustomType。因此 ajv 应该验证其结构并决定是否可以将其解析为给定类型。

我从以下示例代码开始(并创建了一个 Codesandbox example 用于测试目的)

import { AType } from "./AType"; // custom type
import { AnInterface } from "./AnInterface"; // custom interface
import Ajv from "ajv";

type MyComplexType = AType | AnInterface; // create a more complex type from the given imports

const ajvInstance = new Ajv();

const schema = {
type: "object",
properties: {
myComplexType: { type: "string" } // value structure must match structure of MyComplexType
},
required: ["myComplexType"],
additionalProperties: false
};

const validate = ajvInstance.compile(schema);

const data = {
myComplexType: {}
};

const isValid = validate(data);

if (isValid) {
console.info("everything is fine");
} else {
validate.errors?.forEach((error) => console.error(error.message));
}

目前我不知道如何为属性 myComplexType 创建验证模式。我发现了一些讨论

但我认为这些不会有帮助,因为

  • typeof 只返回 "object"
  • instanceof 不适用于类型

那么我是否必须创建一个自定义关键字(如 here 所述)并编写我自己的验证逻辑(检查对象结构),或者是否有任何我已经可以使用的东西?我应该如何配置 myComplexType

最佳答案

我将您的结构扁平化,以便能够用一个代码段来回答。这是 TypeScript,并使用 Ajv 在 NODE JS 上成功运行。

关键是使用来自 Ajv 的 JSONSchemaType 连接 Ajv 和 TypeScript。如果 TYPE 和 AJV 定义不匹配,TypeScript 会报错。这很棒,特别是如果您的编辑器内置了 TypeScript 实时检查器(VS 代码、VIM 等)

请注意,架构 MyComplexTypeSchema 是使用先前定义的架构定义的。它不必是一个巨大的嵌套模式。为每个类型和接口(interface)创建一个架构,并从头开始构建您的库以同时支持 TypeScript 和 Ajv。

'use strict'
import Ajv, {JSONSchemaType} from "ajv"
const ajvInstance = new Ajv();

type AType = "x" | "y";
const ATypeSchema : JSONSchemaType<AType> = {
type:"string",
enum:["x","y"]
}

interface AnInterface {
prop: string;
}
const AnInterfaceSchema : JSONSchemaType<AnInterface> = {
type:"object",
properties:{
prop:{type:"string"}
},
required:["prop"]
}

type MyComplexType = AType | AnInterface;
const MyComplexTypeSchema : JSONSchemaType<MyComplexType> = {
oneOf:[ ATypeSchema,AnInterfaceSchema ]
}

const schema = {
type: "object",
properties: {
myComplexType: MyComplexTypeSchema
},
required: ["myComplexType"],
additionalProperties: false
};

const validate = ajvInstance.compile(schema);

const data1 = {
myComplexType: {
prop:"a string"
}
};

var isValid = validate(data1);

if (isValid) {
console.info("everything is fine in data1");
} else {
validate.errors?.forEach((error) => console.error(error.message));
}

const data2 = {
myComplexType: "x"
};

var isValid = validate(data2);

if (isValid) {
console.info("everything is fine in data2");
} else {
validate.errors?.forEach((error) => console.error(error.message));
}

const bad1 = {
myComplexType: {}
};

var isValid = validate(bad1);

if (isValid) {
console.info("everything is fine in bad1");
} else {
validate.errors?.forEach((error) => console.error("bad1: "+error.message));
}


const bad2 = {
myComplexType: "z"
};

var isValid = validate(bad2);

if (isValid) {
console.info("everything is fine in bad2");
} else {
validate.errors?.forEach((error) => console.error("bad2: "+error.message));
}

我单独留下了你的“const schema”代码,但它可能应该像下面这样,以与其他类型/模式结构保持一致。

type TopType = {
myComplexType : MyComplexType
}

const TopTypeSchema : JSONSchemaType<TopType> = {
type: "object",
properties: {
myComplexType: MyComplexTypeSchema
},
required: ["myComplexType"],
additionalProperties: false
};

const validate2 = ajvInstance.compile(TopTypeSchema);

// TypeScript will not let us build a BAD value if the type is defined
const data1:TopType = {
myComplexType: {
prop:"a string"
}
};

关于javascript - ajv - 确保给定的对象结构与自定义类型的结构相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72450184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com