-
Notifications
You must be signed in to change notification settings - Fork 11
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
feat: add dynamic payload support in schema controller #46
Merged
ashwin275
merged 1 commit into
dhiway:main
from
ashwin275:feature/api-dynamic-payload-support
Sep 27, 2024
Merged
Changes from all commits
Commits
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 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,38 @@ | ||
export type PropertyType = | ||
| StringProperty | ||
| NumberProperty | ||
| BooleanProperty | ||
| ArrayProperty | ||
| ObjectProperty; | ||
|
||
export interface StringProperty { | ||
type: 'string'; | ||
enum?: string[]; | ||
format?: 'date' | 'time' | 'uri'; | ||
minLength?: number; | ||
maxLength?: number; | ||
} | ||
|
||
export interface NumberProperty { | ||
type: 'integer' | 'number'; | ||
enum?: number[]; | ||
minimum?: number; | ||
maximum?: number; | ||
} | ||
|
||
export interface BooleanProperty { | ||
type: 'boolean'; | ||
} | ||
|
||
export interface ArrayProperty { | ||
type: 'array'; | ||
items: PropertyType; | ||
minItems?: number; | ||
maxItems?: number; | ||
} | ||
|
||
export interface ObjectProperty { | ||
type: 'object'; | ||
properties: Record<string, PropertyType>; | ||
required?: string[]; | ||
} |
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,72 @@ | ||
import { PropertyType ,ObjectProperty} from "../types/Schema.interface"; | ||
|
||
|
||
export function extractSchemaFields(schema: any) { | ||
if (!schema || typeof schema !== 'object') { | ||
throw new Error("Invalid schema: Schema must be a valid object."); | ||
} | ||
|
||
|
||
const extractProperty = (property: any): PropertyType => { | ||
switch (property.type) { | ||
case 'string': { | ||
const { type, enum: enumValues, format, minLength, maxLength } = property; | ||
return { | ||
type, | ||
...(enumValues && { enum: enumValues }), | ||
...(format && { format }), | ||
...(minLength && { minLength }), | ||
...(maxLength && { maxLength }) | ||
}; | ||
} | ||
case 'integer': | ||
case 'number': { | ||
const { type, enum: enumValues, minimum, maximum } = property; | ||
return { | ||
type, | ||
...(enumValues && { enum: enumValues }), | ||
...(minimum && { minimum }), | ||
...(maximum && { maximum }) | ||
}; | ||
} | ||
case 'array': { | ||
const { type, items, minItems, maxItems } = property; | ||
return { | ||
type, | ||
items: extractProperty(items), | ||
...(minItems && { minItems }), | ||
...(maxItems && { maxItems }) | ||
}; | ||
} | ||
case 'object': { | ||
const { type, properties, required } = property; | ||
return { | ||
type, | ||
properties: Object.keys(properties).reduce((acc, key) => { | ||
acc[key] = extractProperty(properties[key]); | ||
return acc; | ||
}, {} as ObjectProperty['properties']), | ||
...(required && { required }) | ||
}; | ||
} | ||
case 'boolean': | ||
return { type: 'boolean' }; | ||
default: | ||
throw new Error(`Unsupported property type: ${property.type}`); | ||
} | ||
}; | ||
|
||
|
||
const properties = Object.keys(schema.properties || {}).reduce((acc, key) => { | ||
acc[key] = extractProperty(schema.properties[key]); | ||
return acc; | ||
}, {} as Record<string, PropertyType>); | ||
|
||
|
||
return { | ||
title: schema.title || schema.$id, | ||
properties, | ||
required: schema.required || [], | ||
type: 'object', | ||
}; | ||
} |
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,11 @@ | ||
|
||
export function validateSchema(data:any) { | ||
|
||
if (!data) return "'schema' is required."; | ||
if (!data.title && !data.$id) return "'title' or '$id' is required."; | ||
if (!data.description) return "'description' is required."; | ||
if (!data.properties) return "'properties' is required."; | ||
if (Object.keys(data.properties).length === 0) return "'properties' must contain at least one property."; | ||
return null; | ||
} | ||
|
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.
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.
do you want to take it as a separate variable?
It may help in debug and other reasons later.
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.
If I treat it as a separate variable, I will encounter a type error when passing it to the buildFromProperties function, indicating that the following properties are missing from the type 'ISchema': $id, $schema