Skip to content

Commit bedb367

Browse files
santoshaherkarstevehu
authored andcommitted
API-314: changed schema validator to accept name of the property being validated so that it can be displayed in error. (#124)
1 parent 6b1a28d commit bedb367

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

openapi-validator/src/main/java/com/networknt/openapi/RequestValidator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private Status validateRequestBody(Object requestBody, final OpenApiOperation op
123123
config.setTypeLoose(false);
124124
config.setHandleNullableField(ValidatorHandler.config.isHandleNullableField());
125125

126-
return schemaValidator.validate(requestBody, Overlay.toJson((SchemaImpl)specBody.getContentMediaType("application/json").getSchema()), config);
126+
return schemaValidator.validate(requestBody, Overlay.toJson((SchemaImpl)specBody.getContentMediaType("application/json").getSchema()), config, "requestBody");
127127
}
128128

129129
private Status validateRequestParameters(final HttpServerExchange exchange, final NormalisedPath requestPath, final OpenApiOperation openApiOperation) {
@@ -170,7 +170,7 @@ private Status validatePathParameters(final HttpServerExchange exchange, final N
170170
logger.info("Path parameter cannot be decoded, it will be used directly");
171171
}
172172

173-
return schemaValidator.validate(paramValue, Overlay.toJson((SchemaImpl)(parameter.get().getSchema())));
173+
return schemaValidator.validate(paramValue, Overlay.toJson((SchemaImpl)(parameter.get().getSchema())), paramName);
174174
}
175175
}
176176
return status;
@@ -212,7 +212,7 @@ private Status validateQueryParameter(final HttpServerExchange exchange,
212212

213213
Optional<Status> optional = queryParameterValues
214214
.stream()
215-
.map((v) -> schemaValidator.validate(v, Overlay.toJson((SchemaImpl)queryParameter.getSchema())))
215+
.map((v) -> schemaValidator.validate(v, Overlay.toJson((SchemaImpl)queryParameter.getSchema()), queryParameter.getName()))
216216
.filter(s -> s != null)
217217
.findFirst();
218218

@@ -221,7 +221,7 @@ private Status validateQueryParameter(final HttpServerExchange exchange,
221221
// Since if the queryParameterValue's length larger than 2, it means the query parameter is an array.
222222
// thus array validation should be applied, for example, validate the length of the array.
223223
} else {
224-
return schemaValidator.validate(queryParameterValues, Overlay.toJson((SchemaImpl)queryParameter.getSchema()));
224+
return schemaValidator.validate(queryParameterValues, Overlay.toJson((SchemaImpl)queryParameter.getSchema()), queryParameter.getName());
225225
}
226226
return null;
227227
}
@@ -321,7 +321,7 @@ private Status validateHeader(final HttpServerExchange exchange,
321321
} else {
322322
Optional<Status> optional = headerValues
323323
.stream()
324-
.map((v) -> schemaValidator.validate(v, Overlay.toJson((SchemaImpl)headerParameter.getSchema())))
324+
.map((v) -> schemaValidator.validate(v, Overlay.toJson((SchemaImpl)headerParameter.getSchema()), headerParameter.getName()))
325325
.filter(s -> s != null)
326326
.findFirst();
327327
return optional.orElse(null);
@@ -340,7 +340,7 @@ private ValidationResult validateDeserializedValues(final HttpServerExchange exc
340340
if (null==deserializedValue) {
341341
validationResult.addSkipped(p);
342342
}else {
343-
Status s = schemaValidator.validate(deserializedValue, Overlay.toJson((SchemaImpl)(p.getSchema())));
343+
Status s = schemaValidator.validate(deserializedValue, Overlay.toJson((SchemaImpl)(p.getSchema())), p.getName());
344344
validationResult.addStatus(s);
345345
}
346346
});

openapi-validator/src/main/java/com/networknt/openapi/SchemaValidator.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,31 @@ public SchemaValidator(final OpenApi3 api) {
8181
* @return A status containing error code and description
8282
*/
8383
public Status validate(final Object value, final JsonNode schema, SchemaValidatorsConfig config) {
84-
return doValidate(value, schema, config);
84+
return doValidate(value, schema, config, "$");
85+
}
86+
87+
/**
88+
* Validate the given value against the given property schema.
89+
*
90+
* @param value The value to validate
91+
* @param schema The property schema to validate the value against
92+
* @param config The config model for some validator
93+
* @param at The name of the property being validated
94+
* @return
95+
*/
96+
public Status validate(final Object value, final JsonNode schema, SchemaValidatorsConfig config, String at) {
97+
return doValidate(value, schema, config, at);
8598
}
8699

87100
public Status validate(final Object value, final JsonNode schema) {
88-
return doValidate(value, schema, defaultConfig);
101+
return doValidate(value, schema, defaultConfig, "$");
102+
}
103+
104+
public Status validate(final Object value, final JsonNode schema, String at) {
105+
return doValidate(value, schema, defaultConfig, at);
89106
}
90107

91-
private Status doValidate(final Object value, final JsonNode schema, SchemaValidatorsConfig config) {
108+
private Status doValidate(final Object value, final JsonNode schema, SchemaValidatorsConfig config, String at) {
92109
requireNonNull(schema, "A schema is required");
93110

94111
Status status = null;
@@ -99,7 +116,7 @@ private Status doValidate(final Object value, final JsonNode schema, SchemaValid
99116
}
100117
JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schema, config);
101118
final JsonNode content = Config.getInstance().getMapper().valueToTree(value);
102-
processingReport = jsonSchema.validate(content);
119+
processingReport = jsonSchema.validate(content, content, at);
103120
} catch (Exception e) {
104121
e.printStackTrace();
105122
}

0 commit comments

Comments
 (0)