YUP 在可能存在零个或多个属性的嵌套对象上的验证

huangapple go评论112阅读模式
英文:

YUP validation on nested object with possible no or more properties

问题

  1. 我试图在这个对象上进行yup验证:
  2. placements: {
  3. 3: {},
  4. 5: {},
  5. 6: {0: 'D17'},
  6. 7: {},
  7. 8: {},
  8. 9: {},
  9. 10: {},
  10. 11: {},
  11. }
  12. 问题是,例如3: {}可以为空,这是完全可以的!
  13. 但是,如果它有一个键和值,它应该检查字符串不为空/长度> 0
  14. 我尝试了很多可能的解决方案,但没有任何有效的。
  15. 该对象用于保存从leaflet地图中的放置,如果嵌套对象为空,那没问题,但如果嵌套对象中有一个键,如6: {0: 'D17'},则值应该大于0,因为它将初始化为6: {0: ''}或6:{0: '', 1: '', 2: ''}。我们不知道有多少键和值。
  16. 这是我现在的代码,但根本不起作用:
  17. placements: Yup.object()
  18. .required("必须选择位置")
  19. .test("is-valid", "必须选择至少一个位置", (placements) =>
  20. Object.values(placements).some((placement: { number: string }) => {
  21. return Object.values(placement).some((p) => p !== "");
  22. })
  23. ),
英文:

I'm trying to make a yup validation on this object:

  1. placements: {
  2. 3: {},
  3. 5: {},
  4. 6: {0: 'D17'},
  5. 7: {},
  6. 8: {},
  7. 9: {},
  8. 10: {},
  9. 11: {},
  10. }

The problem is that for example 3: {} could be empty and that is perfectly fine!
But IF it has a kay&value is should check that the string is not empty/length > 0

I have tried so many possible solutions without anything that works.

The object is for saving placements from a leaflet map, if the nested object is empty that is OK, but if the nested object has a key like in 6: {0: 'D17'} the value should be longer than 0 because it will be initialized as 6: {0: ''} or 6:{0: '', 1: '', 2: ''}. We dont know how many key&values it has.

This is the code i have now and its not working at all..

  1. placements: Yup.object()
  2. .required("Plassering må velges")
  3. .test("is-valid", "Minst en plassering må velges", (placements) =>
  4. Object.values(placements).some((placement: { number: string }) => {
  5. return Object.values(placement).some((p) => p !== "");
  6. })
  7. ),

答案1

得分: 1

问题是some在空数组上始终返回false

  1. console.log([].some(() => true));

因此,您必须添加另一个使用||(或)的检查:

  1. placements: Yup.object()
  2. .required("Plassering må velges")
  3. .test("is-valid", "Minst en plassering må velges", (placements) =>
  4. Object.keys(placements).length === 0 || Object.values(placements).some((placement: { number: string }) => {
  5. return Object.values(placement).some((p) => p !== "");
  6. })
  7. ),

其中检查应该是一种检查对象是否为空的方式。在这里,我使用Object.keys并检查长度是否为0(表示没有键)。

英文:

The problem is that some always returns false on an empty array:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. console.log([].some(() =&gt; true));

<!-- end snippet -->

So you'll have to add another check with || (OR):

  1. placements: Yup.object()
  2. .required(&quot;Plassering m&#229; velges&quot;)
  3. .test(&quot;is-valid&quot;, &quot;Minst en plassering m&#229; velges&quot;, (placements) =&gt;
  4. Object.keys(placements).length === 0 || Object.values(placements).some((placement: { number: string }) =&gt; {
  5. return Object.values(placement).some((p) =&gt; p !== &quot;&quot;);
  6. })
  7. ),

where the check should be some way of checking that the object is empty. Here I'm using Object.keys and checking if the length is 0 (meaning there are no keys).

huangapple
  • 本文由 发表于 2023年3月3日 18:01:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定