strong-tie/inbound-calls
0
1'use strict'2 3const http = require('node:http')4const AjvStandaloneCompiler = require('@fastify/ajv-compiler/standalone')5const fs = require('node:fs')6const path = require('node:path')7 8const urlSchema = {9 oneOf: [10 { type: 'string' },11 {12 type: 'object',13 properties: {14 protocol: { type: 'string' },15 hostname: { type: 'string' },16 pathname: { type: 'string' }17 // port type => any18 // query type => any19 },20 additionalProperties: true,21 required: ['pathname']22 }23 ]24}25 26const schema = {27 type: 'object',28 properties: {29 url: urlSchema,30 path: urlSchema,31 cookies: {32 type: 'object',33 additionalProperties: true34 },35 headers: {36 type: 'object',37 additionalProperties: true38 },39 query: {40 anyOf: [41 {42 type: 'object',43 additionalProperties: true44 },45 {46 type: 'string'47 }48 ]49 },50 simulate: {51 type: 'object',52 properties: {53 end: { type: 'boolean' },54 split: { type: 'boolean' },55 error: { type: 'boolean' },56 close: { type: 'boolean' }57 }58 },59 authority: { type: 'string' },60 remoteAddress: { type: 'string' },61 method: { type: 'string', enum: http.METHODS.concat(http.METHODS.map(toLowerCase)) },62 validate: { type: 'boolean' }63 // payload type => any64 },65 additionalProperties: true,66 oneOf: [67 { required: ['url'] },68 { required: ['path'] }69 ]70}71 72function toLowerCase (m) { return m.toLowerCase() }73 74const factory = AjvStandaloneCompiler({75 readMode: false,76 storeFunction (routeOpts, schemaValidationCode) {77 const moduleCode = `// This file is autogenerated by ${__filename.replace(__dirname, 'build')}, do not edit78/* c8 ignore start */79/* eslint-disable */80${schemaValidationCode}81`82 const file = path.join(__dirname, '..', 'lib', 'config-validator.js')83 fs.writeFileSync(file, moduleCode)84 console.log(`Saved ${file} file successfully`)85 }86})87 88const compiler = factory({}, {89 customOptions: {90 code: {91 source: true,92 lines: true,93 optimize: 394 },95 removeAdditional: true,96 coerceTypes: true97 }98})99 100compiler({ schema })101 