在开发期间,我想为使用schema.core/defn定义的所有函数启用验证,而不是必须使用它们进行注释:^:always-validate.这个库有可能吗?
这样的东西不起作用,可能是因为元数据只在编译时添加:
(def dev false)
;; example of schema validation
(sm/defn ^{:always-validate dev}
add :- s/Num
[a :- s/Num b :- s/Num]
(+ a b))
Run Code Online (Sandbox Code Playgroud) 什么时候应该在 prisma 中使用隐式多对多关系,什么时候应该使用显式多对多关系?
他们有什么权衡或者需要注意的地方吗
使用compojure-api创建API时,使用body-params和:form-params有什么区别?例如:
(POST* "/register" []
:body-params [username :- String,
password :- String]
(ok)))
Run Code Online (Sandbox Code Playgroud)
VS
(POST* "/register" []
:form-params [username :- String,
password :- String]
(ok)))
Run Code Online (Sandbox Code Playgroud)
哪一个应该用于单页clojurescript应用程序和/或本机移动应用程序将使用的API?
当我有这样的API定义时:
(POST* "/register" []
:body-params [username :- String,
password :- String,
name :- String]
(ok)))
Run Code Online (Sandbox Code Playgroud)
什么是使名称可选的合适方式?是吗:
(POST* "/register" []
:body-params [username :- String,
password :- String,
{name :- String nil}]
(ok)))
Run Code Online (Sandbox Code Playgroud) 我按照Prisma 提供的GraphQL Prisma Typescript示例创建了一个简单的数据模型,为 Prisma 客户端和解析器生成了代码等。
我的数据模型包括以下节点:
type User {
id: ID! @unique
displayName: String!
}
type SystemUserLogin {
id: ID! @unique
username: String! @unique
passwordEnvironmentVariable: String!
user: User!
}
Run Code Online (Sandbox Code Playgroud)
我已经播种了系统用户和用户。
mutation {
systemUserLogin: createSystemUserLogin({
data: {
username: "SYSTEM",
passwordEnvironmentVariable: "SYSTEM_PASSWORD",
user: {
create: {
displayName: "System User"
}
}
}
})
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个示例突变login:
login: async (_parent, { username, password }, ctx) => {
let user
const systemUser = await ctx.db.systemUserLogin({ username })
const valid = …Run Code Online (Sandbox Code Playgroud) 在我的Clojure项目中,我有这些:
:dependencies [
[org.clojure/clojure "1.8.0"]
[prismatic/schema "1.0.5"]]
Run Code Online (Sandbox Code Playgroud)
这被认为是有效的:
(require '[schema.core :as s])
(def pos (s/pred #(re-matches #"\d+,\d+" %)))
(s/validate pos "0,0")
; "0,0"
Run Code Online (Sandbox Code Playgroud)
基于此,我认为以下内容也是有效的(但事实并非如此):
(require '[schema.core :as s])
(def pos (s/pred #(re-matches #"\d+,\d+" %)))
(def structure {(s/optional-key pos) s/Any})
(s/validate structure {"0,0" true, "2,0" false})
; Value does not match schema: {"0,0" disallowed-key, "2,0" disallowed-key}
Run Code Online (Sandbox Code Playgroud) 不久之前,我问过简单的多态模式,答案一段时间内效果很好.
现在,我希望验证的地图有一个附加值,该值取决于另一个键的值.
一个人为的例子:
{:type :foo {:type :bert {:type :foo
:foo-param :bar :bert-size :medium :foo-param :bar
:method :baz :method :baz :method :bang
:baz-rate :max} :baz-rate :max} :bangness :considerable}
Run Code Online (Sandbox Code Playgroud)
这里的鉴别符是:type和:method,每个都有自己的一组有效的兄弟键和值.
以前只:type存在,以下工作:
(def ^:private test-base-schema {:type (s/enum :foo :abc :banana)})
(def test-schema
(s/conditional #(= (:type %) :foo)
(merge test-base-schema {:foo-param s/Keyword})
; other conditions here
))
Run Code Online (Sandbox Code Playgroud)
然而,现在有多个鉴别器,条件分支的数量将是组合的.
一种选择是允许{s/Any s/Any}在地图中使用s/both,但我不能允许模式"松散",因为意外的键/值应该被视为无效.
我也不想改变被验证的地图的结构,只是为了允许验证使用这个库工作.
是否有一种理智的方法来实现对具有多个条件子模式的映射的严格验证?
关于使用Prismatic/schema来验证函数,我有一个非常简单的问题.我有一个具有单个键的映射的模式,其值是一个函数,它将Bar模式作为其单个参数并返回任何内容(用于副作用):
(require '[schema.core :as s])
(def Bar {:baz s/Int})
(def Action :???)
(def Foo {:action Action})
Run Code Online (Sandbox Code Playgroud)
问题是,我该如何定义Action?我试过这个:
(require '[schema.macros :as sm])
(def Action (sm/=> s/Any Bar))
Run Code Online (Sandbox Code Playgroud)
这看起来很有希望,但我不能让它失败验证:
(s/explain Action)
;=> (=> Any {:baz Int})
;; This should fail
(s/validate Foo {:action :anything-goes})
;=> {:action :anything-goes}
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
我在core_test中阅读了文档和测试,但我无法弄清楚如何做到这一点.
在compojure-api中,我注意到这两种指定资源API的方法:
(POST* "/register" []
:body [user UserRegistration]
(ok)))
Run Code Online (Sandbox Code Playgroud)
和
(POST* "/register" []
:body-params [username :- String,
password :- String]
(ok)))
Run Code Online (Sandbox Code Playgroud)
这两者有什么区别?使用一个与另一个有什么含义?
我有类型的传入数据{:loan/amount 1200}.
是否有可能使用plumatic Schema强制执行此操作,即将{:loan/amount 1200M}数字(甚至是数字串)强制转换为大数字?
我不知道如何定义新的数据类型(如s/Bigdec),然后确保它用于clojure.core/bigdec强制某个值为a java.math.BigDecimal.
我有一个由根类别和子类别组成的棱镜数据模型。一个类别有许多子类别,一个子类别属于一个类别。我的模型看起来像这样:
type Category {
id: ID! @unique
createdAt: DateTime!
updatedAt: DateTime!
name: String!
subCategories: [SubCategory!]! @relation(name: "Subcategories")
}
type SubCategory {
id: ID! @unique
createdAt: DateTime!
updatedAt: DateTime!
name: String!
category: Category! @relation(name: "ParentCategory")
cards: [Card!]! @relation(name: "SubCategoryCards") #Category @relation(name: "CardCategory")
}
Run Code Online (Sandbox Code Playgroud)
现在当我去创建一个新的子类别并通过
mutation {
createSubCategory(data:{
name:"This is a test"
category:{
connect:{
id:"cjp4tyy8z01a6093756xxb04i"
}
}
}){
id
category{
name
id
}
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常。下面我查询子类别及其父类别,并得到我期望的结果。
{
subCategories{
id
name
category{
id
name
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试查询一个类别并获取它的所有子类别时,我得到了一个空数组:
{
categories{
id
name
subCategories{ …Run Code Online (Sandbox Code Playgroud) 我一直在寻找使用棱镜/模式在clojure中进行打字和文档编制,但我无法找到它的全面文档.github自述文件提供了一些有用的基本用法信息,但是给我留下了许多未解决的问题(例如,为什么你需要(s/one s/str"s"中的"s"参数).有没有人知道棱镜模式的全面教程或文档来源?
type Category {
id: ID! @id
name: String!
}
type SubCategoryLevel1 {
id: ID! @id
name: String!
parentCategory: Category! @relation(link: INLINE)
}
type SubCategoryLevel2 {
id: ID! @id
name: String!
parentCategory: SubCategoryLevel1! @relation(link: INLINE)
}
Run Code Online (Sandbox Code Playgroud)
如果我的Category水平没有确定怎么办,我正在使用Prisma ORM和MongoDB.
plumatic-schema ×13
clojure ×9
prisma ×4
graphql ×3
swagger ×3
coercion ×1
database ×1
many-to-many ×1
mongodb ×1