Skip to content

Commit 33dc0d8

Browse files
StartAutomatingStartAutomating
authored andcommitted
feat: application/ld+json.GetJsonSchema() ( Fixes #21 )
1 parent f3fba65 commit 33dc0d8

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

JSON-LD.types.ps1xml

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<!-- Generated with EZOut 2.0.6: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
2+
<Types>
3+
<Type>
4+
<Name>application/ld+json</Name>
5+
<Members>
6+
<ScriptMethod>
7+
<Name>GetJsonSchema</Name>
8+
<Script>
9+
10+
param($graph = $this)
11+
12+
if (-not $graph.'@graph') {
13+
if ($graph.'@context' -is [string] -and
14+
$graph.'@type' -is [string]) {
15+
$gotGraph = Get-JsonLD -Url (
16+
$graph.'@context', $graph.'@type' -join '/' -replace '^http:', 'https:'
17+
)
18+
if ($gotGraph.'@graph') {
19+
$graph = $gotGraph
20+
}
21+
}
22+
}
23+
24+
if (-not $graph.'@graph') { return }
25+
26+
$graphTypes = $graph.'@graph' | Group-Object '@type' -AsHashTable
27+
28+
$classes = $graphTypes['rdfs:class']
29+
if (-not $classes) {
30+
return
31+
}
32+
33+
$baseType = $classes |
34+
Where-Object 'rdfs:label' -eq 'thing'
35+
36+
if (-not $baseType) {
37+
return
38+
}
39+
40+
$ClassHierarchy = @(
41+
$baseType
42+
do {
43+
$parentType = $classes |
44+
Where-Object {
45+
$_.'rdfs:subClassOf'.'@id' -eq $baseType.'@id'
46+
}
47+
if ($parentType) {
48+
$parentType
49+
$baseType = $parentType
50+
}
51+
} while ($parentType)
52+
)
53+
54+
if (-not $ClassHierarchy) { return }
55+
56+
$classInfo = $ClassHierarchy[-1]
57+
58+
$jsonSchema = [Ordered]@{
59+
'$id' = "`$$($classInfo.'@id' -replace 'schema:', 'schema.org/')"
60+
title = $($classInfo.'@id' -replace 'schema:', 'https://schema.org/')
61+
description = $classInfo.'rdfs:comment'
62+
properties = [Ordered]@{}
63+
}
64+
65+
foreach ($rdfProperty in $graphTypes['rdf:property']) {
66+
$propertyInfo = [Ordered]@{}
67+
switch -regex ($rdfProperty.'@id') {
68+
'(?&gt;date|time)$' {
69+
$propertyInfo['type'] = 'string'
70+
$propertyInfo['format'] = 'date-time'
71+
}
72+
'url$' {
73+
$propertyInfo['type'] = 'string'
74+
$propertyInfo['format'] = 'url'
75+
}
76+
'(?&gt;name|description)$' {
77+
$propertyInfo['type'] = 'string'
78+
}
79+
default {
80+
$propertyInfo['type'] = 'object'
81+
}
82+
}
83+
if (@($rdfProperty.'schema:rangeIncludes').Count -eq 1) {
84+
switch ($rdfProperty.'schema:rangeIncludes') {
85+
schema:Boolean {
86+
$propertyInfo['type'] = 'boolean'
87+
}
88+
schema:Integer {
89+
$propertyInfo['type'] = 'integer'
90+
}
91+
schema:Number {
92+
$propertyInfo['type'] = 'number'
93+
}
94+
}
95+
}
96+
if ($rdfProperty.'rdfs:comment') {
97+
$propertyInfo['description'] = $rdfProperty.'rdfs:comment'
98+
}
99+
$propertyName = $rdfProperty.'@id' -replace '^schema:'
100+
$jsonSchema.properties[$propertyName] = $propertyInfo
101+
}
102+
$jsonSchema
103+
104+
105+
</Script>
106+
</ScriptMethod>
107+
</Members>
108+
</Type>
109+
</Types>

0 commit comments

Comments
 (0)