{"id":20980516,"url":"https://github.com/hyperjumptech/swagger-to-js-validator","last_synced_at":"2026-04-12T08:31:06.243Z","repository":{"id":60120257,"uuid":"514960108","full_name":"hyperjumptech/swagger-to-js-validator","owner":"hyperjumptech","description":"Create a request validator from an Open API 3 schema (formerly Swagger)","archived":false,"fork":false,"pushed_at":"2022-09-25T13:16:35.000Z","size":118,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-13T10:17:10.804Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hyperjumptech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-17T21:32:16.000Z","updated_at":"2022-07-17T21:32:41.000Z","dependencies_parsed_at":"2022-09-25T23:03:56.865Z","dependency_job_id":null,"html_url":"https://github.com/hyperjumptech/swagger-to-js-validator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hyperjumptech/swagger-to-js-validator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperjumptech%2Fswagger-to-js-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperjumptech%2Fswagger-to-js-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperjumptech%2Fswagger-to-js-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperjumptech%2Fswagger-to-js-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hyperjumptech","download_url":"https://codeload.github.com/hyperjumptech/swagger-to-js-validator/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperjumptech%2Fswagger-to-js-validator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28049975,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-26T02:00:06.189Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T05:28:59.425Z","updated_at":"2025-12-26T08:21:11.059Z","avatar_url":"https://github.com/hyperjumptech.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# stjv (swagger-to-js-validator)\n\n[![Build \u0026 Test](https://github.com/hyperjumptech/swagger-to-js-validator/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/hyperjumptech/swagger-to-js-validator/actions/workflows/main.yml)\n\n## About\n\nThis CLI generates request validators based on [OpenAPI 3 schema](https://swagger.io/docs/specification/about/) (previously called Swagger).\n\n## Motivation\n\nstjv was created to prevent you from writing code to validate the requests coming to API end points one by one. You can instead write an OpenAPI file to define the specifications for all the end points of your app, such as what HTTP methods are accepted, required query properties, and also required properties in the body of the request, then let stjv generate the code to validate the incoming requests. **Not only you'll get a nicely documented API, you'll also get the validation code you can use in your API end points**.\n\nSo instead of writing\n\n```typescript\n// pages/api/pet.ts\n\nexport default async function handler(\n  req: NextApiRequest,\n  res: NextApiResponse\u003cResponse\u003e\n) {\n  if (req.method === \"POST\") {\n    if (req.body \u0026\u0026 req.body.name) {\n      // do something\n    }\n  }\n}\n```\n\nyou can write\n\n```typescript\nexport default async function handler(\n  req: NextApiRequest,\n  res: NextApiResponse\u003cResponse\u003e\n) {\n  let validated: ValidatedPetRequest;\n  try {\n    validated = await validatePetRequest(req);\n  } catch (error: any) {\n    return res.status(error.statusCode ?? 400).json({ error: error.errors });\n  }\n\n  // from here on, the validated will be guaranteed to have method, query, and body. Not optional anymore.\n}\n```\n\n\n**:construction: WARNING: This package is WIP**\n\n## Features\n\n|  |  |\n| --- | ----------- |\n| ✅  | Generate TypeScript functions                                                                                          |\n| ✅  | Validate the incoming request's HTTP method                                                                            |\n| ✅  | Validate the incoming request's body if any                                                                            |\n| ✅  | Generate various validators. Supported validator as of now: [Yup](https://github.com/jquense/yup)                      |\n| ✅  | Generate validator functions for various web frameworks. Supported framework as of now: [Next.js](https://nextjs.org/) |\n\n## Installation\n\n```sh-session\nnpm install @hyperjumptech/sjtv\n```\n\n## Usage\n\nFirst add a script in the package.json to run `stjv` with two arguments: the path to Open API 3 file and the path to the TypeScript file.\n\n```json\n\"scripts\": {\n  \"generate:ts-validator\": \"stjv generate \u003cpath_to_open_api_3_yaml\u003e \u003coutput_path_to_write_ts_file\u003e\"\n}\n```\n\nNext, run the script\n\n```sh-session\nnpm run generate:ts-validator\n```\n\nThe command above will generate the TypeScript file which contains the functions to validate the incoming requests. Thus, the `output_path_to_write_ts_file` should be inside your project's directory.\n\n### Next.js API\n\nAfter runnning the command above, you can use the generated TypeScript file in the API end points of Next.js. The generated function's names are based on the `paths` you defined in the Open API 3 YAML file. For example, say you have the following `paths` in the YAML file:\n\n```yaml\npaths:\n  /pet:\n    put:\n      tags:\n        - pet\n      summary: Update an existing pet\n      operationId: updatePet\n      requestBody:\n        description: Pet object that needs to be added to the store\n        content:\n          application/json:\n            schema:\n              $ref: '#/components/schemas/Pet'\n          application/xml:\n            schema:\n              $ref: '#/components/schemas/Pet'\n        required: true\n      responses:\n        400:\n          description: Invalid ID supplied\n          content: {}\n        404:\n          description: Pet not found\n          content: {}\n        405:\n          description: Validation exception\n          content: {}\n      security:\n        - petstore_auth:\n            - write:pets\n            - read:pets\n      x-codegen-request-body-name: body\n    post:\n      tags:\n        - pet\n      summary: Add a new pet to the store\n      operationId: addPet\n      requestBody:\n        description: Pet object that needs to be added to the store\n        content:\n          application/json:\n            schema:\n              $ref: '#/components/schemas/Pet'\n          application/xml:\n            schema:\n              $ref: '#/components/schemas/Pet'\n        required: true\n      responses:\n        405:\n          description: Invalid input\n          content: {}\n      security:\n        - petstore_auth:\n            - write:pets\n            - read:pets\n      x-codegen-request-body-name: body\ncomponents:\n  schemas:\n    Pet:\n      required:\n        - name\n        - photoUrls\n      type: object\n      properties:\n        id:\n          type: integer\n          format: int64\n        category:\n          $ref: '#/components/schemas/Category'\n        name:\n          type: string\n          example: doggie\n        photoUrls:\n          type: array\n          xml:\n            name: photoUrl\n            wrapped: true\n          items:\n            type: string\n        tags:\n          type: array\n          xml:\n            name: tag\n            wrapped: true\n          items:\n            $ref: '#/components/schemas/Tag'\n        status:\n          type: string\n          description: pet status in the store\n          enum:\n            - available\n            - pending\n            - sold\n      xml:\n        name: Pet\n```\n\n`stjv` will generate a function called `validatePetRequest` which you can use in the handler of `/api/pet` end point.\n\n```typescript\n// pages/api/pet.ts\nimport type { NextApiRequest, NextApiResponse } from \"next\";\nimport { ValidatedPetRequest, validatePetRequest } from \"../../validation\"; // path to the generated TypeScript file\n\ntype Response = {\n  data?: {\n    name: string;\n  };\n  error?: any;\n};\n\nexport default async function handler(\n  req: NextApiRequest,\n  res: NextApiResponse\u003cResponse\u003e\n) {\n  let validated: ValidatedPetRequest;\n  try {\n    validated = await validatePetRequest(req);\n  } catch (error: any) {\n    return res.status(error.statusCode ?? 400).json({ error: error.errors });\n  }\n\n  res.status(200).json({ data: { name: validated.body.name } });\n}\n```\n\nThe generated validation function, e.g., `validatePetRequest`, will throw error when the request validation fails when the incoming request in using GET method, or the incoming body doesn't have `name` property, etc.\n\nThe error object will be in the shape of\n\n```typescript\n{\n  statusCode: number,\n  errors: {\n    path: string,\n    type: string,\n    message: string\n  }[]\n}\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyperjumptech%2Fswagger-to-js-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhyperjumptech%2Fswagger-to-js-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyperjumptech%2Fswagger-to-js-validator/lists"}