-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#609] feat: add metadata validation against cip fields
- Loading branch information
1 parent
e9cdbea
commit 8e11410
Showing
20 changed files
with
205 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
const { pathsToModuleNameMapper } = require('ts-jest'); | ||
const { compilerOptions } = require('./tsconfig'); | ||
|
||
module.exports = { | ||
preset: 'ts-jest', | ||
moduleFileExtensions: ['js', 'json', 'ts'], | ||
testRegex: '.*\\.(spec|test)\\.ts$', | ||
transform: { | ||
'^.+\\.(t|j)s$': 'ts-jest', | ||
}, | ||
collectCoverageFrom: ['**/*.(t|j)s'], | ||
coverageDirectory: '../coverage', | ||
testEnvironment: 'node', | ||
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths), | ||
modulePaths: ['<rootDir>'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './validateMetadata.dto'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
import { IsUrl, IsNotEmpty } from 'class-validator'; | ||
import { IsUrl, IsNotEmpty, IsEnum, IsOptional } from 'class-validator'; | ||
|
||
import { MetadataStandard } from '@types'; | ||
|
||
export class ValidateMetadataDTO { | ||
@IsNotEmpty() | ||
hash: string; | ||
|
||
@IsUrl() | ||
url: string; | ||
|
||
@IsOptional() | ||
@IsEnum(MetadataStandard) | ||
standard?: MetadataStandard; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ValidationError'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
govtool/metadata-validation/src/schemas/cipStandardSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import * as Joi from 'joi'; | ||
|
||
import { MetadataStandard } from '@types'; | ||
|
||
type StandardSpecification = Record<MetadataStandard, Joi.ObjectSchema<any>>; | ||
|
||
const CIP100_URL = | ||
'https://github.com/cardano-foundation/CIPs/blob/master/CIP-0100/README.md#'; | ||
const CIP108_URL = | ||
'https://github.com/cardano-foundation/CIPs/blob/master/CIP-0108/README.md#'; | ||
|
||
export const cipStandardSchema: StandardSpecification = { | ||
// Source of CIP-108: https://github.com/Ryun1/CIPs/blob/governance-metadata-actions/CIP-0108/README.md | ||
[MetadataStandard.CIP108]: Joi.object({ | ||
'@context': Joi.object({ | ||
'@language': Joi.string().required(), | ||
CIP100: Joi.string().valid(CIP100_URL).required(), | ||
CIP108: Joi.string().valid(CIP108_URL).required(), | ||
hashAlgorithm: Joi.string().valid('CIP100:hashAlgorithm').required(), | ||
body: Joi.object(), | ||
authors: Joi.object(), | ||
}), | ||
authors: Joi.array(), | ||
hashAlgorithm: Joi.object({ | ||
'@value': Joi.string().valid('blake2b-256').required(), | ||
}), | ||
body: Joi.object({ | ||
title: Joi.object({ '@value': Joi.string().max(80).required() }), | ||
abstract: Joi.object({ '@value': Joi.string().max(2500).required() }), | ||
motivation: Joi.object({ '@value': Joi.string().required() }), | ||
rationale: Joi.object({ '@value': Joi.string().required() }), | ||
references: Joi.array().items( | ||
Joi.object({ | ||
'@type': Joi.string(), | ||
label: Joi.object({ '@value': Joi.string().required() }), | ||
uri: Joi.object({ '@value': Joi.string().uri().required() }), | ||
referenceHash: Joi.object({ | ||
hashDigest: Joi.string().required(), | ||
hashAlgorithm: Joi.string().required(), | ||
}), | ||
}).required(), | ||
), | ||
}), | ||
}), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './cipStandardSchema'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './validateMetadata'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './canonizeJSON'; | ||
export * from './validateMetadataStandard'; |
19 changes: 19 additions & 0 deletions
19
govtool/metadata-validation/src/utils/validateMetadataStandard.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { MetadataStandard } from '@types'; | ||
|
||
import { validateMetadataStandard } from './validateMetadataStandard'; | ||
|
||
describe('validateMetadataStandard', () => { | ||
it('should throw MetadataValidationStatus.INCORRECT_FORMAT if validation fails', async () => { | ||
try { | ||
await validateMetadataStandard( | ||
{ | ||
testValue: 'test', | ||
anotherValue: 'another', | ||
}, | ||
MetadataStandard.CIP108, | ||
); | ||
} catch (error) { | ||
expect(error).toBe('INCORRECT_FORMAT'); | ||
} | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
govtool/metadata-validation/src/utils/validateMetadataStandard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { MetadataValidationStatus } from '@enums'; | ||
import { cipStandardSchema } from '@schemas'; | ||
import { MetadataStandard } from '@types'; | ||
|
||
/** | ||
* Validates the metadata against a specific standard. | ||
* @param data - The metadata to be validated. | ||
* @param standard - The metadata standard to validate against. | ||
* @throws {MetadataValidationStatus.INCORRECT_FORMAT} - If the metadata does not conform to the specified standard. | ||
*/ | ||
export const validateMetadataStandard = async ( | ||
data: Record<string, unknown>, | ||
standard: MetadataStandard, | ||
) => { | ||
try { | ||
await cipStandardSchema[standard]?.validateAsync(data); | ||
} catch (error) { | ||
throw MetadataValidationStatus.INCORRECT_FORMAT; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters