Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #64 #65

Merged
merged 4 commits into from
Dec 12, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@
import com.networknt.jsonoverlay.Overlay;
import com.networknt.oas.model.Parameter;
import com.networknt.oas.model.RequestBody;
import com.networknt.oas.model.impl.ParameterImpl;
import com.networknt.oas.model.impl.RequestBodyImpl;
import com.networknt.oas.model.impl.SchemaImpl;
import com.networknt.schema.ValidatorConfig;
import com.networknt.status.Status;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.HeaderValues;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.Collection;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -109,7 +110,9 @@ private Status validateRequestBody(Object requestBody, final OpenApiOperation op
}
return null;
}
return schemaValidator.validate(requestBody, Overlay.toJson((SchemaImpl)specBody.getContentMediaType("application/json").getSchema()));
ValidatorConfig config = new ValidatorConfig();
config.setTypeLoose(false);
return schemaValidator.validate(requestBody, Overlay.toJson((SchemaImpl)specBody.getContentMediaType("application/json").getSchema()), config);
}

private Status validatePathParameters(final NormalisedPath requestPath, final OpenApiOperation openApiOperation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.ValidationMessage;
import com.networknt.schema.ValidatorConfig;
import com.networknt.status.Status;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -74,15 +73,19 @@ public SchemaValidator(final OpenApi3 api) {
*
* @param value The value to validate
* @param schema The property schema to validate the value against
* @param config The config model for some validator
*
* @return A status containing error code and description
*/
public Status validate(final Object value, final JsonNode schema) {
return doValidate(value, schema);
public Status validate(final Object value, final JsonNode schema, ValidatorConfig config) {
return doValidate(value, schema, config);
}

public Status validate(final Object value, final JsonNode schema) {
return doValidate(value, schema, null);
}

private Status doValidate(final Object value, final JsonNode schema) {
private Status doValidate(final Object value, final JsonNode schema, ValidatorConfig config) {
requireNonNull(schema, "A schema is required");

Status status = null;
Expand All @@ -91,7 +94,7 @@ private Status doValidate(final Object value, final JsonNode schema) {
if(jsonNode != null) {
((ObjectNode)schema).set(COMPONENTS_FIELD, jsonNode);
}
JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schema);
JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schema, config);
final JsonNode content = Config.getInstance().getMapper().valueToTree(value);
processingReport = jsonSchema.validate(content);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public class ValidatorHandler implements MiddlewareHandler {

static final Logger logger = LoggerFactory.getLogger(ValidatorHandler.class);

static ValidatorConfig config;
static ValidatorHandlerConfig config;
static {
config = (ValidatorConfig)Config.getInstance().getJsonObjectConfig(OPENAPI_CONFIG_NAME, ValidatorConfig.class);
config = (ValidatorHandlerConfig)Config.getInstance().getJsonObjectConfig(OPENAPI_CONFIG_NAME, ValidatorHandlerConfig.class);
if(config == null) {
config = (ValidatorConfig)Config.getInstance().getJsonObjectConfig(CONFIG_NAME, ValidatorConfig.class);
config = (ValidatorHandlerConfig)Config.getInstance().getJsonObjectConfig(CONFIG_NAME, ValidatorHandlerConfig.class);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
*
* @author Steve Hu
*/
public class ValidatorConfig {
public class ValidatorHandlerConfig {
boolean enabled;
boolean logError;

public ValidatorConfig() {
public ValidatorHandlerConfig() {
}

public boolean isEnabled() {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<version.swagger-parser>1.0.34</version.swagger-parser>
<version.swagger-core>1.5.18</version.swagger-core>
<version.jsr305>3.0.2</version.jsr305>
<version.json-schema-validator>0.1.23</version.json-schema-validator>
<version.json-schema-validator>0.1.24</version.json-schema-validator>
<versions.maven-version>2.4</versions.maven-version>
<argLine>-Xmx512m -XX:MaxPermSize=256m</argLine>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.networknt.validator;

import com.networknt.body.BodyHandler;
import com.networknt.schema.ValidatorConfig;
import com.networknt.status.Status;
import com.networknt.swagger.NormalisedPath;
import com.networknt.swagger.SwaggerOperation;
Expand All @@ -28,7 +29,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.Collection;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -111,6 +113,8 @@ private Status validateRequestBody(Object requestBody,
}
return null;
}
ValidatorConfig config = new ValidatorConfig();
config.setTypeLoose(false);
return schemaValidator.validate(requestBody, ((BodyParameter)bodyParameter.get()).getSchema());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.networknt.config.Config;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.ValidationMessage;
import com.networknt.schema.ValidatorConfig;
import com.networknt.status.Status;
import com.networknt.utility.Util;
import io.swagger.models.Model;
Expand Down Expand Up @@ -79,22 +79,27 @@ public SchemaValidator(final Swagger api) {
* @return A status containing error code and description
*/
public Status validate(final Object value, final Property schema) {
return doValidate(value, schema);
return doValidate(value, schema, null);
}

/**
* Validate the given value against the given model schema.
*
* @param value The value to validate
* @param schema The model schema to validate the value against
* @param config The config model for some validator
*
* @return A status containing error code and description
*/
public Status validate(final Object value, final Model schema, ValidatorConfig config) {
return doValidate(value, schema, config);
}

public Status validate(final Object value, final Model schema) {
return doValidate(value, schema);
return doValidate(value, schema, null);
}

private Status doValidate(final Object value, final Object schema) {
private Status doValidate(final Object value, final Object schema, ValidatorConfig config) {
requireNonNull(schema, "A schema is required");

Status status = null;
Expand All @@ -109,7 +114,7 @@ private Status doValidate(final Object value, final Object schema) {
((ObjectNode)schemaObject).set(DEFINITIONS_FIELD, this.definitions);
}

JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schemaObject);
JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schemaObject, config);

final JsonNode content = Json.mapper().valueToTree(value);
processingReport = jsonSchema.validate(content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public class ValidatorHandler implements MiddlewareHandler {

static final Logger logger = LoggerFactory.getLogger(ValidatorHandler.class);

static ValidatorConfig config;
static ValidatorHandlerConfig config;
static {
config = (ValidatorConfig)Config.getInstance().getJsonObjectConfig(SWAGGER_CONFIG_NAME, ValidatorConfig.class);
config = (ValidatorHandlerConfig)Config.getInstance().getJsonObjectConfig(SWAGGER_CONFIG_NAME, ValidatorHandlerConfig.class);
if(config == null) {
config = (ValidatorConfig)Config.getInstance().getJsonObjectConfig(CONFIG_NAME, ValidatorConfig.class);
config = (ValidatorHandlerConfig)Config.getInstance().getJsonObjectConfig(CONFIG_NAME, ValidatorHandlerConfig.class);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@

package com.networknt.validator;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
* Validator configuration class that maps to validator.yml properties
*
* @author Steve Hu
*/
public class ValidatorConfig {
public class ValidatorHandlerConfig {
boolean enabled;
boolean logError;

public ValidatorConfig() {
public ValidatorHandlerConfig() {
}

public boolean isEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
"name": "Bob",
"email": "[email protected]",
"id": 1
}