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 318 319 320 321 | 1x 47x 47x 68x 68x 70x 4x 66x 22x 4x 3x 3x 10x 5x 2x 8x 7x 2x 4x 1x 3x 3x 3x 1x 4x 3x 3x 10x 5x 2x 3x 2x 2x 1x 2x 8x 1x 7x 7x 10x 1x 9x 9x 1x 8x 1x 7x 7x 4x 7x 1x 6x 6x 11x 1x 10x 10x 1x 9x 9x 9x 4x 1x 3x 96x 3x 29x 5x 4x 5x 6x 5x 4x 4x 3x 3x 3x 3x 10x 10x 4x 4x 5x 4x 4x 8x 5x 5x 2x 3x 3x 5x 6x 8x 6x 6x | import type {
XdrSchema,
XdrEnumSchema,
XdrOpaqueSchema,
XdrVarlenOpaqueSchema,
XdrStringSchema,
XdrArraySchema,
XdrVarlenArraySchema,
XdrStructSchema,
XdrUnionSchema,
XdrOptionalSchema,
XdrConstantSchema,
} from './types';
/**
* XDR schema validator for validating XDR schemas and values according to RFC 4506.
*/
export class XdrSchemaValidator {
/**
* Validates an XDR schema structure.
*/
public validateSchema(schema: XdrSchema): boolean {
try {
return this.validateSchemaInternal(schema);
} catch {
return false;
}
}
/**
* Validates if a value conforms to the given XDR schema.
*/
public validateValue(value: unknown, schema: XdrSchema): boolean {
try {
return this.validateValueInternal(value, schema);
} catch {
return false;
}
}
private validateSchemaInternal(schema: XdrSchema): boolean {
if (!schema || typeof schema !== 'object' || !schema.type) {
return false;
}
switch (schema.type) {
// Primitive types
case 'void':
case 'int':
case 'unsigned_int':
case 'boolean':
case 'hyper':
case 'unsigned_hyper':
case 'float':
case 'double':
case 'quadruple':
return true;
case 'enum':
return this.validateEnumSchema(schema as XdrEnumSchema);
// Wide primitive types
case 'opaque':
return this.validateOpaqueSchema(schema as XdrOpaqueSchema);
case 'vopaque':
return this.validateVarlenOpaqueSchema(schema as XdrVarlenOpaqueSchema);
case 'string':
return this.validateStringSchema(schema as XdrStringSchema);
// Composite types
case 'array':
return this.validateArraySchema(schema as XdrArraySchema);
case 'varray':
return this.validateVarlenArraySchema(schema as XdrVarlenArraySchema);
case 'struct':
return this.validateStructSchema(schema as XdrStructSchema);
case 'union':
return this.validateUnionSchema(schema as XdrUnionSchema);
case 'optional':
return this.validateOptionalSchema(schema as XdrOptionalSchema);
case 'const':
return this.validateConstantSchema(schema as XdrConstantSchema);
default:
return false;
}
}
private validateEnumSchema(schema: XdrEnumSchema): boolean {
if (!schema.values || typeof schema.values !== 'object') {
return false;
}
const values = Object.values(schema.values);
const uniqueValues = new Set(values);
// Check for duplicate values
if (values.length !== uniqueValues.size) {
return false;
}
// Check that all values are integers
return values.every((value) => Number.isInteger(value));
}
private validateOpaqueSchema(schema: XdrOpaqueSchema): boolean {
return typeof schema.size === 'number' && Number.isInteger(schema.size) && schema.size >= 0;
}
private validateVarlenOpaqueSchema(schema: XdrVarlenOpaqueSchema): boolean {
return !schema.size || (typeof schema.size === 'number' && Number.isInteger(schema.size) && schema.size >= 0);
}
private validateStringSchema(schema: XdrStringSchema): boolean {
return !schema.size || (typeof schema.size === 'number' && Number.isInteger(schema.size) && schema.size >= 0);
}
private validateArraySchema(schema: XdrArraySchema): boolean {
if (!schema.elements || typeof schema.size !== 'number' || !Number.isInteger(schema.size) || schema.size < 0) {
return false;
}
return this.validateSchemaInternal(schema.elements);
}
private validateVarlenArraySchema(schema: XdrVarlenArraySchema): boolean {
Iif (!schema.elements) {
return false;
}
if (schema.size !== undefined) {
Iif (typeof schema.size !== 'number' || !Number.isInteger(schema.size) || schema.size < 0) {
return false;
}
}
return this.validateSchemaInternal(schema.elements);
}
private validateStructSchema(schema: XdrStructSchema): boolean {
if (!Array.isArray(schema.fields)) {
return false;
}
const fieldNames = new Set<string>();
for (const field of schema.fields) {
if (!Array.isArray(field) || field.length !== 2) {
return false;
}
const [fieldSchema, fieldName] = field;
if (typeof fieldName !== 'string' || fieldName === '') {
return false;
}
if (fieldNames.has(fieldName)) {
return false; // Duplicate field name
}
fieldNames.add(fieldName);
Iif (!this.validateSchemaInternal(fieldSchema)) {
return false;
}
}
return true;
}
private validateUnionSchema(schema: XdrUnionSchema): boolean {
if (!Array.isArray(schema.arms) || schema.arms.length === 0) {
return false;
}
const discriminants = new Set();
for (const arm of schema.arms) {
if (!Array.isArray(arm) || arm.length !== 2) {
return false;
}
const [discriminant, armSchema] = arm;
// Check for duplicate discriminants
if (discriminants.has(discriminant)) {
return false;
}
discriminants.add(discriminant);
// Validate discriminant type
Iif (typeof discriminant !== 'number' && typeof discriminant !== 'string' && typeof discriminant !== 'boolean') {
return false;
}
Iif (!this.validateSchemaInternal(armSchema)) {
return false;
}
}
// Validate default schema if present
if (schema.default && !this.validateSchemaInternal(schema.default)) {
return false;
}
return true;
}
private validateOptionalSchema(schema: XdrOptionalSchema): boolean {
Iif (!schema.element) {
return false;
}
return this.validateSchemaInternal(schema.element);
}
private validateConstantSchema(schema: XdrConstantSchema): boolean {
Iif (typeof schema.value !== 'number' || !Number.isInteger(schema.value)) {
return false;
}
return true;
}
private validateValueInternal(value: unknown, schema: XdrSchema): boolean {
switch (schema.type) {
case 'void':
return value === null || value === undefined;
case 'int':
return typeof value === 'number' && Number.isInteger(value) && value >= -2147483648 && value <= 2147483647;
case 'unsigned_int':
return typeof value === 'number' && Number.isInteger(value) && value >= 0 && value <= 4294967295;
case 'boolean':
return typeof value === 'boolean';
case 'hyper':
return (typeof value === 'number' && Number.isInteger(value)) || typeof value === 'bigint';
case 'unsigned_hyper':
return (
(typeof value === 'number' && Number.isInteger(value) && value >= 0) ||
(typeof value === 'bigint' && value >= BigInt(0))
);
case 'float':
case 'double':
case 'quadruple':
return typeof value === 'number';
case 'enum': {
const enumSchema = schema as XdrEnumSchema;
return typeof value === 'string' && value in enumSchema.values;
}
case 'opaque': {
const opaqueSchema = schema as XdrOpaqueSchema;
return value instanceof Uint8Array && value.length === opaqueSchema.size;
}
case 'vopaque': {
const vopaqueSchema = schema as XdrVarlenOpaqueSchema;
return value instanceof Uint8Array && (!vopaqueSchema.size || value.length <= vopaqueSchema.size);
}
case 'string': {
const stringSchema = schema as XdrStringSchema;
return typeof value === 'string' && (!stringSchema.size || value.length <= stringSchema.size);
}
case 'array': {
const arraySchema = schema as XdrArraySchema;
return (
Array.isArray(value) &&
value.length === arraySchema.size &&
value.every((item) => this.validateValueInternal(item, arraySchema.elements))
);
}
case 'varray': {
const varraySchema = schema as XdrVarlenArraySchema;
return (
Array.isArray(value) &&
(!varraySchema.size || value.length <= varraySchema.size) &&
value.every((item) => this.validateValueInternal(item, varraySchema.elements))
);
}
case 'struct': {
const structSchema = schema as XdrStructSchema;
if (!value || typeof value !== 'object' || Array.isArray(value)) {
return false;
}
const valueObj = value as Record<string, unknown>;
return structSchema.fields.every(
([fieldSchema, fieldName]) =>
fieldName in valueObj && this.validateValueInternal(valueObj[fieldName], fieldSchema),
);
}
case 'union': {
const unionSchema = schema as XdrUnionSchema;
// For union validation, we need additional context about which arm is selected
// This is a simplified validation - in practice, the discriminant would be known
const matchesArm = unionSchema.arms.some(([, armSchema]) => this.validateValueInternal(value, armSchema));
const matchesDefault = unionSchema.default ? this.validateValueInternal(value, unionSchema.default) : false;
return matchesArm || matchesDefault;
}
case 'optional': {
const optionalSchema = schema as XdrOptionalSchema;
// Optional values can be null/undefined or match the element schema
return value === null || value === undefined || this.validateValueInternal(value, optionalSchema.element);
}
case 'const': {
// Constants have no runtime value validation
return true;
}
default:
return false;
}
}
}
|