Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | 3x 134x 126x 126x 122x 122x 122x 508x 325x 183x 36x 147x 147x 1x 1x 1x 1x 1x 1x 1x 1x 48x 21x 29x 25x 14x 2x 325x 325x 36x 35x 35x 84x 84x 84x 83x 34x 1x 1x 1x 1x 1x 1x 1x 1x 48x 47x 47x 47x 47x 47x 122x 122x 121x 46x 122x 21x 21x 21x 21x 21x 20x 20x 58x 57x 19x 18x 29x 25x 14x 14x 13x 13x 13x 13x 242x 166x 76x 32x 60x 60x 20x 8x 14x 11x 7x 166x 30x 7x 52x 9x 10x 5x 53x 20x 20x 20x 47x 47x 45x 16x 8x 14x 28x 11x 11x 16x 7x 84x 4x 4x 81x | import type {
AvroSchema,
AvroNullSchema,
AvroBooleanSchema,
AvroIntSchema,
AvroLongSchema,
AvroFloatSchema,
AvroDoubleSchema,
AvroBytesSchema,
AvroStringSchema,
AvroRecordSchema,
AvroEnumSchema,
AvroArraySchema,
AvroMapSchema,
AvroUnionSchema,
AvroFixedSchema,
AvroRecordField,
AvroNamedSchema,
} from './types';
/**
* Validates Apache Avro schemas according to the specification.
* Based on https://avro.apache.org/docs/1.12.0/specification/
*/
export class AvroSchemaValidator {
private namedSchemas = new Map<string, AvroNamedSchema>();
/**
* Validates an Avro schema and resolves named schema references.
*/
public validateSchema(schema: AvroSchema): boolean {
this.namedSchemas.clear();
return this.validateSchemaInternal(schema);
}
/**
* Validates that a value conforms to the given Avro schema.
*/
public validateValue(value: unknown, schema: AvroSchema): boolean {
this.namedSchemas.clear();
this.validateSchemaInternal(schema);
return this.validateValueAgainstSchema(value, schema);
}
private validateSchemaInternal(schema: AvroSchema): boolean {
if (typeof schema === 'string') {
// String schema references (either primitive type or named type)
return this.validateStringSchema(schema);
}
if (Array.isArray(schema)) {
// Union schema
return this.validateUnionSchema(schema);
}
if (typeof schema === 'object' && schema !== null) {
switch (schema.type) {
case 'null':
return this.validateNullSchema(schema as AvroNullSchema);
case 'boolean':
return this.validateBooleanSchema(schema as AvroBooleanSchema);
case 'int':
return this.validateIntSchema(schema as AvroIntSchema);
case 'long':
return this.validateLongSchema(schema as AvroLongSchema);
case 'float':
return this.validateFloatSchema(schema as AvroFloatSchema);
case 'double':
return this.validateDoubleSchema(schema as AvroDoubleSchema);
case 'bytes':
return this.validateBytesSchema(schema as AvroBytesSchema);
case 'string':
return this.validateStringTypeSchema(schema as AvroStringSchema);
case 'record':
return this.validateRecordSchema(schema as AvroRecordSchema);
case 'enum':
return this.validateEnumSchema(schema as AvroEnumSchema);
case 'array':
return this.validateArraySchema(schema as AvroArraySchema);
case 'map':
return this.validateMapSchema(schema as AvroMapSchema);
case 'fixed':
return this.validateFixedSchema(schema as AvroFixedSchema);
default:
return false;
}
}
return false;
}
private validateStringSchema(schema: string): boolean {
const primitiveTypes = ['null', 'boolean', 'int', 'long', 'float', 'double', 'bytes', 'string'];
return primitiveTypes.includes(schema) || this.namedSchemas.has(schema);
}
private validateUnionSchema(schema: AvroUnionSchema): boolean {
if (schema.length === 0) return false;
const typeSet = new Set<string>();
for (const subSchema of schema) {
Iif (!this.validateSchemaInternal(subSchema)) return false;
// Union types must be unique
const typeName = this.getSchemaTypeName(subSchema);
if (typeSet.has(typeName)) return false;
typeSet.add(typeName);
}
return true;
}
private validateNullSchema(schema: AvroNullSchema): boolean {
return schema.type === 'null';
}
private validateBooleanSchema(schema: AvroBooleanSchema): boolean {
return schema.type === 'boolean';
}
private validateIntSchema(schema: AvroIntSchema): boolean {
return schema.type === 'int';
}
private validateLongSchema(schema: AvroLongSchema): boolean {
return schema.type === 'long';
}
private validateFloatSchema(schema: AvroFloatSchema): boolean {
return schema.type === 'float';
}
private validateDoubleSchema(schema: AvroDoubleSchema): boolean {
return schema.type === 'double';
}
private validateBytesSchema(schema: AvroBytesSchema): boolean {
return schema.type === 'bytes';
}
private validateStringTypeSchema(schema: AvroStringSchema): boolean {
return schema.type === 'string';
}
private validateRecordSchema(schema: AvroRecordSchema): boolean {
if (schema.type !== 'record' || !schema.name || !Array.isArray(schema.fields)) return false;
const fullName = this.getFullName(schema.name, schema.namespace);
Iif (this.namedSchemas.has(fullName)) return false;
this.namedSchemas.set(fullName, schema);
const fieldNames = new Set<string>();
for (const field of schema.fields) {
Iif (!this.validateRecordField(field)) return false;
if (fieldNames.has(field.name)) return false;
fieldNames.add(field.name);
}
return true;
}
private validateRecordField(field: AvroRecordField): boolean {
return typeof field.name === 'string' && field.name.length > 0 && this.validateSchemaInternal(field.type);
}
private validateEnumSchema(schema: AvroEnumSchema): boolean {
Iif (schema.type !== 'enum' || !schema.name || !Array.isArray(schema.symbols)) return false;
const fullName = this.getFullName(schema.name, schema.namespace);
Iif (this.namedSchemas.has(fullName)) return false;
this.namedSchemas.set(fullName, schema);
if (schema.symbols.length === 0) return false;
const symbolSet = new Set<string>();
for (const symbol of schema.symbols) {
if (typeof symbol !== 'string' || symbolSet.has(symbol)) return false;
symbolSet.add(symbol);
}
// Default symbol must be in symbols array if provided
if (schema.default !== undefined && !schema.symbols.includes(schema.default)) return false;
return true;
}
private validateArraySchema(schema: AvroArraySchema): boolean {
return schema.type === 'array' && this.validateSchemaInternal(schema.items);
}
private validateMapSchema(schema: AvroMapSchema): boolean {
return schema.type === 'map' && this.validateSchemaInternal(schema.values);
}
private validateFixedSchema(schema: AvroFixedSchema): boolean {
Iif (schema.type !== 'fixed' || !schema.name || typeof schema.size !== 'number') return false;
if (schema.size < 0) return false;
const fullName = this.getFullName(schema.name, schema.namespace);
Iif (this.namedSchemas.has(fullName)) return false;
this.namedSchemas.set(fullName, schema);
return true;
}
private validateValueAgainstSchema(value: unknown, schema: AvroSchema): boolean {
if (typeof schema === 'string') {
return this.validateValueAgainstStringSchema(value, schema);
}
if (Array.isArray(schema)) {
// Union - value must match one of the schemas
return schema.some((subSchema) => this.validateValueAgainstSchema(value, subSchema));
}
if (typeof schema === 'object' && schema !== null) {
switch (schema.type) {
case 'null':
return value === null;
case 'boolean':
return typeof value === 'boolean';
case 'int':
return typeof value === 'number' && Number.isInteger(value) && value >= -2147483648 && value <= 2147483647;
case 'long':
return (typeof value === 'number' && Number.isInteger(value)) || typeof value === 'bigint';
case 'float':
case 'double':
return typeof value === 'number';
case 'bytes':
return value instanceof Uint8Array;
case 'string':
return typeof value === 'string';
case 'record':
return this.validateValueAgainstRecord(value, schema as AvroRecordSchema);
case 'enum':
return this.validateValueAgainstEnum(value, schema as AvroEnumSchema);
case 'array':
return this.validateValueAgainstArray(value, schema as AvroArraySchema);
case 'map':
return this.validateValueAgainstMap(value, schema as AvroMapSchema);
case 'fixed':
return this.validateValueAgainstFixed(value, schema as AvroFixedSchema);
default:
return false;
}
}
return false;
}
private validateValueAgainstStringSchema(value: unknown, schema: string): boolean {
switch (schema) {
case 'null':
return value === null;
case 'boolean':
return typeof value === 'boolean';
case 'int':
return typeof value === 'number' && Number.isInteger(value) && value >= -2147483648 && value <= 2147483647;
case 'long':
return (typeof value === 'number' && Number.isInteger(value)) || typeof value === 'bigint';
case 'float':
case 'double':
return typeof value === 'number';
case 'bytes':
return value instanceof Uint8Array;
case 'string':
return typeof value === 'string';
default: {
// Named schema reference
const namedSchema = this.namedSchemas.get(schema);
return namedSchema ? this.validateValueAgainstSchema(value, namedSchema) : false;
}
}
}
private validateValueAgainstRecord(value: unknown, schema: AvroRecordSchema): boolean {
Iif (typeof value !== 'object' || value === null) return false;
const obj = value as Record<string, unknown>;
for (const field of schema.fields) {
const fieldValue = obj[field.name];
if (fieldValue === undefined && field.default === undefined) return false;
if (fieldValue !== undefined && !this.validateValueAgainstSchema(fieldValue, field.type)) return false;
}
return true;
}
private validateValueAgainstEnum(value: unknown, schema: AvroEnumSchema): boolean {
return typeof value === 'string' && schema.symbols.includes(value);
}
private validateValueAgainstArray(value: unknown, schema: AvroArraySchema): boolean {
Iif (!Array.isArray(value)) return false;
return value.every((item) => this.validateValueAgainstSchema(item, schema.items));
}
private validateValueAgainstMap(value: unknown, schema: AvroMapSchema): boolean {
Iif (typeof value !== 'object' || value === null) return false;
const obj = value as Record<string, unknown>;
return Object.values(obj).every((val) => this.validateValueAgainstSchema(val, schema.values));
}
private validateValueAgainstFixed(value: unknown, schema: AvroFixedSchema): boolean {
return value instanceof Uint8Array && value.length === schema.size;
}
private getSchemaTypeName(schema: AvroSchema): string {
if (typeof schema === 'string') return schema;
Iif (Array.isArray(schema)) return 'union';
return schema.type;
}
private getFullName(name: string, namespace?: string): string {
return namespace ? `${namespace}.${name}` : name;
}
}
|