Skip to content

Commit

Permalink
fix indentation in code samples, turn off prettier for md files
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Mar 6, 2021
1 parent 22aa683 commit 0aa047b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 80 deletions.
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,6 @@ const ajv = new Ajv({
})
```

## Options
##### Options

This section is moved to [Initialization options](./options) page
1 change: 0 additions & 1 deletion docs/guide/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const addFormats = require("ajv-formats")

const ajv = new Ajv()
addFormats(ajv)

````
</code-block>

Expand Down
51 changes: 24 additions & 27 deletions docs/guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

````
</code-block>

Expand Down Expand Up @@ -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)
Expand All @@ -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"}'
Expand All @@ -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)
}
}

```
</code-block>
</code-group>
Expand All @@ -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`.
:::
```
43 changes: 24 additions & 19 deletions docs/guide/managing-schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})

````
</code-block>

Expand All @@ -81,7 +84,6 @@ app.post("/user", async (cxt) => {
}
})
````

</code-block>
</code-group>

Expand Down Expand Up @@ -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)
}
})

````
</code-block>

<code-block title="TypeScript">
<code-block title="users.ts">
```javascript
import ajv from "./validation"
Expand All @@ -165,7 +170,6 @@ app.post("/user", async (cxt) => {
}
})
````

</code-block>
</code-group>

Expand Down Expand Up @@ -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.
Expand Down
56 changes: 27 additions & 29 deletions docs/guide/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyData> = {
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
Expand All @@ -44,17 +44,16 @@ const validate = ajv.compile(schema)
// const validate = ajv.compile<MyData>(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)
}

````
</code-block>

Expand Down Expand Up @@ -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
// ...
}
}
}

````
</code-block>
</code-group>
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -117,6 +117,6 @@
}
},
"lint-staged": {
"*.{md,json,yaml,js,ts}": "prettier --write"
"*.{json,yaml,js,ts}": "prettier --write"
}
}

0 comments on commit 0aa047b

Please sign in to comment.