Skip to content

Instantly share code, notes, and snippets.

@ujeenator
Created November 3, 2016 20:08
Show Gist options
  • Select an option

  • Save ujeenator/e40c187c401f9f07d80b9548b59f1ac6 to your computer and use it in GitHub Desktop.

Select an option

Save ujeenator/e40c187c401f9f07d80b9548b59f1ac6 to your computer and use it in GitHub Desktop.
const app = require("koa")();
const router = require("koa-router")();
const r = require("rethinkdbdash")();
const ajv = require("ajv")({
removeAdditional: true
});
app.use(require("koa-body")());
app.use(router.routes());
const person = {
type: "object",
properties: {
name: { type: "string", minLength: 2, maxLength: 50 },
email: { type: "string", format: "email" },
age: { type: "integer" }
},
required: ["name", "email"],
additionalProperties: false
};
router.post("/api/people/add", function*() {
let valid = ajv.validate(person, this.request.body);
if (!valid)
this.throw(400, JSON.stringify(ajv.errors));
this.body = yield r.table("person")
.insert(this.request.body);
});
app.listen(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment