如何解决使用 Laravel 灯塔验证用户密码确认输入的正确方法是什么
我正在使用灯塔库为我的 Laravel 项目开发 GraphQL 架构。但是我在尝试验证用户确认他们的密码时遇到了问题。当我尝试注册用户时会出现此问题。为我的用户考虑以下类型
describe("mysterious flatMap",() => {
it("an array will be mapped to an array of `undefined` when its element NOT set",() => {
const mapped = new Array(1).map(() => "x");
expect(mapped.length).toBe(1); //OK
expect(mapped[0]).toBe(undefined); // OK
});
it("flatMap can join array of `undefined`",() => {
const flat = [undefined].flatMap((_) => [undefined]);
expect(flat.length).toBe(1); // OK
expect(flat[0]).toBe(undefined); //OK
});
it("but flatMap can NOT join the array which is created without elements set,why ??",() => {
const flat = [undefined].flatMap((_) => new Array(1).map(() => "x"));
expect(flat.length).toBe(1); // Error,Received: 0
expect(flat[0]).toBe("x");
});
});
我尝试使用以下突变注册我的用户
type User {
id: ID
first_name: String!
last_name: String!
email: String!
password: String!
phone_number: String
avatar: String
email_verified_at: DateTime
}
我的 CreateUser 输入看起来像这样
extend type Mutation {
createUser(input: CreateUser @spread): User! @create
}
我收到验证失败的预期错误:“字段 [createUser] 验证失败”,所以没问题。但是,当我尝试在请求中添加 input CreateUser {
first_name: String!
last_name: String!
email: String! @rules(apply: ["email"])
password: String! @rules(apply: ["confirmed"])
phone_number: String
}
时,遇到以下错误:password_confirmation
。那是我认为我应该将 "Field \"password_confirmation\" is not defined by type CreateUser."
字段添加到 CreateUser 输入的时候。但是当我尝试验证通过但我收到一个数据库错误 password_confirmation
是一个未定义的列。这是有道理的,因为它不在我的迁移中,而且我认为不应该要求它能够验证密码。
tldr;如何在 lighthouse @rules 指令中使用 laravel password_confirmation
验证
解决方法
可以覆盖 fill
模型的 User
函数并手动取消设置 password_confirmation
属性:
public function fill(array $attributes)
{
unset($attributes['password_confirmation']);
return parent::fill($attributes);
}
在保存模型之前,将运行验证并取消设置 password_confirmation
字段。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。