Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions compiler/src/steps/validate-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,10 @@ const privateNamespaces = ['_internal', 'profiling']
* Any inconsistency is logged as an error.
*
* Missing validations:
* - verify uniqueness of property names in the inheritance chain
* - verify that request parents don't define properties (would they be path/request/body properties?)
* - verify that unions can be distinguished in a JSON stream (otherwise they should be inheritance trees)
*/
export default async function validateModel (apiModel: model.Model, restSpec: Map<string, JsonSpec>, errors: ValidationErrors): Promise<model.Model> {
// Fail hard if the FAIL_HARD env var is defined
const failHard = process.env.FAIL_HARD != null

const initialTypeCount = apiModel.types.length

// Returns the fully-qualified name of a type name
Expand Down Expand Up @@ -87,13 +83,20 @@ export default async function validateModel (apiModel: model.Model, restSpec: Ma
function modelError (msg: string): void {
const fullMsg = (context.length === 0) ? msg : context.join(' / ') + ' - ' + msg

let ignored = false
if (currentEndpoint != null) {
errors.addEndpointError(currentEndpoint, currentPart, fullMsg)
ignored = errors.addEndpointError(currentEndpoint, currentPart, fullMsg)
if (!ignored) {
console.error(currentEndpoint, currentPart, fullMsg)
}
} else {
errors.addGeneralError(fullMsg)
console.error(fullMsg)
}

errorCount++
if (!ignored) {
errorCount++
}
}

// ----- Type definition management
Expand Down Expand Up @@ -206,8 +209,8 @@ export default async function validateModel (apiModel: model.Model, restSpec: Ma
const danglingTypesCount = initialTypeCount - apiModel.types.length
console.info(`Model validation: ${typesSeen.size} types visited, ${danglingTypesCount} dangling types.`)

if (errorCount > 0 && failHard) {
throw new Error('Model is inconsistent. Check logs for details')
if (errorCount > 0) {
throw new Error('Model is inconsistent.')
}

return apiModel
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/validation-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export class ValidationErrors {
}

/** Add some error information relative to an endpoint's request or response */
addEndpointError (endpoint: string, part: 'request' | 'response', message: string): void {
addEndpointError (endpoint: string, part: 'request' | 'response', message: string): boolean {
if (IGNORED_ERRORS.includes(message)) {
return
return true
}

let error = this.endpointErrors[endpoint]
Expand All @@ -50,6 +50,7 @@ export class ValidationErrors {
}

error[part].push(message)
return false
}

/** Add a general error, unrelated to an endpoint */
Expand Down