小编EzP*_*zza的帖子

手动设置 Flutter 验证错误

在验证表单并将请求从 flutter 发送到服务器后端后:我想将来自服务器的任何潜在错误消息设置为以原始表单显示。最好完全像验证错误。

例如:

Widget build(BuildContext context) {
...
  TextFormField(
    onFieldSubmitted: (value) => _signIn(),
    validator: (input) {
      if (input.length < 6)
        return 'Your password is too short';
      return null;
    },
    onSaved: (input) => _password = input,
    decoration: InputDecoration(
      labelText: 'Password',
    ),
    obscureText: true,
  )
...
}

Future<void> _signIn() async {
  final formState = _formKey.currentState;
  if (!formState.validate()) return;
  formState.save();

  try {
    ... // do fancy request stuff
  } catch (e) {
    // this is where I want to set the "validation" …
Run Code Online (Sandbox Code Playgroud)

forms validation error-handling dart flutter

7
推荐指数
1
解决办法
7723
查看次数

如何使用 jsonpath-ng 算术?

jsonpath-ng包声称支持基本算术(https://pypi.org/project/jsonpath-ng/),但解析器不会接受算术语句。这是其中之一:

from jsonpath_ng import parse

jsonpath_expr = parse('$.objects[*].cow + $.objects[*].cat')
obj = {'objects': [
  {'cow': 2, 'cat': 3},
  {'cow': 4, 'cat': 6}
]}
values = [match.value for match in jsonpath_expr.find(obj)]  
print(values)
Run Code Online (Sandbox Code Playgroud)

这会引发错误:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    jsonpath_expr = parse('$.objects[*].cow + $.objects[*].cat')
  File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\jsonpath_ng\parser.py", line 14, in parse
    return JsonPathParser().parse(string)
  File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\jsonpath_ng\parser.py", line 32, in parse
    return self.parse_token_stream(lexer.tokenize(string))
  File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\jsonpath_ng\parser.py", line 55, in parse_token_stream
    return new_parser.parse(lexer = IteratorToTokenStream(token_iterator))
  File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\ply\yacc.py", line …
Run Code Online (Sandbox Code Playgroud)

python jsonpath jsonpath-ng

4
推荐指数
1
解决办法
671
查看次数

列出 Firestore 安全规则中的安全类型

在我的 firestore 安全规则中,我想要一个规则,检查列表的所有元素是否都是字符串类型。

我的“用户”文档有一个名为“朋友”的字段。它是一个字符串列表,表示其他用户文档的文档 ID。这些是我目前的规则:

function userIsAuthenticated() {
  return request.auth != null;
}

match /users/{userID} {

  function resourceIsValidUser() {
    return displayNameIsValid();
  }

  function displayNameIsValid() {
    return request.resource.data.displayName is string &&
      request.resource.data.displayName.size() > 0 &&
      request.resource.data.displayName.size() < 17;
  }

  function photoUrlIsValid() {
    return request.resource.data.photoUrl is string;
  }

  function friendsIsValid() {
    return request.resource.data.friends is list;
  }

  function userIsUserOwner() {
    return request.auth.uid == userID;
  }

  allow read: if userIsAuthenticated();
  allow write: if
    userIsAuthenticated() &&
    resourceIsValidUser() &&
    userIsUserOwner();

}
Run Code Online (Sandbox Code Playgroud)

有什么我可以添加到friendsIsValid()函数中以确保朋友列表只包含字符串值的吗?

type-safety firebase firebase-security google-cloud-firestore

3
推荐指数
1
解决办法
817
查看次数