shou.com
JP / EN

TypeScriptでバリデーション、Ajvを使ってみた

Wed Oct 12, 2022
Wed Oct 12, 2022

最近はlambdaをTypeScriptで書いている僕です。

api gatewayから受け取った値にバリデーションする際にAjvを使ってみたのですが、大変便利だったのでメモしておきます。

環境

  • “ts-node”: “^10.9.1”
  • “typescript”: “~4.8.2”
  • “ajv”: “^8.11.0”
  • “ajv-formats”: “^2.1.1”

基本的な使い方

Using with TypeScript

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import Ajv, {JSONSchemaType} from "ajv"
const ajv = new Ajv()

interface MyData {
  foo: number
  bar?: string
}
const schema: JSONSchemaType<MyData> = {
  type: "object",
  properties: {
    foo: {type: "integer"},
    bar: {type: "string", nullable: true}
  },
  required: ["foo"],
  additionalProperties: false
}

// validate is a type guard for MyData - type is inferred from schema type
const validate = ajv.compile(schema)

// or, if you did not use type annotation for the schema,
// type parameter can be used to make it type guard:
// const validate = ajv.compile<MyData>(schema)

const data = {
  foo: 1,
  bar: "abc"
}

if (validate(data)) {
  // data is MyData here
  console.log(data.foo)
} else {
  console.log(validate.errors)
}
>> 1

コードとドキュメントを見れば、だいぶ簡単にバリデーションが実装できることがわかると思います。

試しにエラーの場合はどうなるか見てみます。場合はrequired: ["foo"]になっているのでこれをちょっと変えて。わざとエラーを発生させます。

1
2
3
const data = {
  bar: "abc"
}

そうすると以下のようにエラーを教えてくれます。

1
2
3
4
5
6
7
8
9
[
  {
    instancePath: '',
    schemaPath: '#/required',
    keyword: 'required',
    params: { missingProperty: 'foo' },
    message: "must have required property 'foo'"
  }
]

フォーマットのバリデーション

emailなどのバリデーションをしたい場合はFormat validationを使用します。

version 7 Ajvからajv-formatsを使用するようになっていますので、古い記事などにはお気をつけてください。

新しい使い方はプラグインを追加することで使えるようになります。

1
2
3
4
5
import Ajv from "ajv"
import addFormats from "ajv-formats"

const ajv = new Ajv()
addFormats(ajv)

試しに先ほどコードでfooをemailにしてバリデーションができているかどうか確認してみます。プロパティにformatを追加するだけです。

全体のコードはこうなります。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import Ajv, {JSONSchemaType} from "ajv";
import addFormats from "ajv-formats";
const ajv = new Ajv();
addFormats(ajv);

interface MyData {
  foo: string;
  bar?: string;
}

const schema: JSONSchemaType<MyData> = {
  type: "object",
  properties: {
    foo: {type: "string", format: "email"},
    bar: {type: "string", nullable: true}
  },
  required: ["foo"],
  additionalProperties: false
};

const validate = ajv.compile(schema);

const data = {
  foo: "examplxample.com",
  bar: "abc"
};

if (validate(data)) {
  // data is MyData here
  console.log(data.foo);
} else {
  console.log(validate.errors);
}

プロパティでemailをバリデーションするようにしています。

1
2
3
4
  properties: {
    foo: {type: "string", format: "email"},
    bar: {type: "string", nullable: true}
  },

上記のコードは試しにわざとエラーを出してます。

1
2
3
4
5
6
7
8
9
[
  {
    instancePath: '/foo',
    schemaPath: '#/properties/foo/format',
    keyword: 'format',
    params: { format: 'email' },
    message: 'must match format "email"'
  }
]

must match format "email"となってますね! 成功です。これは大変便利です。lambaの開発が捗りそう。

See Also