问题标签 [clojure.spec]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
652 浏览

clojure - How do I spec higher order function arguments in Clojure?

Let's say I have a function that takes a function and returns a function that applies any arguments it is given to the passed in function and puts the result in a vector (it's a noddy example, but will hopefully illustrate my point).

I think the spec for the box function looks like this

If I then instrument the box function

and call box with clojure.core/+ I get an exception

If I understand the error correctly then it's taking the any? predicate and generating a PersistentVector for the test, which clojure.core/+ obviously can't use. This means I can get it to work by changing box's argument function spec to be

but what if I want to use box for both clojure.core/+ and clojure.string/lower-case?

N.B. To get spec to work in the REPL I need

in project.clj and the following imports

0 投票
3 回答
2220 浏览

clojure - 使用 clojure.spec 进行强制惯用吗?

我已经看到在各种要点中使用 clojure 构象器来强制数据,但也有一个印象(我不记得在哪里)强制(例如如下)不是构象器的惯用用法。

以上是否是单调的/无意的?如果是这样,什么是合适的/惯用的用法。预期用途的边界在哪里?

0 投票
2 回答
817 浏览

clojure - What does retag parameter in s/multi-spec mean?

Can you explain with examples how does retag parameter impacts multi-spec creation? I find multi-spec documentation hard to digest.

0 投票
1 回答
594 浏览

clojure - 如何检查 Clojure 规范的可解析性?

clojure.spec.alpha允许在定义新规范时使用不可解析的规范:

在这里,:foo/foo无法解决,因此 using:foo/bar将引发使用异常:

这是我的代码中发生的事情,当我输入诸如:my-ns/my-spec而不是::my-ns/my-spec. 我想用单元测试来捕捉那些。

深入研究clojure.spec.alpha源代码,我发现我可以获得所有规格,(keys (s/registry))因此我的测试如下所示:

不幸的是,没有像s/resolvable?in这样的东西clojure.spec.alpha。到目前为止我发现的唯一解决方案是调用(s/valid? spec 42)并假设它没有引发异常意味着它是可解决的,但它不会检查所有分支:

我检查了异常堆栈跟踪以及源代码,以查看是否可以找到任何函数来完全解析规范,而无需使用类似42:foo以上的测试值,但找不到任何函数。

有没有办法检查,对于给定的规范,它在其所有分支中引用的所有规范确实存在?

0 投票
1 回答
988 浏览

clojure - 如何检查 spec/coll-of 中的不同 id

上面的规范要求每个用户映射是不同的,但我如何指定它来:user/ids仅检查不同的

不应允许以下集合:

0 投票
1 回答
241 浏览

clojure - 如何设置 clojureScript 项目以使用规范并在运行时测试 clojure.core 函数?

Clojure 1.9 引入了规范。clojure.core 库中的函数现在具有规范。如何设置 clojurescript 项目以使用规范并在运行时测试 clojure.core 函数?

我使用了库[org.clojure/test.check "0.10.0-alpha2"][org.clojure/spec.alpha "0.1.123"]安装specs和命令instrument。它可以检测我编写规范的函数中的问题。但它没有检测到clojure.core(例如,map)的问题。

也许规范还不能与 clojurescript 一起使用。

0 投票
1 回答
2563 浏览

clojure - Clojure Spec vs Typed vs Schema

在我的 Clojure 项目中,我使用的是 Clojure Spec 但如果我需要使用像 compojure-api 这样的库,那么我需要使用 Schema。

  • 一个比其他的有什么优势?
  • 为什么我会考虑其中一个而不是其他?
  • 哪一个适合编译类型检查?
0 投票
1 回答
1188 浏览

clojure - 如何制作自定义 clojure.spec 生成器?

我正在尝试指定以下名为 Connection 的数据结构:

{:id "some string" :channel "instance of org.httpkit.server.AsyncChannel" }

这是我的规格:

我收到以下错误,我不知道这里出了什么问题:

0 投票
1 回答
552 浏览

clojure - Clojure 规范错误消息:文件名?电话号码?的意思:在?

很抱歉问了这么一个基本的问题。希望答案不明显的事实主要是由于 clojure.spec 仍然是 alpha 版本(0.1.134)。

如何在此规范错误消息中找到违规代码的文件名和行号?关键是什么:in,sc。价值[2 1]

输入时出现此错误lein repl。我对违规行的猜测在这里:

因为当我从我在 Clojure 1.8 下运行的目录中复制它时,首先出现了错误消息。此代码在 Clojure 1.8 下工作。

如果我没有这个方便的线索,我想知道如何追踪有问题的文件和行。即使有这个线索,我也不知道该怎么办。我不知道错误是在我的代码、clojure.core/ns 代码还是 clojure.core/ns 中的规范中。还有,In: [2 1]指的是什么?


更新我发现了错误。我上面的猜测是错误的。该错误甚至不在我的代码中!它在 clojure.core.async 中。我使用的是旧版本:0.2.374。我更改为当前版本 0.3.443,并结束了错误消息。显然,在 Clojure 1.9 之前编写的许多代码都利用了ns宏中的一些松懈,比如省略了前面的冒号:require和其他关键字,所以这将是一个常见问题,直到库更新并且依赖项更新以指向更新的库。一个类似的错误出现在popen中(已经修复——这是快速服务!)。

不过,我的问题不是关于这个错误的细节。这是关于一般问题的:如何在不通过像这样的反复试验进行长时间追逐的情况下找到有问题的代码行。

0 投票
1 回答
139 浏览

clojure - Clojure Spec 在分层规范中访问数据

如果您有一组用于验证分层数据集的规范 - 比如说 yaml 文件。从其中一个子规范中,是否可以引用树中较早出现的数据?