|
| 1 | +/* tslint:disable */ |
| 2 | +// generated by typescript-json-validator |
| 3 | +import Ajv = require('ajv'); |
| 4 | +import { |
| 5 | + EntityTypes, |
| 6 | + EntityOne, |
| 7 | + EntityTwo, |
| 8 | + Entity, |
| 9 | + Value, |
| 10 | +} from './DisjointUnionExample'; |
| 11 | +export const ajv = new Ajv({ |
| 12 | + allErrors: true, |
| 13 | + coerceTypes: false, |
| 14 | + format: 'fast', |
| 15 | + nullable: true, |
| 16 | + unicode: true, |
| 17 | + uniqueItems: true, |
| 18 | + useDefaults: true, |
| 19 | +}); |
| 20 | + |
| 21 | +ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json')); |
| 22 | + |
| 23 | +export {EntityTypes, EntityOne, EntityTwo, Entity, Value}; |
| 24 | +export const Schema = { |
| 25 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 26 | + definitions: { |
| 27 | + Entity: { |
| 28 | + else: { |
| 29 | + else: { |
| 30 | + else: { |
| 31 | + properties: { |
| 32 | + type: { |
| 33 | + enum: ['TypeOne', 'TypeTwo', 'TypeThree'], |
| 34 | + type: 'string', |
| 35 | + }, |
| 36 | + }, |
| 37 | + required: ['type'], |
| 38 | + }, |
| 39 | + if: { |
| 40 | + properties: { |
| 41 | + type: { |
| 42 | + enum: ['TypeThree'], |
| 43 | + type: 'string', |
| 44 | + }, |
| 45 | + }, |
| 46 | + required: ['type'], |
| 47 | + }, |
| 48 | + then: { |
| 49 | + defaultProperties: [], |
| 50 | + properties: { |
| 51 | + baz: { |
| 52 | + type: 'number', |
| 53 | + }, |
| 54 | + type: { |
| 55 | + enum: ['TypeThree'], |
| 56 | + type: 'string', |
| 57 | + }, |
| 58 | + }, |
| 59 | + required: ['baz', 'type'], |
| 60 | + type: 'object', |
| 61 | + }, |
| 62 | + }, |
| 63 | + if: { |
| 64 | + properties: { |
| 65 | + type: { |
| 66 | + enum: ['TypeTwo'], |
| 67 | + type: 'string', |
| 68 | + }, |
| 69 | + }, |
| 70 | + required: ['type'], |
| 71 | + }, |
| 72 | + then: { |
| 73 | + $ref: '#/definitions/EntityTwo', |
| 74 | + }, |
| 75 | + }, |
| 76 | + if: { |
| 77 | + properties: { |
| 78 | + type: { |
| 79 | + enum: ['TypeOne'], |
| 80 | + type: 'string', |
| 81 | + }, |
| 82 | + }, |
| 83 | + required: ['type'], |
| 84 | + }, |
| 85 | + then: { |
| 86 | + $ref: '#/definitions/EntityOne', |
| 87 | + }, |
| 88 | + }, |
| 89 | + EntityOne: { |
| 90 | + defaultProperties: [], |
| 91 | + properties: { |
| 92 | + foo: { |
| 93 | + type: 'string', |
| 94 | + }, |
| 95 | + type: { |
| 96 | + enum: ['TypeOne'], |
| 97 | + type: 'string', |
| 98 | + }, |
| 99 | + }, |
| 100 | + required: ['foo', 'type'], |
| 101 | + type: 'object', |
| 102 | + }, |
| 103 | + EntityTwo: { |
| 104 | + defaultProperties: [], |
| 105 | + properties: { |
| 106 | + bar: { |
| 107 | + type: 'string', |
| 108 | + }, |
| 109 | + type: { |
| 110 | + enum: ['TypeTwo'], |
| 111 | + type: 'string', |
| 112 | + }, |
| 113 | + }, |
| 114 | + required: ['bar', 'type'], |
| 115 | + type: 'object', |
| 116 | + }, |
| 117 | + EntityTypes: { |
| 118 | + enum: ['TypeOne', 'TypeThree', 'TypeTwo'], |
| 119 | + type: 'string', |
| 120 | + }, |
| 121 | + Value: { |
| 122 | + else: { |
| 123 | + else: { |
| 124 | + else: { |
| 125 | + properties: { |
| 126 | + number: { |
| 127 | + enum: [0, 1, 2], |
| 128 | + type: 'number', |
| 129 | + }, |
| 130 | + }, |
| 131 | + required: ['number'], |
| 132 | + }, |
| 133 | + if: { |
| 134 | + properties: { |
| 135 | + number: { |
| 136 | + enum: [2], |
| 137 | + type: 'number', |
| 138 | + }, |
| 139 | + }, |
| 140 | + required: ['number'], |
| 141 | + }, |
| 142 | + then: { |
| 143 | + defaultProperties: [], |
| 144 | + properties: { |
| 145 | + baz: { |
| 146 | + type: 'string', |
| 147 | + }, |
| 148 | + number: { |
| 149 | + enum: [2], |
| 150 | + type: 'number', |
| 151 | + }, |
| 152 | + }, |
| 153 | + required: ['baz', 'number'], |
| 154 | + type: 'object', |
| 155 | + }, |
| 156 | + }, |
| 157 | + if: { |
| 158 | + properties: { |
| 159 | + number: { |
| 160 | + enum: [1], |
| 161 | + type: 'number', |
| 162 | + }, |
| 163 | + }, |
| 164 | + required: ['number'], |
| 165 | + }, |
| 166 | + then: { |
| 167 | + defaultProperties: [], |
| 168 | + properties: { |
| 169 | + bar: { |
| 170 | + type: 'string', |
| 171 | + }, |
| 172 | + number: { |
| 173 | + enum: [1], |
| 174 | + type: 'number', |
| 175 | + }, |
| 176 | + }, |
| 177 | + required: ['bar', 'number'], |
| 178 | + type: 'object', |
| 179 | + }, |
| 180 | + }, |
| 181 | + if: { |
| 182 | + properties: { |
| 183 | + number: { |
| 184 | + enum: [0], |
| 185 | + type: 'number', |
| 186 | + }, |
| 187 | + }, |
| 188 | + required: ['number'], |
| 189 | + }, |
| 190 | + then: { |
| 191 | + defaultProperties: [], |
| 192 | + properties: { |
| 193 | + foo: { |
| 194 | + type: 'string', |
| 195 | + }, |
| 196 | + number: { |
| 197 | + enum: [0], |
| 198 | + type: 'number', |
| 199 | + }, |
| 200 | + }, |
| 201 | + required: ['foo', 'number'], |
| 202 | + type: 'object', |
| 203 | + }, |
| 204 | + }, |
| 205 | + }, |
| 206 | +}; |
| 207 | +ajv.addSchema(Schema, 'Schema'); |
| 208 | +export function validate( |
| 209 | + typeName: 'EntityTypes', |
| 210 | +): (value: unknown) => EntityTypes; |
| 211 | +export function validate(typeName: 'EntityOne'): (value: unknown) => EntityOne; |
| 212 | +export function validate(typeName: 'EntityTwo'): (value: unknown) => EntityTwo; |
| 213 | +export function validate(typeName: 'Entity'): (value: unknown) => Entity; |
| 214 | +export function validate(typeName: 'Value'): (value: unknown) => Value; |
| 215 | +export function validate(typeName: string): (value: unknown) => any { |
| 216 | + const validator: any = ajv.getSchema(`Schema#/definitions/${typeName}`); |
| 217 | + return (value: unknown): any => { |
| 218 | + if (!validator) { |
| 219 | + throw new Error( |
| 220 | + `No validator defined for Schema#/definitions/${typeName}`, |
| 221 | + ); |
| 222 | + } |
| 223 | + |
| 224 | + const valid = validator(value); |
| 225 | + |
| 226 | + if (!valid) { |
| 227 | + throw new Error( |
| 228 | + 'Invalid ' + |
| 229 | + typeName + |
| 230 | + ': ' + |
| 231 | + ajv.errorsText( |
| 232 | + validator.errors!.filter((e: any) => e.keyword !== 'if'), |
| 233 | + {dataVar: typeName}, |
| 234 | + ), |
| 235 | + ); |
| 236 | + } |
| 237 | + |
| 238 | + return value as any; |
| 239 | + }; |
| 240 | +} |
0 commit comments