Skip to content

Commit

Permalink
chore(zhi-common): add json schema validate
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Apr 16, 2023
1 parent fe76924 commit 1cf0f82
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 11 deletions.
1 change: 1 addition & 0 deletions apps/zhi-common/package.json
Expand Up @@ -32,6 +32,7 @@
"tsconfig": "workspace:*"
},
"dependencies": {
"ajv": "^8.12.0",
"zhi-env": "workspace:*",
"zhi-log": "workspace:*"
}
Expand Down
4 changes: 3 additions & 1 deletion apps/zhi-common/src/index.spec.ts
Expand Up @@ -29,7 +29,9 @@ import ZhiCommonUtil from "./lib/ZhiCommonUtil"
import { getNormalizedEnvDefines } from "../../../packages/esbuild-config-custom/esmUtils"

describe("zhi-common", () => {
getNormalizedEnvDefines(["NODE", "VITE_"])
beforeEach(() => {
getNormalizedEnvDefines(["NODE", "VITE_"])
})

it("index", () => {
const logger = ZhiCommonUtil.zhiLog("zhi-common-test")
Expand Down
129 changes: 129 additions & 0 deletions apps/zhi-common/src/lib/jsonUtil.spec.ts
@@ -0,0 +1,129 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import JsonUtil from "./jsonUtil"
import { getNormalizedEnvDefines } from "../../../../packages/esbuild-config-custom/esmUtils"

describe("JsonUtil", () => {
let jsonUtil: JsonUtil

beforeEach(() => {
jsonUtil = new JsonUtil()

getNormalizedEnvDefines(["NODE", "VITE_"])
})

describe("validateJson", () => {
it("should return true when validating valid JSON data against a valid JSON schema", () => {
const jsonSchema = {
type: "object",
properties: {
name: {
type: "string",
},
age: {
type: "number",
},
},
required: ["name", "age"],
}
const jsonData = {
name: "John Doe",
age: 30,
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const result = jsonUtil.validateJson(jsonSchema, jsonData)
expect(result).toBe(true)
})

it("should return false when validating invalid JSON data against a valid JSON schema", () => {
const jsonSchema = {
type: "object",
properties: {
name: {
type: "string",
},
age: {
type: "number",
},
},
required: ["name", "age"],
}
const jsonData = {
name: "John Doe",
age: "30", // age should be a number, not a string
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const result = jsonUtil.validateJson(jsonSchema, jsonData)
expect(result).toBe(false)
})
})

describe("validateObjectSchema", () => {
it("should return true when validating a valid data object against a valid schema object", () => {
const schemaObject = {
type: "object",
properties: {
name: {
type: "string",
},
age: {
type: "number",
},
},
required: ["name", "age"],
}
const dataObject = {
name: "John Doe",
age: 30,
}
const result = jsonUtil.validateObjectSchema(schemaObject, dataObject)
expect(result).toBe(true)
})

it("should return false when validating an invalid data object against a valid schema object", () => {
const schemaObject = {
type: "object",
properties: {
name: {
type: "string",
},
age: {
type: "number",
},
},
required: ["name", "age"],
}
const dataObject = {
name: "John Doe",
age: "30", // age should be a number, not a string
}
const result = jsonUtil.validateObjectSchema(schemaObject, dataObject)
expect(result).toBe(false)
})
})
})
51 changes: 51 additions & 0 deletions apps/zhi-common/src/lib/jsonUtil.ts
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import Ajv, { JSONSchemaType } from "ajv"

/**
* 校验 JSON schema
*
* @author terwer
* @version 1.5.0
* @since 1.5.0
*/
class JsonUtil {
private ajv: Ajv

constructor() {
this.ajv = new Ajv()
}

public validateJson<T>(schema: JSONSchemaType<T>, data: T): boolean {
return this.ajv.validate(schema, data)
}

public validateObjectSchema(schemaObject: object, dataObject: object): boolean {
return this.ajv.validate(schemaObject, dataObject)
}
}

export default JsonUtil
8 changes: 7 additions & 1 deletion apps/zhi-common/src/lib/zhi-common.ts
Expand Up @@ -23,6 +23,8 @@
* questions.
*/

import JsonUtil from "./jsonUtil"

/**
* 平台无关的通用工具类
*
Expand All @@ -31,7 +33,11 @@
* @since 1.3.0
*/
class ZhiCommon {

public readonly jsonUtil

constructor() {
this.jsonUtil = new JsonUtil()
}
}

export default ZhiCommon
5 changes: 4 additions & 1 deletion apps/zhi-env/src/index.spec.ts
Expand Up @@ -29,7 +29,10 @@ import { getNormalizedEnvDefines } from "../../../packages/esbuild-config-custom

describe("zhiEnv", () => {
const NOT_EXIST_KEY = "NOT_EXIST_KEY"
getNormalizedEnvDefines(["NODE", "VITE_"])

beforeEach(() => {
getNormalizedEnvDefines(["NODE", "VITE_"])
})

it("test env", () => {
const env = new Env(import.meta.env)
Expand Down
4 changes: 2 additions & 2 deletions apps/zhi-log/jest.config.cjs
@@ -1,5 +1,5 @@
const sharedConfig = require("jest-config-custom")

module.exports = {
...sharedConfig
}
...sharedConfig,
}
5 changes: 5 additions & 0 deletions apps/zhi-log/src/index.spec.ts
Expand Up @@ -26,8 +26,13 @@
import { describe, it } from "@jest/globals"
import Env from "zhi-env"
import LogFactory, { LogLevelEnum } from "./index"
import { getNormalizedEnvDefines } from "../../../packages/esbuild-config-custom/esmUtils"

describe("zhiLog", () => {
beforeEach(() => {
getNormalizedEnvDefines(["NODE", "VITE_"])
})

it("test default log", function () {
const logger = LogFactory.defaultLogger()
logger.debug("This is debug log")
Expand Down
9 changes: 3 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1cf0f82

Please sign in to comment.