-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64a1c99
commit 1ba4b5a
Showing
6 changed files
with
72 additions
and
90 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
58 changes: 58 additions & 0 deletions
58
packages/shared/src/utils/schema/create-mock-object-from-schema.ts
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,58 @@ | ||
import { JSONSchemaDto } from '../../dto'; | ||
|
||
/** | ||
* Generates a payload based solely on the schema. | ||
* Supports nested schemas and applies defaults where defined. | ||
* @param JSONSchemaDto - Defining the structure. example: | ||
* { | ||
* type: 'object', | ||
* properties: { | ||
* payload: { | ||
* firstName: { type: 'string', default: 'John' }, | ||
* lastName: { type: 'string' } | ||
* } | ||
* } | ||
* } | ||
* @returns - Generated payload. example: { payload: { firstName: 'John', lastName: '{{payload.lastName}}' }} | ||
*/ | ||
export function createMockObjectFromSchema( | ||
schema: JSONSchemaDto, | ||
safe = true, | ||
path = '', | ||
depth = 0 | ||
): Record<string, unknown> { | ||
const MAX_DEPTH = 10; | ||
if (depth >= MAX_DEPTH) { | ||
if (safe) { | ||
return {}; | ||
} | ||
throw new Error( | ||
`Schema has surpassed the maximum allowed depth. Please specify a more shallow payload schema. Max depth: ${MAX_DEPTH}` | ||
); | ||
} | ||
|
||
if (schema?.type !== 'object' || !schema?.properties) { | ||
if (safe) { | ||
return {}; | ||
} | ||
throw new Error('Schema must define an object with properties.'); | ||
} | ||
|
||
return Object.entries(schema.properties).reduce((acc, [key, definition]) => { | ||
if (typeof definition === 'boolean') { | ||
return acc; | ||
} | ||
|
||
const currentPath = path && path.length > 0 ? `${path}.${key}` : key; | ||
|
||
if (definition.default) { | ||
acc[key] = definition.default; | ||
} else if (definition.type === 'object' && definition.properties) { | ||
acc[key] = createMockObjectFromSchema(definition, safe, currentPath, depth + 1); | ||
} else { | ||
acc[key] = `{{${currentPath}}}`; | ||
} | ||
|
||
return acc; | ||
}, {}); | ||
} |