How to use the express-json-validator-middleware.Validator function in express-json-validator-middleware

To help you get started, we’ve selected a few express-json-validator-middleware examples, based on popular ways it is used in public projects.

github NYCPlanning / labs-mapboxgl-print-service / routes / config.js View on Github external
const defaultConfig = {
  mapConfig: {
    container: 'map',
    style: '//raw.githubusercontent.com/NYCPlanning/labs-gl-style/master/data/style.json',
    center: [-73.93181409196421, 40.708434183373726],
    zoom: 10.2,
    bearing: 0,
    pitch: 0,
  },
  title: 'New York City',
  logo: 'https://raw.githubusercontent.com/NYCPlanning/logo/master/dcp_logo_772.png',
  content: '',
};

// Initialize a Validator instance first
const validator = new Validator({allErrors: true}); // pass in options to the Ajv instance

// Define a shortcut function
const validate = validator.validate;

// Define a JSON Schema
const PrintSchema = {
  type: 'object',
  required: ['title', 'mapConfig'],
  properties: {
    title: {
      type: 'string',
    },
    mapConfig: {
      type: 'object',
      required: ['style', 'center', 'zoom'],
      properties: {
github hattmo / c2profilejs / src / server / routes / keystores.ts View on Github external
import * as express from "express";
import * as path from "path";
import { Validator } from "express-json-validator-middleware";
import keystoremodel from "../models/keyStoreModel";
import postKeystoresScema from "../helpers/schemas/postKeystoresSchema";

const route = express.Router()
const validator = new Validator({ allErrors: true });

route.post("/", validator.validate({ body: postKeystoresScema }), async (req, res) => {
  try {
    if (await keystoremodel.addKeystore(req.body.keystore, req.body.opt, req.body.ca)) {
      res.sendStatus(200);
    } else {
      res.sendStatus(400);
    }
  } catch (err) {
    console.error(err);
    res.sendStatus(500);
  }
});

route.get("/", (req, res) => {
  res.json(keystoremodel.getKeystores());
github hattmo / c2profilejs / src / server / routes / profiles.ts View on Github external
export default (profileModel: ProfileModel) => {
  const route = express.Router();
  const validator = new Validator({ allErrors: true });

  route.post("/", validator.validate({ body: postProfileScema }), (req, res, next) => {
    try {
      if (profileModel.addProfile(req.body)) {
        res.sendStatus(200);
      } else {
        res.status(400).json({
          errorMessage: "A profile by that name already exists.",
        });
      }
    } catch (reason) {
      next(reason);
    }
  });

  route.get("/", (_req, res) => {
github decentraland / ethalarm / src / routes / alarms.js View on Github external
import { Validator } from 'express-json-validator-middleware'

import AlarmSchema from '../schemas/alarms'

const validator = new Validator({ allErrors: true })
const validate = validator.validate.bind(validator)

export default class AlarmRouter {
  constructor(alarmService) {
    this.alarm = alarmService
  }

  setup(app) {
    app.post('/alarms', validate({ body: AlarmSchema }), this.postAlarm)
    app.get('/alarms/:id', this.getAlarm)
    app.delete('/alarms/:id', this.deleteAlarm)
  }

  get postAlarm() {
    return (req, res) => buildResponseSchema(this.alarm.storeNewAlarm(req.body), res)
  }

express-json-validator-middleware

An Express middleware to validate requests against JSON Schemas

MIT
Latest version published 4 months ago

Package Health Score

73 / 100
Full package analysis

Popular express-json-validator-middleware functions

Similar packages