-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix #498] Adding possibility to validate before parsing
Fix #498 Signed-off-by: Francisco Javier Tirado Sarti <[email protected]>
- Loading branch information
Showing
12 changed files
with
248 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
api/src/main/java/io/serverlessworkflow/api/DirectReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2020-Present The Serverless Workflow Specification Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.serverlessworkflow.api; | ||
|
||
import io.serverlessworkflow.api.types.Workflow; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
|
||
class DirectReader implements WorkflowReaderOperations { | ||
|
||
@Override | ||
public Workflow read(InputStream input, WorkflowFormat format) throws IOException { | ||
return format.mapper().readValue(input, Workflow.class); | ||
} | ||
|
||
@Override | ||
public Workflow read(Reader input, WorkflowFormat format) throws IOException { | ||
return format.mapper().readValue(input, Workflow.class); | ||
} | ||
|
||
@Override | ||
public Workflow read(byte[] input, WorkflowFormat format) throws IOException { | ||
return format.mapper().readValue(input, Workflow.class); | ||
} | ||
|
||
@Override | ||
public Workflow read(String input, WorkflowFormat format) throws IOException { | ||
return format.mapper().readValue(input, Workflow.class); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
api/src/main/java/io/serverlessworkflow/api/ValidationReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2020-Present The Serverless Workflow Specification Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.serverlessworkflow.api; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.networknt.schema.InputFormat; | ||
import com.networknt.schema.JsonSchema; | ||
import com.networknt.schema.JsonSchemaFactory; | ||
import com.networknt.schema.SchemaValidatorsConfig; | ||
import com.networknt.schema.SpecVersion.VersionFlag; | ||
import com.networknt.schema.ValidationMessage; | ||
import io.serverlessworkflow.api.types.Workflow; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
class ValidationReader implements WorkflowReaderOperations { | ||
private final JsonSchema schemaObject; | ||
|
||
ValidationReader() { | ||
this.schemaObject = | ||
JsonSchemaFactory.getInstance(VersionFlag.V7) | ||
.getSchema( | ||
Thread.currentThread() | ||
.getContextClassLoader() | ||
.getResourceAsStream("schema/workflow.yaml"), | ||
InputFormat.YAML, | ||
SchemaValidatorsConfig.builder().build()); | ||
} | ||
|
||
@Override | ||
public Workflow read(InputStream input, WorkflowFormat format) throws IOException { | ||
return validate(format.mapper().readValue(input, JsonNode.class), format); | ||
} | ||
|
||
@Override | ||
public Workflow read(Reader input, WorkflowFormat format) throws IOException { | ||
return validate(format.mapper().readValue(input, JsonNode.class), format); | ||
} | ||
|
||
@Override | ||
public Workflow read(byte[] input, WorkflowFormat format) throws IOException { | ||
return validate(format.mapper().readValue(input, JsonNode.class), format); | ||
} | ||
|
||
@Override | ||
public Workflow read(String input, WorkflowFormat format) throws IOException { | ||
return validate(format.mapper().readValue(input, JsonNode.class), format); | ||
} | ||
|
||
private Workflow validate(JsonNode value, WorkflowFormat format) { | ||
Set<ValidationMessage> validationErrors = schemaObject.validate(value); | ||
if (!validationErrors.isEmpty()) { | ||
throw new IllegalArgumentException( | ||
validationErrors.stream() | ||
.map(ValidationMessage::toString) | ||
.collect(Collectors.joining("\n"))); | ||
} | ||
return format.mapper().convertValue(value, Workflow.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
api/src/main/java/io/serverlessworkflow/api/WorkflowReaderOperations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2020-Present The Serverless Workflow Specification Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.serverlessworkflow.api; | ||
|
||
import io.serverlessworkflow.api.types.Workflow; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
|
||
public interface WorkflowReaderOperations { | ||
Workflow read(InputStream input, WorkflowFormat format) throws IOException; | ||
|
||
Workflow read(Reader input, WorkflowFormat format) throws IOException; | ||
|
||
Workflow read(byte[] input, WorkflowFormat format) throws IOException; | ||
|
||
Workflow read(String input, WorkflowFormat format) throws IOException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,25 @@ | ||
document: | ||
dsl: 1.0.0-alpha5 | ||
namespace: test | ||
name: call-example | ||
version: 0.1.0 | ||
schedule: | ||
cron: 0 8 * * * | ||
dsl: '1.0.0-alpha5' | ||
namespace: samples | ||
name: call-custom-function-inline | ||
version: '0.1.0' | ||
use: | ||
functions: | ||
getPetById: | ||
input: | ||
schema: | ||
document: | ||
type: object | ||
properties: | ||
petId: | ||
type: string | ||
required: [ petId ] | ||
call: http | ||
with: | ||
method: get | ||
endpoint: https://petstore.swagger.io/v2/pet/{petId} | ||
do: | ||
- getData: | ||
call: http | ||
with: | ||
method: get | ||
endpoint: https://api.agify.io?name=meelad | ||
output: | ||
as: ".data.reading" | ||
- filterData: | ||
for: | ||
in: ".data.reading" | ||
each: reading | ||
do: | ||
- log: | ||
call: https://raw.githubusercontent.com/serverlessworkflow/catalog/main/functions/log/1.0.0/function.yaml | ||
with: | ||
level: information | ||
format: "{TIMESTAMP} [{LEVEL}] ({CONTEXT}): {MESSAGE}" | ||
message: Hello, world! | ||
timestamp: true | ||
- getPet: | ||
call: getPetById | ||
with: | ||
petId: 69 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.