-
Notifications
You must be signed in to change notification settings - Fork 394
chore: validate json files #20120
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
Merged
Merged
chore: validate json files #20120
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ef912e0
chore: validate json files
kpawelczak 8302ca3
Add license header
github-actions[bot] bf610b3
Update validate-translations-json-files.ts
kpawelczak 3166d61
Merge branch 'chore/CXSPA-9605' of https://github.com/SAP/spartacus i…
kpawelczak ebb74e6
update deps
kpawelczak b482844
Update validate-translations-json-files.ts
kpawelczak 55946b8
Update validate-translations-json-files.ts
kpawelczak 7f00568
Update index_spec.ts.snap
kpawelczak 55a50b1
update
kpawelczak bf814b1
Update dependencies.json
kpawelczak 571277b
Merge branch 'develop' into chore/CXSPA-9605
kimhw0630 7672c50
Merge branch 'develop' into chore/CXSPA-9605
kpawelczak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
const TRANSLATIONS_DIR = '../assets/src/translations'; | ||
const MAX_FILE_SIZE = 20 * 1024; // 20 KB limit | ||
const BANNED_KEYS = [ | ||
'<script', | ||
'onerror', | ||
'onload', | ||
'onclick', | ||
'javascript:', | ||
'__proto__', | ||
'constructor', | ||
'eval', | ||
]; | ||
const NOT_ALLOWED_VALUE_REGEX = | ||
/^[{\[](?:[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]|".*?")*[}\]]$/u; | ||
|
||
function validateJson(filePath: string) { | ||
const fileSize = fs.statSync(filePath).size; | ||
|
||
if (fileSize > MAX_FILE_SIZE) { | ||
throw new Error(`File ${filePath} exceeds size limit (${fileSize} bytes).`); | ||
} | ||
|
||
const content = fs.readFileSync(filePath, 'utf8'); | ||
|
||
let parsedJson; | ||
try { | ||
parsedJson = JSON.parse(content); | ||
} catch (error) { | ||
throw new Error(`Invalid JSON format in ${filePath}`); | ||
} | ||
|
||
const traverse = (jsonData: any) => { | ||
for (const key in jsonData) { | ||
if (typeof jsonData[key] === 'object' && jsonData[key] !== null) { | ||
traverse(jsonData[key]); | ||
} else { | ||
const translation = jsonData[key]; | ||
|
||
if (typeof translation !== 'string') { | ||
throw new Error(`Translation entry is not a string.`); | ||
} | ||
|
||
if (NOT_ALLOWED_VALUE_REGEX.test(translation) || NOT_ALLOWED_VALUE_REGEX.test(key) ) { | ||
throw new Error( | ||
`Unallowed char in ${filePath}: ${key} '${translation}'.` | ||
); | ||
} | ||
|
||
if (BANNED_KEYS.some((bannedKey) => translation.includes(bannedKey) || key.includes(bannedKey))) { | ||
throw new Error( | ||
`Forbidden char in ${filePath}: ${key} '${translation}'.` | ||
); | ||
} | ||
} | ||
} | ||
}; | ||
traverse(parsedJson); | ||
} | ||
|
||
function getJsonFiles(dir: any) { | ||
let results: any[] = []; | ||
const list = fs.readdirSync(dir); | ||
|
||
list.forEach((file) => { | ||
const filePath = path.join(dir, file); | ||
const stat = fs.statSync(filePath); | ||
|
||
if (stat && stat.isDirectory()) { | ||
results = results.concat(getJsonFiles(filePath)); | ||
} else if (file.endsWith('.json')) { | ||
results.push(filePath); | ||
} | ||
}); | ||
|
||
return results; | ||
} | ||
|
||
function validateAllTranslations() { | ||
console.log('Validating translation files...'); | ||
|
||
const filePaths = getJsonFiles(TRANSLATIONS_DIR); | ||
|
||
filePaths.forEach((filePath) => { | ||
validateJson(filePath); | ||
}); | ||
|
||
console.log('✅ All translations passed validation!'); | ||
} | ||
|
||
try { | ||
validateAllTranslations(); | ||
} catch (error) { | ||
console.error('❌ Validation failed: ', error); | ||
process.exit(1); // Exit with error status to stop the build | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.