Skip to content

Commit f3fba65

Browse files
feat: application/ld+json.GetJsonSchema() ( Fixes #21 )
1 parent adede61 commit f3fba65

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

Types/JsonLD/GetJsonSchema.ps1

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

Types/JsonLD/PSTypeName.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
application/ld+json

0 commit comments

Comments
 (0)