我可以将clojure架构输入验证为字符串,如下所示。
email : - s/Email
如何编写电子邮件的defschema以使用
Ext.application({
name: 'Fiddle',
launch: function () {
var MyCheckboxModel = Ext.create('Ext.selection.CheckboxModel', {
mode: 'SIMPLE',
listeners: {
selectionchange: function (selectionModel) {}
},
});
var userCheckboxContainersStore = Ext.create('Ext.data.Store', {
storeId: 'userCheckboxStore',
fields: ['data'],
data: [{
data: 'Item 1'
}, {
data: 'Item 2'
}, {
data: 'Item 3'
}, {
data: 'Item 4'
}, {
data: 'Item 5'
},
]
});
var userCheckboxGridPanel = Ext.create('Ext.grid.Panel', {
layout: {
type: 'vbox',
align: 'stretch'
},
defaults: {
border: 0,
bodyStyle: {
backgroundColor: 'transparent'
}
},
store: userCheckboxContainersStore,
selModel: MyCheckboxModel,
title: 'Item List',
columns: [{
dataIndex: 'data'
}],
listeners: {
render: function (gridPanel) {
gridPanel.setSelection(userCheckboxContainersStore.getAt(0));
gridPanel.on('itemclick', function (panel, selected) {
if (selected.id === userCheckboxContainersStore.getAt(0).id) {
var newSelection = gridPanel.getSelection();
if (newSelection.indexOf(selected) === -1)
newSelection.push(selected);
gridPanel.setSelection(newSelection);
}
});
}
},
renderTo: Ext.getBody()
});
}
});
将转换/强制转换为小写。
答案 0 :(得分:2)
我恰巧有一个......
(ns myapp.utils.schema
(:import [org.apache.commons.validator.routines EmailValidator])
(:require [clojure.string :as str]
[schema.core :as s]
[schema.coerce :as sco]))
(def valid-email? "Checks that the value is a valid email string."
(let [ev (EmailValidator/getInstance)]
(fn [s]
(and
(string? s)
(.isValid ev, ^String s)
))))
(def Email "Email schema type, stricter than schema.core/Str."
(s/pred valid-email?))
(defn sanitize-email [s]
(if (string? s)
(-> s str/lower-case str/trim)
s))
(defn email-matcher [s]
(when (= Email s) sanitize-email))
(def matcher
"The Plumatic matcher used for Plumatic schema coercion in myapp."
(sco/first-matcher [email-matcher,,,]))
(defn coercer
[schema]
(sco/coercer schema matcher))
;; ...
(ns myapp.customer
(:require [schema.core :as s]
[myapp.utils.schema :as sc]))
(def Customer
{:customer/email sc/Email
:customer/name s/Str})
(def coerce-customer (sc/coercer Customer))
(coerce-customer {:customer/email "vAlENTin@gmaIl.com "
:customer/name "Valentin"})
=> {:customer/email "valentin@gmail.com"
:customer/name "Valentin"}