validate.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // TODO: Add entity batches validation
  2. import Ajv from 'ajv'
  3. import { FetchedInput, getInputs, InputType, INPUT_TYPES } from './helpers/inputs'
  4. import path from 'path'
  5. import fs from 'fs'
  6. const SCHEMAS_LOCATION = path.join(__dirname, '../schemas')
  7. const ajv = new Ajv({ allErrors: true })
  8. // Add all existing schemas to Ajv to allow them to reference each other
  9. fs.readdirSync(SCHEMAS_LOCATION)
  10. .filter((subdir) => !subdir.includes('.'))
  11. .forEach((subdir) => {
  12. fs.readdirSync(path.join(SCHEMAS_LOCATION, subdir)).forEach((schemaFilename) => {
  13. ajv.addSchema(JSON.parse(fs.readFileSync(path.join(SCHEMAS_LOCATION, subdir, schemaFilename)).toString()))
  14. })
  15. })
  16. const validateJsonSchema = (jsonSchemaShortPath: string, jsonSchema: Record<string, unknown>) => {
  17. if (!ajv.validateSchema(jsonSchema)) {
  18. console.log(`\nERROR! ${jsonSchemaShortPath} - schema validation failed!`)
  19. console.log(ajv.errorsText(undefined, { separator: '\n' }))
  20. console.log('\n')
  21. process.exitCode = 100
  22. return false
  23. }
  24. return true
  25. }
  26. const validateInputAgainstSchema = (input: FetchedInput, jsonSchema: Record<string, unknown>) => {
  27. if (!ajv.validate(jsonSchema, input.data)) {
  28. console.log(`\nERROR! ${input.fileName} - validation failed!`)
  29. console.log(ajv.errorsText(undefined, { separator: '\n' }))
  30. console.log('\n')
  31. process.exitCode = 100
  32. return false
  33. }
  34. return true
  35. }
  36. const getJsonSchemaForInput = (inputType: InputType, input: FetchedInput) => {
  37. let schemaLocation = ''
  38. if (inputType === 'classes') {
  39. schemaLocation = path.join(SCHEMAS_LOCATION, 'extrinsics', 'CreateClass.schema.json')
  40. }
  41. if (inputType === 'schemas') {
  42. schemaLocation = path.join(SCHEMAS_LOCATION, 'extrinsics', 'AddClassSchema.schema.json')
  43. }
  44. if (inputType === 'entityBatches') {
  45. const jsonSchemaFilename = input.fileName.split('_')[1].replace('.json', '.schema.json')
  46. schemaLocation = path.join(SCHEMAS_LOCATION, 'entityBatches', jsonSchemaFilename)
  47. }
  48. return {
  49. jsonSchemaPath: schemaLocation,
  50. jsonSchema: JSON.parse(fs.readFileSync(schemaLocation).toString()),
  51. }
  52. }
  53. const alreadyValidatedJsonSchemas = new Map<string, boolean>()
  54. INPUT_TYPES.forEach((inputType) => {
  55. console.log(`Validating inputs/${inputType} and related json-schemas...\n`)
  56. getInputs(inputType).forEach((input) => {
  57. const { jsonSchemaPath, jsonSchema } = getJsonSchemaForInput(inputType, input)
  58. const jsonSchemaShortPath = path.relative(path.join(SCHEMAS_LOCATION, '..'), jsonSchemaPath)
  59. // Validate the schema itself
  60. let isJsonSchemaValid = alreadyValidatedJsonSchemas.get(jsonSchemaShortPath)
  61. if (isJsonSchemaValid === undefined) {
  62. console.log(`Validating ${jsonSchemaShortPath}...`)
  63. isJsonSchemaValid = validateJsonSchema(jsonSchemaShortPath, jsonSchema)
  64. alreadyValidatedJsonSchemas.set(jsonSchemaShortPath, isJsonSchemaValid)
  65. }
  66. if (!isJsonSchemaValid) {
  67. return
  68. }
  69. console.log(`Validating inputs/${inputType}/${input.fileName}...`)
  70. validateInputAgainstSchema(input, jsonSchema)
  71. })
  72. console.log('\n\n')
  73. })