新しい学生オブジェクトを作成するための「student/create」のような API コマンドがあります。コードは次のようになります。
def student = new Student(firstName: firstName, lastName: lastName, email: email)
if (! student.validate()) {
response.error = "UNKNOWN_ERROR" // in case we can't find anything better
student.errors.allErrors.each { error ->
// set response.error to an appropriate value
println error
}
} else {
student.save()
}
私たちの目標は、検証が失敗したときに「EMAIL_DUPLICATE」や「FIRSTNAME_LENGTH」などの妥当なエラー メッセージを表示することです。そのため、予想される一連のエラーに対して取得したエラーをテストして、そのように応答できるようにしたいと考えています。
ここから得られるものは次のprintln
とおりです。
フィールド 'email' のオブジェクト 'com.example.Student' のフィールド エラー: 拒否された値 [student@example.com]; コード [com.example.Student.email.unique.error.com.example.Student.email,com.example.Student.email.unique.error.email,com.example.Student.email.unique.error.java. lang.String,com.example.Student.email.unique.error,student.email.unique.error.com.example.Student.email,student.email.unique.error.email,student.email.unique.error. java.lang.String,student.email.unique.error,com.example.Student.email.unique.com.example.Student.email,com.example.Student.email.unique.email,com.example.Student. email.unique.java.lang.String,com.example.Student.email.unique,student.email.unique.com.example.Student.email,student.email.unique.email,student.email.unique.java. lang.String,student.email.unique,unique.com.example.Student.email,unique.email,unique.java. lang.String,unique]; 引数[電子メール、クラスcom.example.Student、student@example.com.org]; デフォルト メッセージ [値 [{2}] を持つクラス [{1}] のプロパティ [{0}] は一意である必要があります]
API ユーザーにそれを伝えることができるように、これはメールがデータベースで既に使用されていることをどのように把握できますか?
(明確にするために、「student@example.com の値を持つクラス Student のプロパティの電子メールは一意である必要があります」のようなものではなく、「EMAIL_DUPLICATE」のようなコンピューターで読み取り可能なメッセージを提供したいと考えています)