0.0.6 • Published 4 years ago
valmo v0.0.6
Valmo
A simple yet powerful way to ensure data validity.
Content
Schema layout
Valmo uses the same schema standard as ajv as this is the validator Valmo uses. It is in turn based on json-schema.org. Both have great reasources on how to use schemas.
Getting started
Example project:
// index.js
const valmo = require("valmo")
const express = require("express")
const server = express()
server.use(express.json())
const port = 8080
const schema = require('./userSchema')
server.post(
'/user',
valmo.validate(schema, "request.body.user"),
(request, response) => {
response
.status(200)
.send("That is a valid user!")
.end()
}
)
server.listen(port, () => console.log(`Waiting for connections on port ${port}`))
// userSchema.js
module.exports = {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.org/test.json",
"title": "User",
"description": "A user model for a website",
"type": "object",
"properties": {
"username": {
"description": "User identifier",
"type": "string"
},
"email": {
"description": "A users personal email",
"type": "string"
}
},
"required": [
"username",
"email"
]
};