@graphprotocol/graph-cli
Version:
CLI for building for and deploying to The Graph
42 lines (41 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateContractValues = exports.validateContract = void 0;
const validateContract = (value, ProtocolContract) => {
const contract = new ProtocolContract(value);
const { valid, error } = contract.validate();
if (!valid) {
return {
valid,
error: `Contract ${ProtocolContract.identifierName()} is invalid: ${value}\n${error}`,
};
}
return { valid, error };
};
exports.validateContract = validateContract;
const validateContractValues = (manifest, protocol) => {
const ProtocolContract = protocol.getContract();
const fieldName = ProtocolContract.identifierName();
return manifest.dataSources
.filter(datasource => protocol.isValidKindName(datasource.kind))
.reduce((errors, dataSource, dataSourceIndex) => {
const path = ['dataSources', dataSourceIndex, 'source', fieldName];
// No need to validate if the source has no contract field
// @ts-expect-error TODO: we need to rework the classes to make this `fieldName` not be a string
if (!dataSource.source[fieldName]) {
return errors;
}
// @ts-expect-error TODO: we need to rework the classes to make this `fieldName` not be a string
const contractValue = dataSource.source[fieldName];
const { valid, error } = (0, exports.validateContract)(contractValue, ProtocolContract);
// Validate whether the contract is valid for the protocol
if (valid) {
return errors;
}
return errors.push({
path,
message: error,
});
}, []);
};
exports.validateContractValues = validateContractValues;