From 0aa047bad652129ff39df2a403387988cf60e636 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 6 Mar 2021 22:16:58 +0000 Subject: [PATCH] fix indentation in code samples, turn off prettier for md files --- docs/api.md | 2 +- docs/guide/formats.md | 1 - docs/guide/getting-started.md | 51 +++++++++++++++---------------- docs/guide/managing-schemas.md | 43 ++++++++++++++------------ docs/guide/typescript.md | 56 ++++++++++++++++------------------ package.json | 6 ++-- 6 files changed, 79 insertions(+), 80 deletions(-) diff --git a/docs/api.md b/docs/api.md index 114ff39c7..8ea27e338 100644 --- a/docs/api.md +++ b/docs/api.md @@ -498,6 +498,6 @@ const ajv = new Ajv({ }) ``` -## Options +##### Options This section is moved to [Initialization options](./options) page diff --git a/docs/guide/formats.md b/docs/guide/formats.md index a8c39062f..c2eb1cb4d 100644 --- a/docs/guide/formats.md +++ b/docs/guide/formats.md @@ -14,7 +14,6 @@ const addFormats = require("ajv-formats") const ajv = new Ajv() addFormats(ajv) - ```` diff --git a/docs/guide/getting-started.md b/docs/guide/getting-started.md index dc64a4193..e5182787e 100644 --- a/docs/guide/getting-started.md +++ b/docs/guide/getting-started.md @@ -38,25 +38,24 @@ const Ajv = require("ajv").default const ajv = new Ajv() // options can be passed, e.g. {allErrors: true} const schema = { -type: "object", -properties: { -foo: {type: "integer"}, -bar: {type: "string"} -}, -required: ["foo"], -additionalProperties: false + type: "object", + properties: { + foo: {type: "integer"}, + bar: {type: "string"} + }, + required: ["foo"], + additionalProperties: false } const validate = ajv.compile(schema) const validData = { -foo: 1, -bar: "abc" + foo: 1, + bar: "abc" } const valid = validate(data) if (!valid) console.log(validate.errors) - ```` @@ -120,12 +119,12 @@ const Ajv = require("ajv/dist/jtd").default const ajv = new Ajv() // options can be passed, e.g. {allErrors: true} const schema = { -properties: { -foo: {type: "int32"} -}, -optionalProperties: { -bar: {type: "string"} -} + properties: { + foo: {type: "int32"} + }, + optionalProperties: { + bar: {type: "string"} + } } const serialize = ajv.compileSerializer(schema) @@ -134,8 +133,8 @@ console.log(serialize(data)) const parse = ajv.compileParser(schema) const data = { -foo: 1, -bar: "abc" + foo: 1, + bar: "abc" } const json = '{"foo": 1, "bar": "abc"}' @@ -145,15 +144,14 @@ console.log(parseAndLog(json)) // logs {foo: 1, bar: "abc"} console.log(parseAndLog(invalidJson)) // logs error and position function parseAndLog(json) { -const data = parse(json) -if (data === undefined) { -console.log(parse.message) // error message from the last parse call -console.log(parse.position) // error position in string -} else { -console.log(data) -} + const data = parse(json) + if (data === undefined) { + console.log(parse.message) // error message from the last parse call + console.log(parse.position) // error position in string + } else { + console.log(data) + } } - ``` @@ -165,4 +163,3 @@ You would have smaller performance benefits in case your schema contains some pr ::: warning Please note Compiled parsers, unlike JSON.parse, do not throw the exception in case JSON string is not a valid JSON or in case data is invalid according to the schema. As soon as the parser determines that either JSON or data is invalid, it returns `undefined` and reports error and position via parsers properties `message` and `position`. ::: -``` diff --git a/docs/guide/managing-schemas.md b/docs/guide/managing-schemas.md index b442e1cc2..084f6159e 100644 --- a/docs/guide/managing-schemas.md +++ b/docs/guide/managing-schemas.md @@ -47,16 +47,19 @@ const schema_user = require("./schema_user.json") const ajv = new Ajv() const validate_user = ajv.compile(schema_user) + + + + // this is just some abstract API framework app.post("/user", async (cxt) => { -if (validate_user(cxt.body)) { -// create user -} else { -// report error -cxt.status(400) -} + if (validate_user(cxt.body)) { + // create user + } else { + // report error + cxt.status(400) + } }) - ```` @@ -81,7 +84,6 @@ app.post("/user", async (cxt) => { } }) ```` - @@ -132,21 +134,24 @@ And then you can import Ajv instance and access any schema in any application mo ```javascript const {ajv} = require("./validation") + + + + // this is just some abstract API framework app.post("/user", async (cxt) => { -const validate = ajv.getSchema("user") -if (validate(cxt.body)) { -// create user -} else { -// report error -cxt.status(400) -} + const validate = ajv.getSchema("user") + if (validate(cxt.body)) { + // create user + } else { + // report error + cxt.status(400) + } }) - ```` - + ```javascript import ajv from "./validation" @@ -165,7 +170,6 @@ app.post("/user", async (cxt) => { } }) ```` - @@ -214,7 +218,8 @@ then the above logic can be simpler: ```javascript const schema_user = require("./schema_user.json") -const validate = ajv.getSchema("https://example.com/user.json") || ajv.compile(schema_user) +const validate = ajv.getSchema("https://example.com/user.json") + || ajv.compile(schema_user) ``` The above is possible because when the schema has `$id` attribute `compile` method both compiles the schema (returning the validation function) and adds it to the Ajv instance cache at the same time. diff --git a/docs/guide/typescript.md b/docs/guide/typescript.md index 5253f52c7..1a4bfad97 100644 --- a/docs/guide/typescript.md +++ b/docs/guide/typescript.md @@ -22,18 +22,18 @@ import Ajv, {JSONSchemaType} from "ajv" const ajv = new Ajv() // options can be passed, e.g. {allErrors: true} interface MyData { -foo: number -bar?: string + foo: number + bar?: string } const schema: JSONSchemaType = { -type: "object", -properties: { -foo: {type: "integer"}, -bar: {type: "string"} -}, -required: ["foo"], -additionalProperties: false + type: "object", + properties: { + foo: {type: "integer"}, + bar: {type: "string"} + }, + required: ["foo"], + additionalProperties: false } // validate is a type guard for MyData - type is inferred from schema type @@ -44,17 +44,16 @@ const validate = ajv.compile(schema) // const validate = ajv.compile(schema) const validData = { -foo: 1, -bar: "abc" + foo: 1, + bar: "abc" } if (validate(data)) { -// data is MyData here -console.log(data.foo) + // data is MyData here + console.log(data.foo) } else { -console.log(validate.errors) + console.log(validate.errors) } - ```` @@ -119,22 +118,21 @@ import {DefinedError} from "ajv" // ... if (validate(data)) { -// data is MyData here -console.log(data.foo) + // data is MyData here + console.log(data.foo) } else { -// The type cast is needed, as Ajv uses a wider type to allow extension -// You can extend this type to include your error types as needed. -for (const err of validate.errors as DefinedError[]) { -switch (err.keyword) { -case "type": -// err type is narrowed here to have "type" error params properties -console.log(err.params.type) -break -// ... -} -} + // The type cast is needed, as Ajv uses a wider type to allow extension + // You can extend this type to include your error types as needed. + for (const err of validate.errors as DefinedError[]) { + switch (err.keyword) { + case "type": + // err type is narrowed here to have "type" error params properties + console.log(err.params.type) + break + // ... + } + } } - ```` diff --git a/package.json b/package.json index 13f1c6887..7131d9352 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,8 @@ ], "scripts": { "eslint": "eslint \"lib/**/*.ts\" \"spec/**/*.*s\" scripts --ignore-pattern spec/JSON-Schema-Test-Suite", - "prettier:write": "prettier --write \"./**/*.{md,json,yaml,js,ts}\"", - "prettier:check": "prettier --list-different \"./**/*.{md,json,yaml,js,ts}\"", + "prettier:write": "prettier --write \"./**/*.{json,yaml,js,ts}\"", + "prettier:check": "prettier --list-different \"./**/*.{json,yaml,js,ts}\"", "test-spec": "cross-env TS_NODE_PROJECT=spec/tsconfig.json mocha -r ts-node/register \"spec/**/*.spec.{ts,js}\" -R dot", "test-codegen": "nyc cross-env TS_NODE_PROJECT=spec/tsconfig.json mocha -r ts-node/register 'spec/codegen.spec.ts' -R spec", "test-debug": "npm run test-spec -- --inspect-brk", @@ -117,6 +117,6 @@ } }, "lint-staged": { - "*.{md,json,yaml,js,ts}": "prettier --write" + "*.{json,yaml,js,ts}": "prettier --write" } }