AnzhiZhang commented on issue #3692:
URL: https://github.com/apache/texera/issues/3692#issuecomment-3230132435
<details>
<summary>Origin issue, for reference only, no need to read.</summary>
## Problem
Currently, we have many operator descriptions using `@JsonProperty(required
= true)` on string fields. For example, chart operators (see #3192).
https://github.com/apache/texera/blob/c7fcd9f4278f850e3c1456866ba6f25f85a8f489/core/workflow-operator/src/main/scala/edu/uci/ics/amber/operator/visualization/barChart/BarChartOpDesc.scala#L41-L70
For example, if we have this class.
```scala
import com.fasterxml.jackson.annotation.JsonProperty
class Test {
@JsonProperty(value = "test", required = true)
var test: String = ""
}
```
The JSON schema will be
```json
{
"$schema" : "http://json-schema.org/draft-07/schema#",
"type" : "object",
"additionalProperties" : false,
"properties" : {
"test" : {
"propertyOrder" : 1,
"type" : "string",
"title" : "Test"
}
},
"required" : [ "test" ]
}
```
We are using [Ajv](https://github.com/ajv-validator/ajv) in the frontend to
validate JSON schema, if we test this schema in the frontend (a simple example).
```ts
import Ajv from "ajv";
const ajv = new Ajv({ allErrors: true, strict: false });
const schema = {
type: "object",
properties: {
test: {
type: "string",
},
},
required: ["test"],
};
const json = {
test: "",
};
// true
console.log(ajv.validate(schema, json));
const json2 = {};
// false
console.log(ajv.validate(schema, json2));
```
It only works for a real null property, not an empty string.
The debug information confirms this finding.
<img width="2360" height="1030" alt="Image"
src="https://github.com/user-attachments/assets/9d7e1876-9dfe-40b2-862c-0dadc16fcab7"
/>
<img width="2384" height="812" alt="Image"
src="https://github.com/user-attachments/assets/8e0f026c-37a7-4070-8516-347589559f86"
/>
## Solution
We will need to add `@NotNull` annotation from
`javax.validation.constraints.NotNull` to introduce `"minLength" : 1,`
requirement to the JSON Schema
(https://github.com/ajv-validator/ajv/issues/885#issuecomment-441749423).
```java
import com.fasterxml.jackson.annotation.JsonProperty
import javax.validation.constraints.NotNull
class Test {
@JsonProperty(value = "test", required = true)
@NotNull(message = "test cannot be empty")
var test: String = ""
}
```
Will generate the schema.
```json
{
"$schema" : "http://json-schema.org/draft-07/schema#",
"type" : "object",
"additionalProperties" : false,
"properties" : {
"test" : {
"propertyOrder" : 1,
"type" : "string",
"minLength" : 1,
"title" : "Test"
}
},
"required" : [ "test" ]
}
```
To test it.
```ts
const schema2 = {
type: "object",
properties: {
test: {
type: "string",
minLength : 1,
},
},
required: ["test"],
};
// false
console.log(ajv.validate(schema2, json));
```
## Alternative Solutions
To validate "empty string" (in the context of Java empty stringļ¼ which is 0
length) in Ajv, the only solution is `minLength`, a more detailed discussion
can be found here (https://stackoverflow.com/a/47471462/19420247)
1. Use `javax.validation.constraints.Pattern` to validate by regex pattern,
which also resolves the issue if the input string that all chars are spaces,
but may require more performance and is not consistent with Java.
2. Catch error from backend. This is a workaround, as we are expecting to
validate at the frontend. If we choose this solution, the validation status at
the front will be valid at first, then turn to invalid. This will reflect on
the Run button (flash).
## Demo
This demo shows the effect of adding `@NotNull`. The bar chart operator is
edited to have `@NotNull`. When adding the bar chart operator, the workflow is
consistently invalid. However, without this requirement on the dot plot
operator, the workflow changes to valid sometimes because these validations are
passed due to empty string attributes (see debug screenshots above).
https://github.com/user-attachments/assets/067a7e84-b4e4-4a5c-b83a-b5a320914f6c
## Related Issues
- #3656
- #3192
- https://github.com/ajv-validator/ajv/issues/885#issuecomment-441749423
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]