LoginSignup
0
0

More than 1 year has passed since last update.

OpenAPI 定義からyup定義を生成するRust製CLIを作ってみた

Posted at

OpenAPIv3の定義ファイルからyupのバリデーション定義を自動生成してくれるCLIを作ってみました

使い方

openapi-yup-generator -f openapi3.yaml -o yup-defs.js

すると以下のようなjsファイルが生成される

/* tslint:disable */
/* eslint-disable */
//DO NOT EDIT MANUALLY

import { object, string, number, date, array, bool } from 'yup';

export const LightingSummary = object({
  zones: array().of(object({
    id: string().optional(),
    name: string().optional(),
    deviceId: number().integer().optional(),
    deviceType: string().optional(),
    zone: string().optional(),
  })).optional(),
  zoneStatus: array().of(object({
    id: string().optional(),
    name: string().optional(),
    lastUpdate: date().optional(),
    level: number().integer().optional(),
  }).label('the status of the lighting zone.')).optional(),
}).label('ok');

export const LightingZone = object({
  id: string().optional(),
  name: string().optional(),
  deviceId: number().integer().optional(),
  deviceType: string().optional(),
  zone: string().optional(),
});

インストール方法

curl -sfL https://raw.githubusercontent.com/igtm/openapi-yup-generator/master/install.sh | sudo sh -s -- -b=/usr/local/bin

(M1Mac等で権限でエラーになる場合は 先頭に sudo をつけて実行してみてください)

作った背景

最近APIにgoa、FrontにReact(Typescript)、FrontのAPIまわりは openapi-generator-cliで自動生成させる構成をよく使っていました。
ただ、SPAを採用するとAPIとFrontの2重でバリデーション定義を管理しないといけないのが辛いので、openapi3.yamlからyupの定義ができたら良いなと思っており今回Rustの勉強も兼ねて作ってみました。

yupでよく使う定義は対応したつもりですが、これも使いたいなどあれば気軽にissue/PR化していただければなと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0