@ark/json-sema
这是什么?
@ark/json-schema 是一个允许从 JSON Schema chema 转换为 ArkType 类型的软件包。 例如:
import { jsonSchemaToType } from "@ark/json-schema"
const T = jsonSchemaToType({ type: "string", minLength: 5, maxLength: 10 })
等于:
import { type } from "arktype"
const T = type("5<=string<=10")
这使得目前以JSON Schema为基础的运行时验证在其代码库中的人能够轻松地采用ArkType.
如果您想要转换您的已存在的 ArkType Type到JSON Schema 你不需要这个图书馆.
相反,使用内置 toJsonSchema() 每页中存在的方法 Type,例如:
import { type } from "arktype"
// { type: "string", minLength: 5, maxLength: 10 }
const schema = type("5<=string<=10").toJsonSchema()
额外类型安全
如果你想确保你的JSON Schema计划有效,你也可以这样做! 只需导入相关的 Schema 类型从 @ark/json-schema 像这样:
import type { JsonSchema } from "arktype"
const integerSchema: JsonSchema.Numeric = {
type: "integer",
multipleOf: "3" // errors stating that 'multipleOf' must be a number
}
请注意,对于字符串计划,您必须从 @ark/json-schema 改为 arktype。这是因为 @ark/json-schema 还不支持 format 关键词同时 arktype 已经.
import type { StringSchema } from "@ark/json-schema"
const stringSchema: StringSchema = {
type: "string",
minLength: "3" // errors stating that 'minLength' must be a number
}
限制
- 没有
dependencies支助 - 没有
if编号 :else编号 :then支助 multipleOf只支持整数