-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from ashwin275/feature/api-dynamic-payload-sup…
…port feat: add dynamic payload support in schema controller
- Loading branch information
Showing
4 changed files
with
135 additions
and
10 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
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; | ||
} | ||
|