Skip to content

Commit 535c8d2

Browse files
Task-59 : Added ConvertTo-Definition to the psd1
1 parent e70e2b7 commit 535c8d2

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

schema/schema.psm1

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,14 @@ class schemaDocument {
296296
}
297297
}
298298
}
299+
class schemaDefinition {
300+
[ValidateSet('object')]
301+
[string]$type = 'object'
302+
[object]$properties
303+
[string[]]$required
304+
305+
schemaDefinition () {}
306+
}
299307
$Global:RawSchema = $null;
300308
function Get-Document {
301309
[CmdletBinding(
@@ -362,6 +370,35 @@ function Get-Definition {
362370
}
363371
}
364372
}
373+
function ConvertTo-Definition {
374+
[CmdletBinding(
375+
HelpURI = 'https://github.com/SchemaModule/PowerShell/blob/master/docs/ConvertTo-SchemaDefinition.md#convertto-schemadefinition',
376+
PositionalBinding = $true)]
377+
#[OutputType([string],[schemaDefinition])]
378+
param (
379+
[Parameter(ValueFromPipeline)]
380+
[object]$Definition,
381+
[switch]$AsJson
382+
)
383+
process {
384+
Write-Verbose "Creating New Definition";
385+
$NewDefinition = New-SchemaElement -Type definition;
386+
$NewDefinition.required = $Definition.required;
387+
$NewDefinition.properties = $Definition.properties;
388+
foreach ($defProperty in $Definition.properties.psobject.properties.name) {
389+
write-verbose "Definition Property : $($defProperty)"
390+
if ($NewDefinition.properties.($defProperty).type) {
391+
write-verbose "Found type property"
392+
$NewDefinition.properties.($defProperty).type = $Definition.properties.($defProperty).type.split(' ')[0]
393+
}
394+
}
395+
if ($AsJson) {
396+
return ($NewDefinition |ConvertTo-Json -Depth 99)
397+
} else {
398+
return $NewDefinition
399+
}
400+
}
401+
}
365402
function Get-Reference {
366403
[CmdletBinding(
367404
HelpURI = 'https://github.com/SchemaModule/PowerShell/blob/master/docs/Get-SchemaReference.md#get-schemareference',
@@ -557,7 +594,7 @@ function New-Property {
557594
[ValidateNotNullOrEmpty()]
558595
[string]$Name,
559596

560-
[ValidateSet([schemaDocument], [schemaNumber], [schemaInteger], [schemaString], [schemaObject], [schemaArray], [schemaBoolean])]
597+
[ValidateSet([schemaDocument], [schemaNumber], [schemaInteger], [schemaString], [schemaObject], [schemaArray], [schemaBoolean],[schemaDefinition])]
561598
$Value,
562599

563600
$Array
@@ -578,7 +615,7 @@ function New-Element {
578615
PositionalBinding = $true)]
579616
[OutputType([schemaDocument], [schemaString], [schemaInteger], [schemaNumber], [schemaBoolean], [schemaObject], [schemaArray])]
580617
param (
581-
[ValidateSet('string', 'number', 'integer', 'object', 'boolean', 'array', 'document')]
618+
[ValidateSet('string', 'number', 'integer', 'object', 'boolean', 'array', 'document', 'definition')]
582619
[string]$Type
583620
)
584621
if ($PSCmdlet.ShouldProcess("NewElement")) {
@@ -605,6 +642,9 @@ function New-Element {
605642
'document' {
606643
[schemaDocument]::new()
607644
}
645+
'definition' {
646+
[schemaDefinition]::new()
647+
}
608648
}
609649
}
610650
}
@@ -834,6 +874,14 @@ function ConvertTo-Element {
834874
write-verbose "Found `$schema property"
835875
$Result.schema = $Object.$prop
836876
}
877+
elseif ($prop -eq 'definitions') {
878+
write-verbose "Found Definitions"
879+
write-verbose $object.$prop;
880+
foreach ($definition in $object.$prop.psobject.properties) {
881+
write-verbose "Definition Name : $($Definition.Name)"
882+
$Result.definitions += ((New-SchemaProperty -Name $definition.Name -Value ($definition.Value |ConvertTo-SchemaDefinition)))
883+
}
884+
}
837885
else {
838886
if ($Object.$prop.GetType().IsArray)
839887
{

0 commit comments

Comments
 (0)