项目作者: vasuvanka

项目描述 :
Json Validator
高级语言: TypeScript
项目地址: git://github.com/vasuvanka/json-validator.git
创建时间: 2020-04-05T07:58:29Z
项目社区:https://github.com/vasuvanka/json-validator

开源协议:MIT License

下载


json-validator for Node.js / deno

Json Validator - validates a json object against defined schema.

Install

  1. npm install @vasuvanka/json-validator

Docs

Deno Example

  1. import { validate } from "https://deno.land/x/jsonvalidator/index.ts";
  2. const bodySchema = {
  3. name: {
  4. type: String,
  5. },
  6. phone: { type: Number },
  7. isLoggedIn: { type: Boolean },
  8. address: {
  9. line: {
  10. add: [{ type: Number }],
  11. },
  12. street: { type: String },
  13. city: { type: String },
  14. pincode: { type: Number },
  15. },
  16. list: [{ type: String }],
  17. };
  18. const body = {
  19. name: "Hello",
  20. phone: 88010000000,
  21. address: {
  22. line: {
  23. add: [1],
  24. },
  25. street: "streetlk111",
  26. city: "some city",
  27. pincode: 453672,
  28. },
  29. isLoggedIn: false,
  30. list: ["hello", "world"],
  31. };
  32. const error = validate(body, bodySchema, { allowUnknown: true });
  33. console.log(error);
  34. const err = validate(body, bodySchema, { allowUnknown: false });
  35. console.log(err);

Node.js Example

  1. const { validate } = require("@vasuvanka/json-validator");
  2. const bodySchema = {
  3. name: {
  4. type: String,
  5. },
  6. phone: { type: Number },
  7. isLoggedIn: { type: Boolean },
  8. address: {
  9. line: {
  10. add: [{ type: Number }],
  11. },
  12. street: { type: String },
  13. city: { type: String },
  14. pincode: { type: Number },
  15. },
  16. list: [{ type: String }],
  17. };
  18. const body = {
  19. name: "Hello",
  20. phone: 88010000000,
  21. address: {
  22. line: {
  23. add: [1],
  24. },
  25. street: "streetlk111",
  26. city: "some city",
  27. pincode: 453672,
  28. },
  29. isLoggedIn: false,
  30. list: ["hello", "world"],
  31. };
  32. const error = validate(body, bodySchema);
  33. console.log(error);
  34. const err = validate(body, bodySchema, { allowUnknown: false });
  35. console.log(err);

LICENCE

MIT

Free software,hell ya.