validate.ts 2.9 KB

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