|
15 | 15 | package model
|
16 | 16 |
|
17 | 17 | import (
|
18 |
| - "bytes" |
19 | 18 | "encoding/json"
|
| 19 | + "regexp" |
20 | 20 |
|
21 | 21 | "github.com/serverlessworkflow/sdk-go/v2/util"
|
22 | 22 | )
|
@@ -122,7 +122,7 @@ type BaseWorkflow struct {
|
122 | 122 | // qualities.
|
123 | 123 | // +optional
|
124 | 124 | Annotations []string `json:"annotations,omitempty"`
|
125 |
| - // DataInputSchema URI of the JSON Schema used to validate the workflow data input |
| 125 | + // DataInputSchema URI or Object of the JSON Schema used to validate the workflow data input |
126 | 126 | // +optional
|
127 | 127 | DataInputSchema *DataInputSchema `json:"dataInputSchema,omitempty"`
|
128 | 128 | // Serverless Workflow schema version
|
@@ -522,12 +522,32 @@ type dataInputSchemaUnmarshal DataInputSchema
|
522 | 522 | // UnmarshalJSON implements json.Unmarshaler
|
523 | 523 | func (d *DataInputSchema) UnmarshalJSON(data []byte) error {
|
524 | 524 | d.ApplyDefault()
|
525 |
| - if data[0] == '"' && len(data) > 0 { |
526 |
| - replaced := bytes.Replace(data, []byte(`"`), []byte(``), -1) |
527 |
| - repp := FromString(string(replaced)) |
528 |
| - d.Schema = &repp |
529 |
| - } else { |
530 |
| - return util.UnmarshalObject("dataInputSchema", data, (*dataInputSchemaUnmarshal)(d)) |
| 525 | + err := util.UnmarshalObject("dataInputSchema", data, (*dataInputSchemaUnmarshal)(d)) |
| 526 | + if err != nil { |
| 527 | + |
| 528 | + return err |
| 529 | + } |
| 530 | + |
| 531 | + if d.Schema != nil && d.Schema.Type == String { |
| 532 | + // Define the regex pattern to match the prefixes |
| 533 | + pattern := `^(http|https|file)` |
| 534 | + regex := regexp.MustCompile(pattern) |
| 535 | + // if it is not external, treat as JSON object |
| 536 | + if !regex.MatchString(d.Schema.StringValue) { |
| 537 | + point := FromString(d.Schema.StringValue) |
| 538 | + d.Schema = &point |
| 539 | + return nil |
| 540 | + } |
| 541 | + |
| 542 | + data, err := util.LoadExternalResource(d.Schema.StringValue) |
| 543 | + if err != nil { |
| 544 | + return err |
| 545 | + } |
| 546 | + |
| 547 | + er := util.UnmarshalObject("schema", data, &d.Schema) |
| 548 | + // clean the string value to avoid the json URI being appended |
| 549 | + d.Schema.StringValue = "" |
| 550 | + return er |
531 | 551 | }
|
532 | 552 | return nil
|
533 | 553 | }
|
|
0 commit comments