Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip leading invalid characters from Schema (fix #489) #538

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ export function toSafeString(string: string) {
return upperFirst(
// remove accents, umlauts, ... by their basic latin letters
deburr(string)
// remove leading invalid characters
.replace(/^[^a-zA-Z_$]+/g, '')
// replace chars which are not valid for typescript identifiers with whitespace
.replace(/(^\s*[^a-zA-Z_$])|([^a-zA-Z_$\d])/g, ' ')
// uppercase leading underscores followed by lowercase
Expand Down
24 changes: 24 additions & 0 deletions test/__snapshots__/test/test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,22 @@ Generated by [AVA](https://avajs.dev).
}␊
`

## leadingNumbersNames.js

> Expected output to match snapshot for e2e test: leadingNumbersNames.js

`/* eslint-disable */␊
/**␊
* This file was automatically generated by json-schema-to-typescript.␊
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
* and run json-schema-to-typescript to regenerate this file.␊
*/␊
export interface ExampleSchema {␊
[k: string]: unknown;␊
}␊
`

## namedProperty.js

> Expected output to match snapshot for e2e test: namedProperty.js
Expand Down Expand Up @@ -448473,6 +448489,14 @@ Generated by [AVA](https://avajs.dev).
a?: string;␊
[k: string]: unknown;␊
}␊
/**␊
* This interface was referenced by \`SafeTypeNames\`'s JSON-Schema␊
* via the \`definition\` "12345tartsWithManyDigits".␊
*/␊
export interface TartsWithManyDigits {␊
a?: string;␊
[k: string]: unknown;␊
}␊
/**␊
* This interface was referenced by \`SafeTypeNames\`'s JSON-Schema␊
* via the \`definition\` " 5tartsWithBlankAndDigit".␊
Expand Down
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
7 changes: 7 additions & 0 deletions test/e2e/leadingNumbersNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const input = {
title: '12345Example Schema',
type: 'object',
properties: {

}
}
5 changes: 5 additions & 0 deletions test/e2e/safeTypeNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export const input = {
a: {type: 'string'}
}
},
'12345tartsWithManyDigits': {
properties: {
a: {type: 'string'}
}
},
' 5tartsWithBlankAndDigit': {
properties: {
a: {type: 'string'}
Expand Down
9 changes: 9 additions & 0 deletions test/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ export function run() {
t.is(generateName('$Abc_123', usedNames), '$Abc_123')
t.is(generateName('Abc-de-f', usedNames), 'AbcDeF')

// Leading invalid characters should be removed
t.is(generateName('2022Abc-de-f', usedNames), 'AbcDeF1')
t.is(generateName('555tartsWithThreeDigits', usedNames), 'TartsWithThreeDigits')
t.is(generateName('1234555666tartsWithManyDigits', usedNames), 'TartsWithManyDigits')
t.is(generateName('123-455-5666%% startsWithDigitsAndSpecialChars', usedNames), 'StartsWithDigitsAndSpecialChars')
t.is(generateName('123456$Abc_123', usedNames), '$Abc_1231') // there's already '$Abc_123' on line 17 above
t.is(generateName('654321_cbA_123', usedNames), '_CbA_123')
t.is(generateName('1654321_cbA_123', usedNames), '_CbA_1231') // there's already '_CbA_123' output above

// Index should increment:
t.is(generateName('a', usedNames), 'A1')
t.is(generateName('a', usedNames), 'A2')
Expand Down