forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Validate JSON before uploading
- Loading branch information
Showing
7 changed files
with
111 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,25 @@ | ||
export type Schema = { | ||
[key: string]: 'string' | 'number' | 'boolean' | 'object' | 'array' | Schema; | ||
}; | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
export function hasStructure(obj: any, schema: Schema): boolean { | ||
if (typeof obj !== 'object' || obj === null) return false; | ||
|
||
for (const key in schema) { | ||
const expectedType = schema[key]; | ||
|
||
if (['string', 'number', 'boolean'].includes(expectedType as string)) { | ||
if (typeof obj[key] !== expectedType) return false; | ||
} else if (expectedType === 'object') { | ||
if (typeof obj[key] !== 'object' || obj[key] === null) return false; | ||
} else if (expectedType === 'array') { | ||
if (!Array.isArray(obj[key])) return false; | ||
} else if (typeof expectedType === 'object') { | ||
// Recursively check nested objects | ||
if (!hasStructure(obj[key], expectedType as Schema)) return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |
This file contains 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