Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/3rd-party-lib/ajv-validator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# AJV Validator

### Import
#### Nodejs
```ts
import nhttp from "nhttp-land";
import Ajv from "ajv";
```

### Usage
#### Nodejs
```ts
const app = nhttp();
const ajv = new Ajv();

// ajv validate middleware
const validate = (schema) => (rev, next) => {
const valid = ajv.validate(schema, rev.body);
if (valid) return next();
throw new HttpError(422, ajv.errors);
}

const foobarSchema = {
type: "object",
properties: {
foo: {type: "integer"},
bar: {type: "string"}
},
required: ["foo"],
additionalProperties: false
}

app.post("/save", validate(foobarSchema), () => "success save foobar");
```