Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript io ts的字段名/密钥验证?_Typescript_Fp Ts - Fatal编程技术网

Typescript io ts的字段名/密钥验证?

Typescript io ts的字段名/密钥验证?,typescript,fp-ts,Typescript,Fp Ts,如何确保传入的JSON密钥有效?我使用t.decode,它没有抛出错误 const UserRequired = t.type({ firstName : t.string, lastName : t.string, }) const UserOptional = t.partial({ image : t.string, }) const User = t.intersection([UserRequired, UserOptional]) 传入的JSON有

如何确保传入的JSON密钥有效?我使用t.decode,它没有抛出错误

const UserRequired = t.type({
    firstName : t.string,
    lastName : t.string, 
})

const UserOptional = t.partial({
 
    image : t.string,
})

const User = t.intersection([UserRequired, UserOptional])

传入的JSON有一个图像字段,但其名称为imageee。io ts不会抛出错误。如何确保可选检查?

它不会抛出错误,因为您将
图像
标记为可选字段,因此缺少它并不意味着验证应该失败

如果您不想让除了枚举之外还有任意字段的对象通过验证器,则需要将其包装到
t.strict


旁注:
iots
从不“抛出错误”,它只返回
Left
对象,其中包含失败的验证数据。以防万一:)

它不会抛出错误,因为您将
图像
标记为可选字段,因此缺少它并不意味着验证应该失败

如果您不想让除了枚举之外还有任意字段的对象通过验证器,则需要将其包装到
t.strict

旁注:
iots
从不“抛出错误”,它只返回
Left
对象,其中包含失败的验证数据。以防万一:)