nu_scripts/modules/to-json-schema/to-json-schema.nu
Auca Coyan 13a73ab635
🐛 fix more parser errors (#783)
Hi! I reduced some of the errors in the daily CI. Still there are a few
of them, but at least is something

- Added the badge for the `daily.yml` (currently failing)
- removed old `docker` from v0.60
- removed old `git` from auto-generate completions
- removed `nethack` from auto-generate completions (wasn't very useful)
- removed `root` from auto-generate completions (wasn't very useful)
- removed `valgrind` from auto-generate completions (wasn't very useful)
- moved `less` from auto-generate to custom-completions.
- moved `mix` from auto-generate to custom-completions.
- moved `tar` from auto-generate to custom-completions.
- moved `tcpdump` from auto-generate to custom-completions.
- moved `virsh` from auto-generate to custom-completions.
- moved `zef` from auto-generate to custom-completions.
- fixed `base16.nu`
- fixed `from-cpuinfo.nu`
- fixed `from-dmicode.nu`
- fixed `to-number-format.nu`
- fixed `to-json-schema.nu`
2024-03-15 21:10:27 -05:00

79 lines
2.6 KiB
Plaintext

# Converts specific JSON to JSON schema.
def to-json-schema [schema: int, url: string, json: string] {
if ($schema != 4 or $schema != 7) {
error make { msg: $"'http://json-schema.org/draft-0($schema)/schema' is not supported yet." }
}
if url == "" {
error make { msg: "Documentation url can't be empty." }
}
let schema = $"http://json-schema.org/draft-0($schema)/schema"
$json | jq 'def to_title: . | gsub("(?<x>[A-Z])"; " \(.x)") | ascii_downcase;
def to_description: . | to_title + "\n" + $url;
def to_singular:
if . | test("^[aeiouy]"; "i") then
. | sub("^(?<x>.*?)s\n"; "An \(.x)\n")
else
. | sub("^(?<x>.*?)s\n"; "A \(.x)\n")
end;
def wrap: {
type: "object",
properties: with_entries(
if .value | type != "object" then
{
key,
value: ({
title: .key | to_title,
description: .key | to_description,
type: (.value | type),
} +
if .key | test("width|height|size") then
{ minimum: 0 }
else
{}
end +
if .value | type == "array" then
{
uniqueItems: true,
items: {
description: .key | to_description | to_singular
}
} *
if .value | length > 0 then
{
items: {
type: .value[0] | type
}
}
else
{}
end
else
{}
end +
if .key | test("^([Mm]y|[Ss]ample|[Ee]xample)[A-Z]") then
{ examples: [.value] }
else
{ default: .value }
end)
}
else
.value = {
title: .key | to_title,
description: .key | to_description
} + (.value | wrap) + {
additionalProperties: false
}
end)
};
{
"$schema": $schema,
title: "config",
description: "A config"
} + (. | wrap)' --arg url $url --arg schema $schema
}