How to use the validate function from jsonschema

Find comprehensive JavaScript jsonschema.validate code examples handpicked from public code repositorys.

54
55
56
57
58
59
60
61
62
63
// arrive as strings from querystring, but we want as int/bool
if (q.minSalary !== undefined) q.minSalary = +q.minSalary;
q.hasEquity = q.hasEquity === "true";

try {
  const validator = jsonschema.validate(q, jobSearchSchema);
  if (!validator.valid) {
    const errs = validator.errors.map(e => e.stack);
    throw new BadRequestError(errs);
  }
fork icon1
star icon1
watch icon1

+ 8 other calls in file

98
99
100
101
102
103
104
105
106
107
108
 * Authorization required: admin
 */


router.patch("/:handle", ensureAdmin, async function (req, res, next) {
  try {
    const validator = jsonschema.validate(req.body, companyUpdateSchema);
    if (!validator.valid) {
      const errs = validator.errors.map(e => e.stack);
      throw new BadRequestError(errs);
    }
fork icon1
star icon1
watch icon1

+ 2 other calls in file

31
32
33
34
35
36
37
38
39
40
  throw new BadRequestError();
}

const { username, password } = req.body;

const validator = jsonschema.validate({ username, password }, userAuth, {
  required: true,
});

if (!validator.valid) {
fork icon1
star icon0
watch icon1

+ 7 other calls in file

23
24
25
26
27
28
29
30
31
32
33
 * Authorization required:  admin
 */


router.post("/", ensureAdmin, async function (req, res, next) {
  console.log("I've hit the post route");
  const validator = jsonschema.validate(req.body, jobNewSchema, {
    required: true,
  });
  if (!validator.valid) {
    const errs = validator.errors.map((e) => e.stack);
fork icon0
star icon0
watch icon1

+ 11 other calls in file

72
73
74
75
76
77
78
79
80
81
 * => {isbn, amazon_url, author, language, pages, publisher, title, year}
 *
 * */

static async create(data) {
  const schemaRes = jsonschema.validate(data, bookSchema);
  if (schemaRes.valid) {
    const result = await db.query(
      `INSERT INTO books (
          isbn,
fork icon0
star icon0
watch icon1

+ 2 other calls in file

143
144
145
146
147
148
149
150
151
152
}

const validateInput = (req, res, next) => {
    const {body} = req;
    if (BookingJsonSchema !== undefined) {
        const val = validate(body, BookingJsonSchema);
        if (val.valid === false) {
            return res.status(422).json({errors: val.errors});
        }
    }
fork icon0
star icon0
watch icon1

119
120
121
122
123
124
125
126
127
128
}

const validateInput = (req, res, next) => {
    const {body} = req;
    if (MovieTheaterJsonSchema !== undefined) {
        const val = validate(body, MovieTheaterJsonSchema);
        if (val.valid === false) {
            return res.status(422).json({errors: val.errors});
        }
    }
fork icon0
star icon0
watch icon1

55
56
57
58
59
60
61
62
63
64
// get integers
if (query.minEmployees !== undefined) query.minEmployees = +query.minEmployees;
if (query.maxEmployees !== undefined) query.maxEmployees = +query.maxEmployees;
try {
  console.log(query)
  const validator = jsonschema.validate(query, companySearchSchema);
  if (!validator.valid) {
    const errs = validator.errors.map(e => e.stack);
    throw new BadRequestError(errs);
  }
fork icon0
star icon0
watch icon1

+ 11 other calls in file

45
46
47
48
49
50
51
52
53
54
 * 
 *  Returns a category created.
 */
router.post('/', ensureAdmin, async (req, res, next) => {
    try{
        const validator = jsonschema.validate(req.body, categoryNewSchema);
        if(!validator.valid) {
            const errs = validator.errors.map(e => e.stack);
            throw new BadRequestError(errs);
        }
fork icon0
star icon0
watch icon0

51
52
53
54
55
56
57
58
59
60
 * 
 * Authorization: admin or the same user as the :username param
 */
router.patch('/:username', ensureCorrectUserOrAdmin, async (req, res, next) => {
    try{
        const validator = jsonschema.validate(req.body, userUpdateSchema);
        if (!validator.valid) {
            const errs = validator.errors.map(e => e.stack);
            throw new BadRequestError(errs);
        }
fork icon0
star icon0
watch icon0

52
53
54
55
56
57
58
59
60
61
62
 * Authrization required: admin
 */


router.post('/', ensureAdmin, async function (req, res, next) {
    try{
        const validator = jsonschema.validate(req.body, itemNewSchema);
        if (!validator.valid){
            const errs = validator.errors.map(e => e.stack);
            throw new BadRequestError(errs);
        }
fork icon0
star icon0
watch icon0

18
19
20
21
22
23
24
25
26
27
28
 * Authorization required: none
 */


router.post("/token", async function (req, res, next) {
  try {
    const validator = jsonschema.validate(req.body, userAuthSchema);
    if (!validator.valid) {
      const errs = validator.errors.map(e => e.stack);
      throw new BadRequestError(errs);
    }
fork icon0
star icon0
watch icon0

26
27
28
29
30
31
32
33
34
35
36
 * Authorization required: admin
 **/


router.post("/", ensureAdmin, async function (req, res, next) {
  try {
    const validator = jsonschema.validate(req.body, userNewSchema);
    if (!validator.valid) {
      const errs = validator.errors.map(e => e.stack);
      throw new BadRequestError(errs);
    }
fork icon0
star icon0
watch icon0

42
43
44
45
46
47
48
49
50
51
52
 * Authorization required: none
 */


router.post("/register", async function (req, res, next) {
  try {
    // const validator = jsonschema.validate(req.body, userRegisterSchema);
    // if (!validator.valid) {
    //   const errs = validator.errors.map((e) => e.stack);
    //   throw new BadRequestError(errs);
    // }
fork icon0
star icon0
watch icon0

24
25
26
27
28
29
30
31
32
33
34
 *  Authorization required: admin
 */


router.post("/", ensureAdmin, async function(req, res, next) {
  try {
    const validator = jsonschema.validate(req.body, lineNewSchema);
    if(!validator.valid) {
      const errs = validator.errors.map(e => e.stack);
      throw new BadRequestError(errs);
    }
fork icon0
star icon0
watch icon0

+ 5 other calls in file

52
53
54
55
56
57
58
59
60
61
62
router.get("/", async function(req, res, next) {
  const q = req.query;
  if(q.authorId !== undefined) q.authorId = +q.authorId;


  try {
    const validator = jsonschema.validate(q, workSearchSchema);
    if(!validator.valid) {
      const errs = validator.errors.map(e => e.stack);
      throw new BadRequestError(errs);
    }
fork icon0
star icon0
watch icon0

+ 5 other calls in file

22
23
24
25
26
27
28
29
30
31
32
 *
 * Authorization required: admin only
 */


router.post("/", ensureIsAdmin, async function (req, res, next) {
  const validator = jsonschema.validate(
    req.body,
    companyNewSchema,
    {required: true}
  );
fork icon0
star icon0
watch icon1

+ 11 other calls in file

53
54
55
56
57
58
59
60
61
62
63
64
65
 * 
 */


router.post("/", async function(req, res, next) {
    try {
        const validatorOne = jsonschema.validate(req.body.orderInfo, orderNewSchema)


        const validatorTwo = jsonschema.validate(req.body.orderInfo, orderNewTwoSchema)


        if(!validatorOne.valid && !validatorTwo.valid){
fork icon0
star icon0
watch icon0

+ 5 other calls in file

17
18
19
20
21
22
23
24
25
26
27
//  Returns { id, username, title, habit_description }
//  Authorization required: admin or current user


router.post("/:username", ensureCorrectUserOrAdmin, async function (req, res, next) {
  try {
    const validator = jsonschema.validate(req.body, todoNewSchema);
    if (!validator.valid) {
      const errs = validator.errors.map(e => e.stack);
      throw new BadRequestError(errs);
    }
fork icon0
star icon0
watch icon0

+ 2 other calls in file