diff --git a/index.d.ts b/index.d.ts index 632cc99..9bcbce5 100644 --- a/index.d.ts +++ b/index.d.ts @@ -176,6 +176,7 @@ declare class Parameter { declare interface ValidateOptions { readonly includeErrors?: string[] + readonly isResponse?: boolean } type Operation = { diff --git a/lib/validation/custom-zschema-validators.js b/lib/validation/custom-zschema-validators.js index f6eccff..d9c5049 100644 --- a/lib/validation/custom-zschema-validators.js +++ b/lib/validation/custom-zschema-validators.js @@ -207,7 +207,7 @@ function readOnlyValidator (report, schema, json) { } var isResponse = this.validateOptions && this.validateOptions.isResponse; - + if (!isResponse && schema && schema.readOnly && json !== undefined) { let errorMessage = 'ReadOnly property `"{0}": '; @@ -216,11 +216,22 @@ function readOnlyValidator (report, schema, json) { } else { errorMessage += '{1}'; } + let propertyName = ''; + if (schema.title && typeof schema.title === 'string') { + try { + var result = JSON.parse(schema.title); + if (result.path && Array.isArray(result.path) && result.path.length) { + propertyName = result.path[result.path.length - 1]; + } + } catch (err) { + // do nothing + } + } errorMessage += '`, cannot be sent in the request.'; report.addCustomError( 'READONLY_PROPERTY_NOT_ALLOWED_IN_REQUEST', errorMessage, - [report.parentReport.path[0], json], + [propertyName, json], null, schema );