Skip to content

Commit

Permalink
refactor: makeId (#246)
Browse files Browse the repository at this point in the history
* refactor: makeId

* docs: rephrase readme
  • Loading branch information
toomuchdesign authored May 11, 2024
1 parent 60c5ee5 commit a957401
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

Generate **TypeScript JSON schema** files (`.ts` modules with `as const` assertions) from **OpenAPI** definitions.

**TypeScript JSON schemas** are 100% valid JSON schemas which serve as the single source of truth for runtime validation and data type inference.

TypeScript JSON schemas serve various purposes, including:

- Validate data and infer validated data TS types with the same JSON schema (with any JSON schema validator like [Ajv](https://ajv.js.org/))
Expand All @@ -20,8 +22,6 @@ Given an OpenAPI definition file, `openapi-ts-json-schema` will:
- Generate a TypeScript JSON schema file for each definition (`.ts` files with `as const` assertion)
- Organizing schemas in a folder structure mirroring the original OpenAPI definition layout.

TypeScript JSON schemas are 100% valid JSON schemas.

`openapi-ts-json-schema` is currently in v0, which means it's still in its testing phase. I'm actively collecting feedback from users to improve its functionality and usability. **Please don't hesitate to open an issue if you encounter any problems or issues while using it.**

## Installation
Expand Down
13 changes: 7 additions & 6 deletions src/utils/makeId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'node:path';
import { filenamify } from '.';

/**
* Generate a local OpenAPI ref from a schema internal id
* Generate schema internal id
*/
const TRALING_SLASH_REGEX = /\/$/;
export function makeId({
Expand All @@ -12,15 +12,16 @@ export function makeId({
schemaRelativeDirName: string;
schemaName: string;
}): string {
return (
const normalizedSchemaRelativeDirName =
'/' +
path
.normalize(schemaRelativeDirName)
// Supporting definitionPathsToGenerateFrom dot notation
.replaceAll('.', '/')
// Replace windows backslashes \
.replaceAll('\\', '/')
.replace(TRALING_SLASH_REGEX, '') +
'/' +
filenamify(schemaName)
);
.replace(TRALING_SLASH_REGEX, '');

// Shall we replace '/' with a different separator?
return normalizedSchemaRelativeDirName + '/' + filenamify(schemaName);
}

0 comments on commit a957401

Please sign in to comment.