|
@@ -1,42 +1,84 @@
|
|
|
// TODO: Add entity batches validation
|
|
|
import Ajv from 'ajv'
|
|
|
-import CreateClassSchema from '../schemas/extrinsics/CreateClass.schema.json'
|
|
|
-import AddClassSchemaSchema from '../schemas/extrinsics/AddClassSchema.schema.json'
|
|
|
-import { getInputs, InputType } from './helpers/inputs'
|
|
|
-
|
|
|
-type JsonSchema = {
|
|
|
- schemaName: string
|
|
|
- jsonSchema: Record<string, unknown>
|
|
|
- relatedInputType: InputType
|
|
|
-}
|
|
|
+import { FetchedInput, getInputs, InputType, INPUT_TYPES } from './helpers/inputs'
|
|
|
+import path from 'path'
|
|
|
+import fs from 'fs'
|
|
|
|
|
|
-const schemas: JsonSchema[] = [
|
|
|
- { schemaName: 'CreateClass', jsonSchema: CreateClassSchema, relatedInputType: 'classes' },
|
|
|
- { schemaName: 'AddClassSchema', jsonSchema: AddClassSchemaSchema, relatedInputType: 'schemas' },
|
|
|
-]
|
|
|
+const SCHEMAS_LOCATION = path.join(__dirname, '../schemas')
|
|
|
|
|
|
const ajv = new Ajv({ allErrors: true })
|
|
|
+// Add all existing schemas to Ajv to allow them to reference each other
|
|
|
+fs.readdirSync(SCHEMAS_LOCATION)
|
|
|
+ .filter((subdir) => !subdir.includes('.'))
|
|
|
+ .forEach((subdir) => {
|
|
|
+ fs.readdirSync(path.join(SCHEMAS_LOCATION, subdir)).forEach((schemaFilename) => {
|
|
|
+ ajv.addSchema(JSON.parse(fs.readFileSync(path.join(SCHEMAS_LOCATION, subdir, schemaFilename)).toString()))
|
|
|
+ })
|
|
|
+ })
|
|
|
|
|
|
-schemas.forEach(({ schemaName, jsonSchema, relatedInputType }) => {
|
|
|
- // Validate the schema itself
|
|
|
- console.log(`Validating schema for ${schemaName}...`)
|
|
|
+const validateJsonSchema = (jsonSchemaShortPath: string, jsonSchema: Record<string, unknown>) => {
|
|
|
if (!ajv.validateSchema(jsonSchema)) {
|
|
|
- console.log(`\nERROR! ${schemaName} - schema validation failed!`)
|
|
|
+ console.log(`\nERROR! ${jsonSchemaShortPath} - schema validation failed!`)
|
|
|
console.log(ajv.errorsText(undefined, { separator: '\n' }))
|
|
|
console.log('\n')
|
|
|
process.exitCode = 100
|
|
|
- return
|
|
|
+
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+const validateInputAgainstSchema = (input: FetchedInput, jsonSchema: Record<string, unknown>) => {
|
|
|
+ if (!ajv.validate(jsonSchema, input.data)) {
|
|
|
+ console.log(`\nERROR! ${input.fileName} - validation failed!`)
|
|
|
+ console.log(ajv.errorsText(undefined, { separator: '\n' }))
|
|
|
+ console.log('\n')
|
|
|
+ process.exitCode = 100
|
|
|
+
|
|
|
+ return false
|
|
|
}
|
|
|
|
|
|
- // Validate inputs
|
|
|
- console.log('Validating inputs...')
|
|
|
- getInputs(relatedInputType).forEach(({ fileName, data }) => {
|
|
|
- if (!ajv.validate(jsonSchema, data)) {
|
|
|
- console.log(`\nERROR! ${relatedInputType}/${fileName} - validation failed!`)
|
|
|
- console.log(ajv.errorsText(undefined, { separator: '\n' }))
|
|
|
- console.log('\n')
|
|
|
- process.exitCode = 100
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+const getJsonSchemaForInput = (inputType: InputType, input: FetchedInput) => {
|
|
|
+ let schemaLocation = ''
|
|
|
+ if (inputType === 'classes') {
|
|
|
+ schemaLocation = path.join(SCHEMAS_LOCATION, 'extrinsics', 'CreateClass.schema.json')
|
|
|
+ }
|
|
|
+ if (inputType === 'schemas') {
|
|
|
+ schemaLocation = path.join(SCHEMAS_LOCATION, 'extrinsics', 'AddClassSchema.schema.json')
|
|
|
+ }
|
|
|
+ if (inputType === 'entityBatches') {
|
|
|
+ const jsonSchemaFilename = input.fileName.split('_')[1].replace('.json', '.schema.json')
|
|
|
+ schemaLocation = path.join(SCHEMAS_LOCATION, 'entityBatches', jsonSchemaFilename)
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ jsonSchemaPath: schemaLocation,
|
|
|
+ jsonSchema: JSON.parse(fs.readFileSync(schemaLocation).toString()),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const alreadyValidatedJsonSchemas = new Map<string, boolean>()
|
|
|
+INPUT_TYPES.forEach((inputType) => {
|
|
|
+ console.log(`Validating inputs/${inputType} and related json-schemas...\n`)
|
|
|
+ getInputs(inputType).forEach((input) => {
|
|
|
+ const { jsonSchemaPath, jsonSchema } = getJsonSchemaForInput(inputType, input)
|
|
|
+ const jsonSchemaShortPath = path.relative(path.join(SCHEMAS_LOCATION, '..'), jsonSchemaPath)
|
|
|
+ // Validate the schema itself
|
|
|
+ let isJsonSchemaValid = alreadyValidatedJsonSchemas.get(jsonSchemaShortPath)
|
|
|
+ if (isJsonSchemaValid === undefined) {
|
|
|
+ console.log(`Validating ${jsonSchemaShortPath}...`)
|
|
|
+ isJsonSchemaValid = validateJsonSchema(jsonSchemaShortPath, jsonSchema)
|
|
|
+ alreadyValidatedJsonSchemas.set(jsonSchemaShortPath, isJsonSchemaValid)
|
|
|
+ }
|
|
|
+ if (!isJsonSchemaValid) {
|
|
|
+ return
|
|
|
}
|
|
|
+ console.log(`Validating inputs/${inputType}/${input.fileName}...`)
|
|
|
+ validateInputAgainstSchema(input, jsonSchema)
|
|
|
})
|
|
|
|
|
|
console.log('\n\n')
|