-
Notifications
You must be signed in to change notification settings - Fork 4k
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
fix(dashboard): nested payload gen #7240
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. was moved to shared and refactored to be a more generic function and not payload object based. |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. was deleted, less is more? now we reuse the createMockObjectFromSchema from shared the same one that BE used. |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. util from the API. |
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; | ||
}, {}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the change was needed because now createMockObjectFromSchema is a more generic function and not
payload
object based.