Aschema-cplusplus/test/inputs/schema/pattern-properties.schema/default/quicktype.hpp+35 −0
| @@ -0,0 +1,35 @@ | ||
| 1 | +// To parse this JSON data, first install | |
| 2 | +// | |
| 3 | +// json.hpp https://github.com/nlohmann/json | |
| 4 | +// | |
| 5 | +// Then include this file, and then do | |
| 6 | +// | |
| 7 | +// TopLevel data = nlohmann::json::parse(jsonString); | |
| 8 | + | |
| 9 | +#pragma once | |
| 10 | + | |
| 11 | +#include "json.hpp" | |
| 12 | + | |
| 13 | +#include <optional> | |
| 14 | +#include <stdexcept> | |
| 15 | +#include <regex> | |
| 16 | + | |
| 17 | +namespace quicktype { | |
| 18 | + using nlohmann::json; | |
| 19 | + | |
| 20 | + #ifndef NLOHMANN_UNTYPED_quicktype_HELPER | |
| 21 | + #define NLOHMANN_UNTYPED_quicktype_HELPER | |
| 22 | + inline json get_untyped(const json & j, const char * property) { | |
| 23 | + if (j.find(property) != j.end()) { | |
| 24 | + return j.at(property).get<json>(); | |
| 25 | + } | |
| 26 | + return json(); | |
| 27 | + } | |
| 28 | + | |
| 29 | + inline json get_untyped(const json & j, std::string property) { | |
| 30 | + return get_untyped(j, property.data()); | |
| 31 | + } | |
| 32 | + #endif | |
| 33 | + | |
| 34 | + using TopLevel = std::map<std::string, bool>; | |
| 35 | +} |
Aschema-csharp-SystemTextJson/test/inputs/schema/pattern-properties.schema/default/QuickType.cs+55 −0
| @@ -0,0 +1,55 @@ | ||
| 1 | +// <auto-generated /> | |
| 2 | +// | |
| 3 | +// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do: | |
| 4 | +// | |
| 5 | +// using QuickType; | |
| 6 | +// | |
| 7 | +// var topLevel = TopLevel.FromJson(jsonString); | |
| 8 | +#nullable enable | |
| 9 | +#pragma warning disable CS8618 | |
| 10 | +#pragma warning disable CS8601 | |
| 11 | +#pragma warning disable CS8602 | |
| 12 | +#pragma warning disable CS8603 | |
| 13 | +#pragma warning disable CS8604 | |
| 14 | +#pragma warning disable CS8625 | |
| 15 | +#pragma warning disable CS8765 | |
| 16 | + | |
| 17 | +namespace QuickType | |
| 18 | +{ | |
| 19 | + using System; | |
| 20 | + using System.Collections.Generic; | |
| 21 | + | |
| 22 | + using System.Globalization; | |
| 23 | + using Newtonsoft.Json; | |
| 24 | + using Newtonsoft.Json.Converters; | |
| 25 | + | |
| 26 | + public class TopLevel | |
| 27 | + { | |
| 28 | + public static Dictionary<string, bool> FromJson(string json) => JsonConvert.DeserializeObject<Dictionary<string, bool>>(json, QuickType.Converter.Settings); | |
| 29 | + } | |
| 30 | + | |
| 31 | + public static partial class Serialize | |
| 32 | + { | |
| 33 | + public static string ToJson(this Dictionary<string, bool> self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings); | |
| 34 | + } | |
| 35 | + | |
| 36 | + internal static partial class Converter | |
| 37 | + { | |
| 38 | + public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings | |
| 39 | + { | |
| 40 | + MetadataPropertyHandling = MetadataPropertyHandling.Ignore, | |
| 41 | + DateParseHandling = DateParseHandling.None, | |
| 42 | + Converters = | |
| 43 | + { | |
| 44 | + new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } | |
| 45 | + }, | |
| 46 | + }; | |
| 47 | + } | |
| 48 | +} | |
| 49 | +#pragma warning restore CS8618 | |
| 50 | +#pragma warning restore CS8601 | |
| 51 | +#pragma warning restore CS8602 | |
| 52 | +#pragma warning restore CS8603 | |
| 53 | +#pragma warning restore CS8604 | |
| 54 | +#pragma warning restore CS8625 | |
| 55 | +#pragma warning restore CS8765 |
Aschema-csharp/test/inputs/schema/pattern-properties.schema/default/QuickType.cs+159 −0
| @@ -0,0 +1,159 @@ | ||
| 1 | +// <auto-generated /> | |
| 2 | +// | |
| 3 | +// To parse this JSON data, add NuGet 'System.Text.Json' then do: | |
| 4 | +// | |
| 5 | +// using QuickType; | |
| 6 | +// | |
| 7 | +// var topLevel = TopLevel.FromJson(jsonString); | |
| 8 | +#nullable enable | |
| 9 | +#pragma warning disable CS8618 | |
| 10 | +#pragma warning disable CS8601 | |
| 11 | +#pragma warning disable CS8602 | |
| 12 | +#pragma warning disable CS8603 | |
| 13 | + | |
| 14 | +namespace QuickType | |
| 15 | +{ | |
| 16 | + using System; | |
| 17 | + using System.Collections.Generic; | |
| 18 | + | |
| 19 | + using System.Text.Json; | |
| 20 | + using System.Text.Json.Serialization; | |
| 21 | + using System.Globalization; | |
| 22 | + | |
| 23 | + public class TopLevel | |
| 24 | + { | |
| 25 | + public static Dictionary<string, bool> FromJson(string json) => JsonSerializer.Deserialize<Dictionary<string, bool>>(json, QuickType.Converter.Settings); | |
| 26 | + } | |
| 27 | + | |
| 28 | + public static partial class Serialize | |
| 29 | + { | |
| 30 | + public static string ToJson(this Dictionary<string, bool> self) => JsonSerializer.Serialize(self, QuickType.Converter.Settings); | |
| 31 | + } | |
| 32 | + | |
| 33 | + internal static partial class Converter | |
| 34 | + { | |
| 35 | + public static readonly JsonSerializerOptions Settings = new(JsonSerializerDefaults.General) | |
| 36 | + { | |
| 37 | + Converters = | |
| 38 | + { | |
| 39 | + new DateOnlyConverter(), | |
| 40 | + new TimeOnlyConverter(), | |
| 41 | + IsoDateTimeOffsetConverter.Singleton | |
| 42 | + }, | |
| 43 | + }; | |
| 44 | + } | |
| 45 | + | |
| 46 | + public class DateOnlyConverter : JsonConverter<DateOnly> | |
| 47 | + { | |
| 48 | + private readonly string serializationFormat; | |
| 49 | + public DateOnlyConverter() : this(null) { } | |
| 50 | + | |
| 51 | + public DateOnlyConverter(string? serializationFormat) | |
| 52 | + { | |
| 53 | + this.serializationFormat = serializationFormat ?? "yyyy-MM-dd"; | |
| 54 | + } | |
| 55 | + | |
| 56 | + public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 57 | + { | |
| 58 | + var value = reader.GetString(); | |
| 59 | + return DateOnly.Parse(value!); | |
| 60 | + } | |
| 61 | + | |
| 62 | + public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options) | |
| 63 | + => writer.WriteStringValue(value.ToString(serializationFormat)); | |
| 64 | + } | |
| 65 | + | |
| 66 | + public class TimeOnlyConverter : JsonConverter<TimeOnly> | |
| 67 | + { | |
| 68 | + private readonly string serializationFormat; | |
| 69 | + | |
| 70 | + public TimeOnlyConverter() : this(null) { } | |
| 71 | + | |
| 72 | + public TimeOnlyConverter(string? serializationFormat) | |
| 73 | + { | |
| 74 | + this.serializationFormat = serializationFormat ?? "HH:mm:ss.fff"; | |
| 75 | + } | |
| 76 | + | |
| 77 | + public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 78 | + { | |
| 79 | + var value = reader.GetString(); | |
| 80 | + return TimeOnly.Parse(value!); | |
| 81 | + } | |
| 82 | + | |
| 83 | + public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options) | |
| 84 | + => writer.WriteStringValue(value.ToString(serializationFormat)); | |
| 85 | + } | |
| 86 | + | |
| 87 | + internal class IsoDateTimeOffsetConverter : JsonConverter<DateTimeOffset> | |
| 88 | + { | |
| 89 | + public override bool CanConvert(Type t) => t == typeof(DateTimeOffset); | |
| 90 | + | |
| 91 | + private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK"; | |
| 92 | + | |
| 93 | + private DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind; | |
| 94 | + private string? _dateTimeFormat; | |
| 95 | + private CultureInfo? _culture; | |
| 96 | + | |
| 97 | + public DateTimeStyles DateTimeStyles | |
| 98 | + { | |
| 99 | + get => _dateTimeStyles; | |
| 100 | + set => _dateTimeStyles = value; | |
| 101 | + } | |
| 102 | + | |
| 103 | + public string? DateTimeFormat | |
| 104 | + { | |
| 105 | + get => _dateTimeFormat ?? string.Empty; | |
| 106 | + set => _dateTimeFormat = (string.IsNullOrEmpty(value)) ? null : value; | |
| 107 | + } | |
| 108 | + | |
| 109 | + public CultureInfo Culture | |
| 110 | + { | |
| 111 | + get => _culture ?? CultureInfo.CurrentCulture; | |
| 112 | + set => _culture = value; | |
| 113 | + } | |
| 114 | + | |
| 115 | + public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) | |
| 116 | + { | |
| 117 | + string text; | |
| 118 | + | |
| 119 | + | |
| 120 | + if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal | |
| 121 | + || (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal) | |
| 122 | + { | |
| 123 | + value = value.ToUniversalTime(); | |
| 124 | + } | |
| 125 | + | |
| 126 | + text = value.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture); | |
| 127 | + | |
| 128 | + writer.WriteStringValue(text); | |
| 129 | + } | |
| 130 | + | |
| 131 | + public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 132 | + { | |
| 133 | + string? dateText = reader.GetString(); | |
| 134 | + | |
| 135 | + if (string.IsNullOrEmpty(dateText) == false) | |
| 136 | + { | |
| 137 | + if (!string.IsNullOrEmpty(_dateTimeFormat)) | |
| 138 | + { | |
| 139 | + return DateTimeOffset.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles); | |
| 140 | + } | |
| 141 | + else | |
| 142 | + { | |
| 143 | + return DateTimeOffset.Parse(dateText, Culture, _dateTimeStyles); | |
| 144 | + } | |
| 145 | + } | |
| 146 | + else | |
| 147 | + { | |
| 148 | + return default(DateTimeOffset); | |
| 149 | + } | |
| 150 | + } | |
| 151 | + | |
| 152 | + | |
| 153 | + public static readonly IsoDateTimeOffsetConverter Singleton = new IsoDateTimeOffsetConverter(); | |
| 154 | + } | |
| 155 | +} | |
| 156 | +#pragma warning restore CS8618 | |
| 157 | +#pragma warning restore CS8601 | |
| 158 | +#pragma warning restore CS8602 | |
| 159 | +#pragma warning restore CS8603 |
Aschema-dart/test/inputs/schema/pattern-properties.schema/default/TopLevel.dart+9 −0
| @@ -0,0 +1,9 @@ | ||
| 1 | +// To parse this JSON data, do | |
| 2 | +// | |
| 3 | +// final topLevel = topLevelFromJson(jsonString); | |
| 4 | + | |
| 5 | +import 'dart:convert'; | |
| 6 | + | |
| 7 | +Map<String, bool> topLevelFromJson(String str) => Map.from(json.decode(str)).map((k, v) => MapEntry<String, bool>(k, v)); | |
| 8 | + | |
| 9 | +String topLevelToJson(Map<String, bool> data) => json.encode(Map.from(data).map((k, v) => MapEntry<String, dynamic>(k, v))); |
Aschema-elm/test/inputs/schema/pattern-properties.schema/default/QuickType.elm+41 −0
| @@ -0,0 +1,41 @@ | ||
| 1 | +-- To decode the JSON data, add this file to your project, run | |
| 2 | +-- | |
| 3 | +-- elm install NoRedInk/elm-json-decode-pipeline | |
| 4 | +-- | |
| 5 | +-- add these imports | |
| 6 | +-- | |
| 7 | +-- import Json.Decode exposing (decodeString) | |
| 8 | +-- import QuickType exposing (quickType) | |
| 9 | +-- | |
| 10 | +-- and you're off to the races with | |
| 11 | +-- | |
| 12 | +-- decodeString quickType myJsonString | |
| 13 | + | |
| 14 | +module QuickType exposing | |
| 15 | + ( QuickType | |
| 16 | + , quickTypeToString | |
| 17 | + , quickType | |
| 18 | + ) | |
| 19 | + | |
| 20 | +import Json.Decode as Jdec | |
| 21 | +import Json.Decode.Pipeline as Jpipe | |
| 22 | +import Json.Encode as Jenc | |
| 23 | +import Dict exposing (Dict) | |
| 24 | + | |
| 25 | +type alias QuickType = Dict String Bool | |
| 26 | + | |
| 27 | +-- decoders and encoders | |
| 28 | + | |
| 29 | +quickType : Jdec.Decoder QuickType | |
| 30 | +quickType = Jdec.dict Jdec.bool | |
| 31 | + | |
| 32 | +quickTypeToString : QuickType -> String | |
| 33 | +quickTypeToString r = Jenc.encode 0 (Jenc.dict identity Jenc.bool r) | |
| 34 | + | |
| 35 | +--- encoder helpers | |
| 36 | + | |
| 37 | +makeNullableEncoder : (a -> Jenc.Value) -> Maybe a -> Jenc.Value | |
| 38 | +makeNullableEncoder f m = | |
| 39 | + case m of | |
| 40 | + Just x -> f x | |
| 41 | + Nothing -> Jenc.null |
Aschema-flow/test/inputs/schema/pattern-properties.schema/default/TopLevel.js+181 −0
| @@ -0,0 +1,181 @@ | ||
| 1 | +// @flow | |
| 2 | + | |
| 3 | +// To parse this data: | |
| 4 | +// | |
| 5 | +// const Convert = require("./TopLevel"); | |
| 6 | +// | |
| 7 | +// const topLevel = Convert.toTopLevel(json); | |
| 8 | +// | |
| 9 | +// These functions will throw an error if the JSON doesn't | |
| 10 | +// match the expected interface, even if the JSON is valid. | |
| 11 | + | |
| 12 | +// Converts JSON strings to/from your types | |
| 13 | +// and asserts the results of JSON.parse at runtime | |
| 14 | +function toTopLevel(json: string): { [key: string]: boolean } { | |
| 15 | + return cast(JSON.parse(json), m(true)); | |
| 16 | +} | |
| 17 | + | |
| 18 | +function topLevelToJson(value: { [key: string]: boolean }): string { | |
| 19 | + return JSON.stringify(uncast(value, m(true)), null, 2); | |
| 20 | +} | |
| 21 | + | |
| 22 | +function invalidValue(typ: any, val: any, key: any, parent: any = '') { | |
| 23 | + const prettyTyp = prettyTypeName(typ); | |
| 24 | + const parentText = parent ? ` on ${parent}` : ''; | |
| 25 | + const keyText = key ? ` for key "${key}"` : ''; | |
| 26 | + throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`); | |
| 27 | +} | |
| 28 | + | |
| 29 | +function prettyTypeName(typ: any): string { | |
| 30 | + if (Array.isArray(typ)) { | |
| 31 | + if (typ.length === 2 && typ[0] === undefined) { | |
| 32 | + return `an optional ${prettyTypeName(typ[1])}`; | |
| 33 | + } else { | |
| 34 | + return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`; | |
| 35 | + } | |
| 36 | + } else if (typeof typ === "object" && typ.literal !== undefined) { | |
| 37 | + return typ.literal; | |
| 38 | + } else { | |
| 39 | + return typeof typ; | |
| 40 | + } | |
| 41 | +} | |
| 42 | + | |
| 43 | +function jsonToJSProps(typ: any): any { | |
| 44 | + if (typ.jsonToJS === undefined) { | |
| 45 | + const map: any = {}; | |
| 46 | + typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ }); | |
| 47 | + typ.jsonToJS = map; | |
| 48 | + } | |
| 49 | + return typ.jsonToJS; | |
| 50 | +} | |
| 51 | + | |
| 52 | +function jsToJSONProps(typ: any): any { | |
| 53 | + if (typ.jsToJSON === undefined) { | |
| 54 | + const map: any = {}; | |
| 55 | + typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ }); | |
| 56 | + typ.jsToJSON = map; | |
| 57 | + } | |
| 58 | + return typ.jsToJSON; | |
| 59 | +} | |
| 60 | + | |
| 61 | +function transform(val: any, typ: any, getProps: any, key: any = '', parent: any = ''): any { | |
| 62 | + function transformPrimitive(typ: string, val: any): any { | |
| 63 | + if (typeof typ === typeof val) return val; | |
| 64 | + return invalidValue(typ, val, key, parent); | |
| 65 | + } | |
| 66 | + | |
| 67 | + function transformUnion(typs: any[], val: any): any { | |
| 68 | + // val must validate against one typ in typs | |
| 69 | + const l = typs.length; | |
| 70 | + for (let i = 0; i < l; i++) { | |
| 71 | + const typ = typs[i]; | |
| 72 | + try { | |
| 73 | + return transform(val, typ, getProps); | |
| 74 | + } catch (_) {} | |
| 75 | + } | |
| 76 | + return invalidValue(typs, val, key, parent); | |
| 77 | + } | |
| 78 | + | |
| 79 | + function transformEnum(cases: string[], val: any): any { | |
| 80 | + if (cases.indexOf(val) !== -1) return val; | |
| 81 | + return invalidValue(cases.map(a => { return l(a); }), val, key, parent); | |
| 82 | + } | |
| 83 | + | |
| 84 | + function transformArray(typ: any, val: any): any { | |
| 85 | + // val must be an array with no invalid elements | |
| 86 | + if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent); | |
| 87 | + return val.map(el => transform(el, typ, getProps)); | |
| 88 | + } | |
| 89 | + | |
| 90 | + function transformDate(val: any): any { | |
| 91 | + if (val === null) { | |
| 92 | + return null; | |
| 93 | + } | |
| 94 | + const d = new Date(val); | |
| 95 | + if (isNaN(d.valueOf())) { | |
| 96 | + return invalidValue(l("Date"), val, key, parent); | |
| 97 | + } | |
| 98 | + return d; | |
| 99 | + } | |
| 100 | + | |
| 101 | + function transformObject(props: { [k: string]: any }, additional: any, val: any): any { | |
| 102 | + if (val === null || typeof val !== "object" || Array.isArray(val)) { | |
| 103 | + return invalidValue(l(ref || "object"), val, key, parent); | |
| 104 | + } | |
| 105 | + const result: any = {}; | |
| 106 | + Object.getOwnPropertyNames(props).forEach(key => { | |
| 107 | + const prop = props[key]; | |
| 108 | + const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined; | |
| 109 | + result[prop.key] = transform(v, prop.typ, getProps, key, ref); | |
| 110 | + }); | |
| 111 | + Object.getOwnPropertyNames(val).forEach(key => { | |
| 112 | + if (!Object.prototype.hasOwnProperty.call(props, key)) { | |
| 113 | + result[key] = transform(val[key], additional, getProps, key, ref); | |
| 114 | + } | |
| 115 | + }); | |
| 116 | + return result; | |
| 117 | + } | |
| 118 | + | |
| 119 | + if (typ === "any") return val; | |
| 120 | + if (typ === null) { | |
| 121 | + if (val === null) return val; | |
| 122 | + return invalidValue(typ, val, key, parent); | |
| 123 | + } | |
| 124 | + if (typ === false) return invalidValue(typ, val, key, parent); | |
| 125 | + let ref: any = undefined; | |
| 126 | + while (typeof typ === "object" && typ.ref !== undefined) { | |
| 127 | + ref = typ.ref; | |
| 128 | + typ = typeMap[typ.ref]; | |
| 129 | + } | |
| 130 | + if (Array.isArray(typ)) return transformEnum(typ, val); | |
| 131 | + if (typeof typ === "object") { | |
| 132 | + return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val) | |
| 133 | + : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val) | |
| 134 | + : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val) | |
| 135 | + : invalidValue(typ, val, key, parent); | |
| 136 | + } | |
| 137 | + // Numbers can be parsed by Date but shouldn't be. | |
| 138 | + if (typ === Date && typeof val !== "number") return transformDate(val); | |
| 139 | + return transformPrimitive(typ, val); | |
| 140 | +} | |
| 141 | + | |
| 142 | +function cast<T>(val: any, typ: any): T { | |
| 143 | + return transform(val, typ, jsonToJSProps); | |
| 144 | +} | |
| 145 | + | |
| 146 | +function uncast<T>(val: T, typ: any): any { | |
| 147 | + return transform(val, typ, jsToJSONProps); | |
| 148 | +} | |
| 149 | + | |
| 150 | +function l(typ: any) { | |
| 151 | + return { literal: typ }; | |
| 152 | +} | |
| 153 | + | |
| 154 | +function a(typ: any) { | |
| 155 | + return { arrayItems: typ }; | |
| 156 | +} | |
| 157 | + | |
| 158 | +function u(...typs: any[]) { | |
| 159 | + return { unionMembers: typs }; | |
| 160 | +} | |
| 161 | + | |
| 162 | +function o(props: any[], additional: any) { | |
| 163 | + return { props, additional }; | |
| 164 | +} | |
| 165 | + | |
| 166 | +function m(additional: any) { | |
| 167 | + const props: any[] = []; | |
| 168 | + return { props, additional }; | |
| 169 | +} | |
| 170 | + | |
| 171 | +function r(name: string) { | |
| 172 | + return { ref: name }; | |
| 173 | +} | |
| 174 | + | |
| 175 | +const typeMap: any = { | |
| 176 | +}; | |
| 177 | + | |
| 178 | +module.exports = { | |
| 179 | + "topLevelToJson": topLevelToJson, | |
| 180 | + "toTopLevel": toTopLevel, | |
| 181 | +}; |
Aschema-golang/test/inputs/schema/pattern-properties.schema/default/quicktype.go+21 −0
| @@ -0,0 +1,21 @@ | ||
| 1 | +// Code generated from JSON Schema using quicktype. DO NOT EDIT. | |
| 2 | +// To parse and unparse this JSON data, add this code to your project and do: | |
| 3 | +// | |
| 4 | +// topLevel, err := UnmarshalTopLevel(bytes) | |
| 5 | +// bytes, err = topLevel.Marshal() | |
| 6 | + | |
| 7 | +package main | |
| 8 | + | |
| 9 | +import "encoding/json" | |
| 10 | + | |
| 11 | +type TopLevel map[string]bool | |
| 12 | + | |
| 13 | +func UnmarshalTopLevel(data []byte) (TopLevel, error) { | |
| 14 | + var r TopLevel | |
| 15 | + err := json.Unmarshal(data, &r) | |
| 16 | + return r, err | |
| 17 | +} | |
| 18 | + | |
| 19 | +func (r *TopLevel) Marshal() ([]byte, error) { | |
| 20 | + return json.Marshal(r) | |
| 21 | +} |
Aschema-javascript/test/inputs/schema/pattern-properties.schema/default/TopLevel.js+179 −0
| @@ -0,0 +1,179 @@ | ||
| 1 | +// To parse this data: | |
| 2 | +// | |
| 3 | +// const Convert = require("./TopLevel"); | |
| 4 | +// | |
| 5 | +// const topLevel = Convert.toTopLevel(json); | |
| 6 | +// | |
| 7 | +// These functions will throw an error if the JSON doesn't | |
| 8 | +// match the expected interface, even if the JSON is valid. | |
| 9 | + | |
| 10 | +// Converts JSON strings to/from your types | |
| 11 | +// and asserts the results of JSON.parse at runtime | |
| 12 | +function toTopLevel(json) { | |
| 13 | + return cast(JSON.parse(json), m(true)); | |
| 14 | +} | |
| 15 | + | |
| 16 | +function topLevelToJson(value) { | |
| 17 | + return JSON.stringify(uncast(value, m(true)), null, 2); | |
| 18 | +} | |
| 19 | + | |
| 20 | +function invalidValue(typ, val, key, parent = '') { | |
| 21 | + const prettyTyp = prettyTypeName(typ); | |
| 22 | + const parentText = parent ? ` on ${parent}` : ''; | |
| 23 | + const keyText = key ? ` for key "${key}"` : ''; | |
| 24 | + throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`); | |
| 25 | +} | |
| 26 | + | |
| 27 | +function prettyTypeName(typ) { | |
| 28 | + if (Array.isArray(typ)) { | |
| 29 | + if (typ.length === 2 && typ[0] === undefined) { | |
| 30 | + return `an optional ${prettyTypeName(typ[1])}`; | |
| 31 | + } else { | |
| 32 | + return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`; | |
| 33 | + } | |
| 34 | + } else if (typeof typ === "object" && typ.literal !== undefined) { | |
| 35 | + return typ.literal; | |
| 36 | + } else { | |
| 37 | + return typeof typ; | |
| 38 | + } | |
| 39 | +} | |
| 40 | + | |
| 41 | +function jsonToJSProps(typ) { | |
| 42 | + if (typ.jsonToJS === undefined) { | |
| 43 | + const map = {}; | |
| 44 | + typ.props.forEach((p) => map[p.json] = { key: p.js, typ: p.typ }); | |
| 45 | + typ.jsonToJS = map; | |
| 46 | + } | |
| 47 | + return typ.jsonToJS; | |
| 48 | +} | |
| 49 | + | |
| 50 | +function jsToJSONProps(typ) { | |
| 51 | + if (typ.jsToJSON === undefined) { | |
| 52 | + const map = {}; | |
| 53 | + typ.props.forEach((p) => map[p.js] = { key: p.json, typ: p.typ }); | |
| 54 | + typ.jsToJSON = map; | |
| 55 | + } | |
| 56 | + return typ.jsToJSON; | |
| 57 | +} | |
| 58 | + | |
| 59 | +function transform(val, typ, getProps, key = '', parent = '') { | |
| 60 | + function transformPrimitive(typ, val) { | |
| 61 | + if (typeof typ === typeof val) return val; | |
| 62 | + return invalidValue(typ, val, key, parent); | |
| 63 | + } | |
| 64 | + | |
| 65 | + function transformUnion(typs, val) { | |
| 66 | + // val must validate against one typ in typs | |
| 67 | + const l = typs.length; | |
| 68 | + for (let i = 0; i < l; i++) { | |
| 69 | + const typ = typs[i]; | |
| 70 | + try { | |
| 71 | + return transform(val, typ, getProps); | |
| 72 | + } catch (_) {} | |
| 73 | + } | |
| 74 | + return invalidValue(typs, val, key, parent); | |
| 75 | + } | |
| 76 | + | |
| 77 | + function transformEnum(cases, val) { | |
| 78 | + if (cases.indexOf(val) !== -1) return val; | |
| 79 | + return invalidValue(cases.map(a => { return l(a); }), val, key, parent); | |
| 80 | + } | |
| 81 | + | |
| 82 | + function transformArray(typ, val) { | |
| 83 | + // val must be an array with no invalid elements | |
| 84 | + if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent); | |
| 85 | + return val.map(el => transform(el, typ, getProps)); | |
| 86 | + } | |
| 87 | + | |
| 88 | + function transformDate(val) { | |
| 89 | + if (val === null) { | |
| 90 | + return null; | |
| 91 | + } | |
| 92 | + const d = new Date(val); | |
| 93 | + if (isNaN(d.valueOf())) { | |
| 94 | + return invalidValue(l("Date"), val, key, parent); | |
| 95 | + } | |
| 96 | + return d; | |
| 97 | + } | |
| 98 | + | |
| 99 | + function transformObject(props, additional, val) { | |
| 100 | + if (val === null || typeof val !== "object" || Array.isArray(val)) { | |
| 101 | + return invalidValue(l(ref || "object"), val, key, parent); | |
| 102 | + } | |
| 103 | + const result = {}; | |
| 104 | + Object.getOwnPropertyNames(props).forEach(key => { | |
| 105 | + const prop = props[key]; | |
| 106 | + const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined; | |
| 107 | + result[prop.key] = transform(v, prop.typ, getProps, key, ref); | |
| 108 | + }); | |
| 109 | + Object.getOwnPropertyNames(val).forEach(key => { | |
| 110 | + if (!Object.prototype.hasOwnProperty.call(props, key)) { | |
| 111 | + result[key] = transform(val[key], additional, getProps, key, ref); | |
| 112 | + } | |
| 113 | + }); | |
| 114 | + return result; | |
| 115 | + } | |
| 116 | + | |
| 117 | + if (typ === "any") return val; | |
| 118 | + if (typ === null) { | |
| 119 | + if (val === null) return val; | |
| 120 | + return invalidValue(typ, val, key, parent); | |
| 121 | + } | |
| 122 | + if (typ === false) return invalidValue(typ, val, key, parent); | |
| 123 | + let ref = undefined; | |
| 124 | + while (typeof typ === "object" && typ.ref !== undefined) { | |
| 125 | + ref = typ.ref; | |
| 126 | + typ = typeMap[typ.ref]; | |
| 127 | + } | |
| 128 | + if (Array.isArray(typ)) return transformEnum(typ, val); | |
| 129 | + if (typeof typ === "object") { | |
| 130 | + return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val) | |
| 131 | + : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val) | |
| 132 | + : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val) | |
| 133 | + : invalidValue(typ, val, key, parent); | |
| 134 | + } | |
| 135 | + // Numbers can be parsed by Date but shouldn't be. | |
| 136 | + if (typ === Date && typeof val !== "number") return transformDate(val); | |
| 137 | + return transformPrimitive(typ, val); | |
| 138 | +} | |
| 139 | + | |
| 140 | +function cast(val, typ) { | |
| 141 | + return transform(val, typ, jsonToJSProps); | |
| 142 | +} | |
| 143 | + | |
| 144 | +function uncast(val, typ) { | |
| 145 | + return transform(val, typ, jsToJSONProps); | |
| 146 | +} | |
| 147 | + | |
| 148 | +function l(typ) { | |
| 149 | + return { literal: typ }; | |
| 150 | +} | |
| 151 | + | |
| 152 | +function a(typ) { | |
| 153 | + return { arrayItems: typ }; | |
| 154 | +} | |
| 155 | + | |
| 156 | +function u(...typs) { | |
| 157 | + return { unionMembers: typs }; | |
| 158 | +} | |
| 159 | + | |
| 160 | +function o(props, additional) { | |
| 161 | + return { props, additional }; | |
| 162 | +} | |
| 163 | + | |
| 164 | +function m(additional) { | |
| 165 | + const props = []; | |
| 166 | + return { props, additional }; | |
| 167 | +} | |
| 168 | + | |
| 169 | +function r(name) { | |
| 170 | + return { ref: name }; | |
| 171 | +} | |
| 172 | + | |
| 173 | +const typeMap = { | |
| 174 | +}; | |
| 175 | + | |
| 176 | +module.exports = { | |
| 177 | + "topLevelToJson": topLevelToJson, | |
| 178 | + "toTopLevel": toTopLevel, | |
| 179 | +}; |
Aschema-kotlinx/test/inputs/schema/pattern-properties.schema/default/TopLevel.kt+13 −0
| @@ -0,0 +1,13 @@ | ||
| 1 | +// To parse the JSON, install kotlin's serialization plugin and do: | |
| 2 | +// | |
| 3 | +// val json = Json { allowStructuredMapKeys = true } | |
| 4 | +// val topLevel = json.parse(TopLevel.serializer(), jsonString) | |
| 5 | + | |
| 6 | +package quicktype | |
| 7 | + | |
| 8 | +import kotlinx.serialization.* | |
| 9 | +import kotlinx.serialization.json.* | |
| 10 | +import kotlinx.serialization.descriptors.* | |
| 11 | +import kotlinx.serialization.encoding.* | |
| 12 | + | |
| 13 | +typealias TopLevel = HashMap<String, Boolean> |
Aschema-python/test/inputs/schema/pattern-properties.schema/default/quicktype.py+22 −0
| @@ -0,0 +1,22 @@ | ||
| 1 | +from typing import Any, TypeVar, Callable | |
| 2 | + | |
| 3 | + | |
| 4 | +T = TypeVar("T") | |
| 5 | + | |
| 6 | + | |
| 7 | +def from_dict(f: Callable[[Any], T], x: Any) -> dict[str, T]: | |
| 8 | + assert isinstance(x, dict) | |
| 9 | + return { k: f(v) for (k, v) in x.items() } | |
| 10 | + | |
| 11 | + | |
| 12 | +def from_bool(x: Any) -> bool: | |
| 13 | + assert isinstance(x, bool) | |
| 14 | + return x | |
| 15 | + | |
| 16 | + | |
| 17 | +def top_level_from_dict(s: Any) -> dict[str, bool]: | |
| 18 | + return from_dict(from_bool, s) | |
| 19 | + | |
| 20 | + | |
| 21 | +def top_level_to_dict(x: dict[str, bool]) -> Any: | |
| 22 | + return from_dict(from_bool, x) |
Aschema-ruby/test/inputs/schema/pattern-properties.schema/default/TopLevel.rb+26 −0
| @@ -0,0 +1,26 @@ | ||
| 1 | +# This code may look unusually verbose for Ruby (and it is), but | |
| 2 | +# it performs some subtle and complex validation of JSON data. | |
| 3 | +# | |
| 4 | +# To parse this JSON, add 'dry-struct' and 'dry-types' gems, then do: | |
| 5 | +# | |
| 6 | +# top_level = TopLevel.from_json! "{…}" | |
| 7 | +# puts top_level["…"] | |
| 8 | +# | |
| 9 | +# If from_json! succeeds, the value returned matches the schema. | |
| 10 | + | |
| 11 | +require 'json' | |
| 12 | +require 'dry-types' | |
| 13 | +require 'dry-struct' | |
| 14 | + | |
| 15 | +module Types | |
| 16 | + include Dry.Types(default: :nominal) | |
| 17 | + | |
| 18 | + Bool = Strict::Bool | |
| 19 | + Hash = Strict::Hash | |
| 20 | +end | |
| 21 | + | |
| 22 | +class TopLevel | |
| 23 | + def self.from_json!(json) | |
| 24 | + Types::Hash[JSON.parse(json, quirks_mode: true)].map { |k, v| [k, Types::Bool[v]] }.to_h | |
| 25 | + end | |
| 26 | +end |
Aschema-rust/test/inputs/schema/pattern-properties.schema/default/module_under_test.rs+17 −0
| @@ -0,0 +1,17 @@ | ||
| 1 | +// Example code that deserializes and serializes the model. | |
| 2 | +// extern crate serde; | |
| 3 | +// #[macro_use] | |
| 4 | +// extern crate serde_derive; | |
| 5 | +// extern crate serde_json; | |
| 6 | +// | |
| 7 | +// use generated_module::TopLevel; | |
| 8 | +// | |
| 9 | +// fn main() { | |
| 10 | +// let json = r#"{"answer": 42}"#; | |
| 11 | +// let model: TopLevel = serde_json::from_str(&json).unwrap(); | |
| 12 | +// } | |
| 13 | + | |
| 14 | +use serde::{Serialize, Deserialize}; | |
| 15 | +use std::collections::HashMap; | |
| 16 | + | |
| 17 | +pub type TopLevel = HashMap<String, bool>; |
Aschema-scala3-upickle/test/inputs/schema/pattern-properties.schema/default/TopLevel.scala+12 −0
| @@ -0,0 +1,12 @@ | ||
| 1 | +package quicktype | |
| 2 | + | |
| 3 | +import io.circe.syntax._ | |
| 4 | +import io.circe._ | |
| 5 | +import cats.syntax.functor._ | |
| 6 | + | |
| 7 | +// If a union has a null in, then we'll need this too... | |
| 8 | +type NullValue = None.type | |
| 9 | + | |
| 10 | +type TopLevel = Map[String, Boolean] | |
| 11 | + | |
| 12 | +given (using ev : Boolean): Encoder[Map[String, Boolean]] = Encoder.encodeMap[String, Boolean] |
Aschema-scala3/test/inputs/schema/pattern-properties.schema/default/TopLevel.scala+68 −0
| @@ -0,0 +1,68 @@ | ||
| 1 | +package quicktype | |
| 2 | + | |
| 3 | +// Custom pickler so that missing keys and JSON nulls both read as None, | |
| 4 | +// and None is left out when writing (upickle's default for Option is a | |
| 5 | +// JSON array). | |
| 6 | +object OptionPickler extends upickle.AttributeTagged: | |
| 7 | + import upickle.default.Writer | |
| 8 | + import upickle.default.Reader | |
| 9 | + override implicit def OptionWriter[T: Writer]: Writer[Option[T]] = | |
| 10 | + implicitly[Writer[T]].comap[Option[T]] { | |
| 11 | + case None => null.asInstanceOf[T] | |
| 12 | + case Some(x) => x | |
| 13 | + } | |
| 14 | + | |
| 15 | + override implicit def OptionReader[T: Reader]: Reader[Option[T]] = { | |
| 16 | + new Reader.Delegate[Any, Option[T]](implicitly[Reader[T]].map(Some(_))){ | |
| 17 | + override def visitNull(index: Int) = None | |
| 18 | + } | |
| 19 | + } | |
| 20 | +end OptionPickler | |
| 21 | + | |
| 22 | +// If a union has a null in, then we'll need this too... | |
| 23 | +type NullValue = None.type | |
| 24 | +given OptionPickler.ReadWriter[NullValue] = OptionPickler.readwriter[ujson.Value].bimap[NullValue]( | |
| 25 | + _ => ujson.Null, | |
| 26 | + json => if json.isNull then None else throw new upickle.core.Abort("not null") | |
| 27 | +) | |
| 28 | + | |
| 29 | +object JsonExt: | |
| 30 | + val valueReader = OptionPickler.readwriter[ujson.Value] | |
| 31 | + | |
| 32 | + // upickle's built-in primitive readers are lenient -- the numeric and | |
| 33 | + // boolean readers accept strings, and the string reader accepts | |
| 34 | + // numbers and booleans -- so untagged unions need strict readers to | |
| 35 | + // pick the right member. | |
| 36 | + val strictString: OptionPickler.Reader[String] = valueReader.map { | |
| 37 | + case ujson.Str(s) => s | |
| 38 | + case json => throw new upickle.core.Abort("expected string, got " + json) | |
| 39 | + } | |
| 40 | + val strictLong: OptionPickler.Reader[Long] = valueReader.map { | |
| 41 | + case ujson.Num(n) if n.isWhole => n.toLong | |
| 42 | + case json => throw new upickle.core.Abort("expected integer, got " + json) | |
| 43 | + } | |
| 44 | + val strictDouble: OptionPickler.Reader[Double] = valueReader.map { | |
| 45 | + case ujson.Num(n) => n | |
| 46 | + case json => throw new upickle.core.Abort("expected number, got " + json) | |
| 47 | + } | |
| 48 | + val strictBoolean: OptionPickler.Reader[Boolean] = valueReader.map { | |
| 49 | + case ujson.Bool(b) => b | |
| 50 | + case json => throw new upickle.core.Abort("expected boolean, got " + json) | |
| 51 | + } | |
| 52 | + | |
| 53 | + def badMerge[T](r1: => OptionPickler.Reader[?], rest: OptionPickler.Reader[?]*): OptionPickler.Reader[T] = valueReader.map { json => | |
| 54 | + var t: T | Null = null | |
| 55 | + val stack = Vector.newBuilder[Throwable] | |
| 56 | + (r1 +: rest).foreach { reader => | |
| 57 | + if t == null then | |
| 58 | + try | |
| 59 | + t = OptionPickler.read[T](json, trace = true)(using reader.asInstanceOf[OptionPickler.Reader[T]]) | |
| 60 | + catch | |
| 61 | + case exc => stack += exc | |
| 62 | + } | |
| 63 | + if t != null then t.nn else throw new Exception(json.toString(), stack.result().headOption.getOrElse(null)) | |
| 64 | + } | |
| 65 | +end JsonExt | |
| 66 | + | |
| 67 | + | |
| 68 | +type TopLevel = Map[String, Boolean] |
Aschema-schema/test/inputs/schema/pattern-properties.schema/default/TopLevel.schema+8 −0
| @@ -0,0 +1,8 @@ | ||
| 1 | +{ | |
| 2 | + "$schema": "http://json-schema.org/draft-06/schema#", | |
| 3 | + "type": "object", | |
| 4 | + "additionalProperties": { | |
| 5 | + "type": "boolean" | |
| 6 | + }, | |
| 7 | + "definitions": {} | |
| 8 | +} |
Aschema-typescript/test/inputs/schema/pattern-properties.schema/default/TopLevel.ts+176 −0
| @@ -0,0 +1,176 @@ | ||
| 1 | +// To parse this data: | |
| 2 | +// | |
| 3 | +// import { Convert } from "./TopLevel"; | |
| 4 | +// | |
| 5 | +// const topLevel = Convert.toTopLevel(json); | |
| 6 | +// | |
| 7 | +// These functions will throw an error if the JSON doesn't | |
| 8 | +// match the expected interface, even if the JSON is valid. | |
| 9 | + | |
| 10 | +// Converts JSON strings to/from your types | |
| 11 | +// and asserts the results of JSON.parse at runtime | |
| 12 | +export class Convert { | |
| 13 | + public static toTopLevel(json: string): { [key: string]: boolean } { | |
| 14 | + return cast(JSON.parse(json), m(true)); | |
| 15 | + } | |
| 16 | + | |
| 17 | + public static topLevelToJson(value: { [key: string]: boolean }): string { | |
| 18 | + return JSON.stringify(uncast(value, m(true)), null, 2); | |
| 19 | + } | |
| 20 | +} | |
| 21 | + | |
| 22 | +function invalidValue(typ: any, val: any, key: any, parent: any = ''): never { | |
| 23 | + const prettyTyp = prettyTypeName(typ); | |
| 24 | + const parentText = parent ? ` on ${parent}` : ''; | |
| 25 | + const keyText = key ? ` for key "${key}"` : ''; | |
| 26 | + throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`); | |
| 27 | +} | |
| 28 | + | |
| 29 | +function prettyTypeName(typ: any): string { | |
| 30 | + if (Array.isArray(typ)) { | |
| 31 | + if (typ.length === 2 && typ[0] === undefined) { | |
| 32 | + return `an optional ${prettyTypeName(typ[1])}`; | |
| 33 | + } else { | |
| 34 | + return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`; | |
| 35 | + } | |
| 36 | + } else if (typeof typ === "object" && typ.literal !== undefined) { | |
| 37 | + return typ.literal; | |
| 38 | + } else { | |
| 39 | + return typeof typ; | |
| 40 | + } | |
| 41 | +} | |
| 42 | + | |
| 43 | +function jsonToJSProps(typ: any): any { | |
| 44 | + if (typ.jsonToJS === undefined) { | |
| 45 | + const map: any = {}; | |
| 46 | + typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ }); | |
| 47 | + typ.jsonToJS = map; | |
| 48 | + } | |
| 49 | + return typ.jsonToJS; | |
| 50 | +} | |
| 51 | + | |
| 52 | +function jsToJSONProps(typ: any): any { | |
| 53 | + if (typ.jsToJSON === undefined) { | |
| 54 | + const map: any = {}; | |
| 55 | + typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ }); | |
| 56 | + typ.jsToJSON = map; | |
| 57 | + } | |
| 58 | + return typ.jsToJSON; | |
| 59 | +} | |
| 60 | + | |
| 61 | +function transform(val: any, typ: any, getProps: any, key: any = '', parent: any = ''): any { | |
| 62 | + function transformPrimitive(typ: string, val: any): any { | |
| 63 | + if (typeof typ === typeof val) return val; | |
| 64 | + return invalidValue(typ, val, key, parent); | |
| 65 | + } | |
| 66 | + | |
| 67 | + function transformUnion(typs: any[], val: any): any { | |
| 68 | + // val must validate against one typ in typs | |
| 69 | + const l = typs.length; | |
| 70 | + for (let i = 0; i < l; i++) { | |
| 71 | + const typ = typs[i]; | |
| 72 | + try { | |
| 73 | + return transform(val, typ, getProps); | |
| 74 | + } catch (_) {} | |
| 75 | + } | |
| 76 | + return invalidValue(typs, val, key, parent); | |
| 77 | + } | |
| 78 | + | |
| 79 | + function transformEnum(cases: string[], val: any): any { | |
| 80 | + if (cases.indexOf(val) !== -1) return val; | |
| 81 | + return invalidValue(cases.map(a => { return l(a); }), val, key, parent); | |
| 82 | + } | |
| 83 | + | |
| 84 | + function transformArray(typ: any, val: any): any { | |
| 85 | + // val must be an array with no invalid elements | |
| 86 | + if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent); | |
| 87 | + return val.map(el => transform(el, typ, getProps)); | |
| 88 | + } | |
| 89 | + | |
| 90 | + function transformDate(val: any): any { | |
| 91 | + if (val === null) { | |
| 92 | + return null; | |
| 93 | + } | |
| 94 | + const d = new Date(val); | |
| 95 | + if (isNaN(d.valueOf())) { | |
| 96 | + return invalidValue(l("Date"), val, key, parent); | |
| 97 | + } | |
| 98 | + return d; | |
| 99 | + } | |
| 100 | + | |
| 101 | + function transformObject(props: { [k: string]: any }, additional: any, val: any): any { | |
| 102 | + if (val === null || typeof val !== "object" || Array.isArray(val)) { | |
| 103 | + return invalidValue(l(ref || "object"), val, key, parent); | |
| 104 | + } | |
| 105 | + const result: any = {}; | |
| 106 | + Object.getOwnPropertyNames(props).forEach(key => { | |
| 107 | + const prop = props[key]; | |
| 108 | + const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined; | |
| 109 | + result[prop.key] = transform(v, prop.typ, getProps, key, ref); | |
| 110 | + }); | |
| 111 | + Object.getOwnPropertyNames(val).forEach(key => { | |
| 112 | + if (!Object.prototype.hasOwnProperty.call(props, key)) { | |
| 113 | + result[key] = transform(val[key], additional, getProps, key, ref); | |
| 114 | + } | |
| 115 | + }); | |
| 116 | + return result; | |
| 117 | + } | |
| 118 | + | |
| 119 | + if (typ === "any") return val; | |
| 120 | + if (typ === null) { | |
| 121 | + if (val === null) return val; | |
| 122 | + return invalidValue(typ, val, key, parent); | |
| 123 | + } | |
| 124 | + if (typ === false) return invalidValue(typ, val, key, parent); | |
| 125 | + let ref: any = undefined; | |
| 126 | + while (typeof typ === "object" && typ.ref !== undefined) { | |
| 127 | + ref = typ.ref; | |
| 128 | + typ = typeMap[typ.ref]; | |
| 129 | + } | |
| 130 | + if (Array.isArray(typ)) return transformEnum(typ, val); | |
| 131 | + if (typeof typ === "object") { | |
| 132 | + return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val) | |
| 133 | + : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val) | |
| 134 | + : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val) | |
| 135 | + : invalidValue(typ, val, key, parent); | |
| 136 | + } | |
| 137 | + // Numbers can be parsed by Date but shouldn't be. | |
| 138 | + if (typ === Date && typeof val !== "number") return transformDate(val); | |
| 139 | + return transformPrimitive(typ, val); | |
| 140 | +} | |
| 141 | + | |
| 142 | +function cast<T>(val: any, typ: any): T { | |
| 143 | + return transform(val, typ, jsonToJSProps); | |
| 144 | +} | |
| 145 | + | |
| 146 | +function uncast<T>(val: T, typ: any): any { | |
| 147 | + return transform(val, typ, jsToJSONProps); | |
| 148 | +} | |
| 149 | + | |
| 150 | +function l(typ: any) { | |
| 151 | + return { literal: typ }; | |
| 152 | +} | |
| 153 | + | |
| 154 | +function a(typ: any) { | |
| 155 | + return { arrayItems: typ }; | |
| 156 | +} | |
| 157 | + | |
| 158 | +function u(...typs: any[]) { | |
| 159 | + return { unionMembers: typs }; | |
| 160 | +} | |
| 161 | + | |
| 162 | +function o(props: any[], additional: any) { | |
| 163 | + return { props, additional }; | |
| 164 | +} | |
| 165 | + | |
| 166 | +function m(additional: any) { | |
| 167 | + const props: any[] = []; | |
| 168 | + return { props, additional }; | |
| 169 | +} | |
| 170 | + | |
| 171 | +function r(name: string) { | |
| 172 | + return { ref: name }; | |
| 173 | +} | |
| 174 | + | |
| 175 | +const typeMap: any = { | |
| 176 | +}; |
No generated files match these filters.