The data to validate as a grant file
True if the data is a valid grant file, false otherwise
Performs runtime validation to ensure data conforms to the GrantFile interface. Checks for required fields (grantee, operation, parameters) and validates their types and formats. This is a type guard function that enables TypeScript to narrow the type when it returns true.
const unknownData = await fetch(url).then(r => r.json());
if (validateGrantFile(unknownData)) {
// TypeScript now knows unknownData is a GrantFile
console.log(`Grant for operation: ${unknownData.operation}`);
console.log(`Grantee: ${unknownData.grantee}`);
} else {
throw new Error('Invalid grant file format');
}
// Validation examples:
validateGrantFile({
grantee: '0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36',
operation: 'read',
parameters: {}
}); // true
validateGrantFile({
grantee: 'invalid-address',
operation: 'read',
parameters: {}
}); // false (invalid address format)
validateGrantFile({
operation: 'read',
parameters: {}
}); // false (missing grantee)
Validates that a grant file has the required structure.