Deno.js Tutorial to Check if JSON Object Schema is Valid or Not Using json-validator Library in TypeScript Full Project For Beginners

 

 

schema validator for deno

Json Schema Validator – validates a json object against defined schema.

Docs

Example

 

 

import { validate } from "https://deno.land/x/schema_validator/mod.ts";
const bodySchema = {
  name: {
    type: String,
  },
  phone: { type: Number },
  isLoggedIn: { type: Boolean },
  address: {
    line: {
      add: [{ type: Number }],
    },
    street: { type: String },
    city: { type: String },
    pincode: { type: Number },
  },
  list: [{ type: String }],
};

const body = {
  name: "Hello",
  phone: 88010000000,
  address: {
    line: {
      add: [1],
    },
    street: "streetlk111",
    city: "some city",
    pincode: 453672,
  },
  isLoggedIn: false,
  list: ["hello", "world"],
};
const error = validate(body, bodySchema, { allowUnknown: true });
console.log(error);

const err = validate(body, bodySchema, { allowUnknown: false });
console.log(err);

Leave a Reply