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

Feature: 自定义验证schema.string是否符合条件。 #1104

Closed
Tracked by #1085
BERADQ opened this issue Jun 2, 2023 · 5 comments
Closed
Tracked by #1085

Feature: 自定义验证schema.string是否符合条件。 #1104

BERADQ opened this issue Jun 2, 2023 · 5 comments
Labels
feature 新特性

Comments

@BERADQ
Copy link

BERADQ commented Jun 2, 2023

Describe the problem related to the feature request

很显然,string是很通用的一个类型,也经常使用

在我开发的插件的用户群体里接到的问题反馈,很大一部分都是由于字符串的格式错误造成的问题。

我希望可以使用一个回调函数来验证字符串格式是否正确,来提高用户体验和string的通用性。

Describe the solution you'd like

//required不可以因为check而省略,因为check不一定保证了值非空从而无法确定类型。
Schema.string().required().check((value)=>{
  //...
  return true or false;
})
//+============================+
CheckCallback(value:string):boolean

如上伪代码,check或许还需要防抖,

所谓防抖,就是指触发事件后 n 秒后才执行函数,如果在 n 秒内又触发了事件,则会重新计算函数执行时间。
这里需要的防抖是为了减少频繁复杂验证带来的卡顿,n可以为800ms (属于是经验来的)

检查在前端或后端实现都是可以的,但是在前端实现可以带来更好的用户体验。

当回调函数返回true时,值符合条件。返回false时,值不符合条件,此时项目标红禁止插件启动。

Describe alternatives you've considered

我尝试过通过启动插件时检查值是否符合规定,如不符合则throw error来阻止用户启动插件。

但是,这样无法直观的表达是那一个值出现的问题,我尝试通过error来告知用户,

但是总是有人不看log,直接放弃使用或询问。

Additional context

schema非常好,甚至我都想拿来用在自己写的玩具上了。

@BERADQ BERADQ added the feature 新特性 label Jun 2, 2023
@shigma
Copy link
Member

shigma commented Jun 2, 2023

我也觉得是个好主意,但是在后端定义的函数并不好传到前端。所以我目前比较倾向于只提供一些可序列化的检验能力,例如正则表达式之类?

@BERADQ
Copy link
Author

BERADQ commented Jun 2, 2023

我也觉得是个好主意,但是在后端定义的函数并不好传到前端。所以我目前比较倾向于只提供一些可序列化的检验能力,例如正则表达式之类?

可行,但是既然正则可以作为字符串传递,函数也是不是不可以。

如果使用正则的话,我不是很清楚一个问题:

const str0 = `beee
teeested str
eee`
const str1 = `eee`

//我想要匹配三个连续的字符且仅能是三个连续的字符。例如上面两个变量,能被匹配的只有str1。这个能实现吗,原谅我正则水平不足

//关于函数转为字符串
const a = function(){console.log(123)}
or
const a = () => {console.log(123)}
a.toString();
// ^ "function(){console.log(123)}"

//关于字符串转为函数
Function(srting) or eval(string)

@shigma
Copy link
Member

shigma commented Jun 2, 2023

你这种方案是不行的,函数里只要有闭包变量就无法 eval。

@BERADQ
Copy link
Author

BERADQ commented Jun 2, 2023

是我考虑不周,

那么就使用正则表达式吧。我刚才的问题好像可以通过正则表达式的正|反向预查来解决(不细说了)。

@shigma
Copy link
Member

shigma commented Jul 7, 2023

正则表达式已经支持了。

其实后端早就支持了,最新的更新 shigma/schemastery@01bee32 支持了正则表达式的前端错误提示。

@shigma shigma closed this as completed Jul 7, 2023
shigma added a commit to shigma/schemastery that referenced this issue Jul 7, 2023
@shigma shigma mentioned this issue Jul 7, 2023
21 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 新特性
Projects
None yet
Development

No branches or pull requests

2 participants