嵌套列表中的Cerberus强制

时间:2018-12-09 02:01:18

标签: python list validation cerberus coerce

以下代码出现意外行为:

$GroupSize = $_POST['groupsize'];

$con = mysqli_connect($host, $user, $password, $dbName) or die("Nope");

if (isset($_POST['create'])) {

//assign group id to groups created
//insert RegistrationId into groupinformation from records randomly selected from userInformation,  group Id must increment by every group created in groupinformation

//$query ="Select RegistrationId from userInformation order by RAND() LIMIT ".$GroupSize;
   $query = "INSERT INTO groupInformation SELECT RegistrationId FROM (SELECT RegistrationId FROM userInformation ORDER BY RAND() LIMIT ".$GroupSize.") as res";

    $result = mysqli_query($con, $query) or die ("query failed " . mysqli_error($con));


//unsure how to use this array
    while (($row = mysqli_fetch_row($result)) == true) {
        echo $row[0].'<br>';

    }

我希望不会有任何错误,因为强制应该照顾类型。但是我得到了

import cerberus
v = cerberus.Validator()
schema = {'list_of_values': {'type': 'list',
                             'schema': {'items': [{'type': 'string', 'coerce': str},
                                                  {'type': 'integer', 'coerce': int}]}}
                             }
document = {'list_of_values': [['hello', 100], [123, "122"]]}
v.validate(document, schema)
v.errors

这是一个错误吗?我是不是误解了胁迫的原理?

1 个答案:

答案 0 :(得分:0)

@ funky-future

有些不对劲,我确实可以通过将示例复制并粘贴到提示中来重现该问题:

>>> import cerberus
>>> v = cerberus.Validator()
>>> schema = {'list_of_values': {'type': 'list',
...                              'schema': {'items': [{'type': 'string', 'coerce': str},
...                                                   {'type': 'integer', 'coerce': int}]}}
...                              }
>>> document = {'list_of_values': [['hello', 100], [123, "122"]]}
>>> v.validate(document, schema)
False
>>> v.errors
{'list_of_values': [{1: [{0: ['must be of string type'], 1: ['must be of integer type']}]}]}

Python3.5.2,cerberus1.2