how can i ignore username / email / phone validation check for current user when updating?
update: i also used class validation but user is null and cant reach the current user id there
updateUserMutator(input: UpdateUserInput! ): User #guard(with: ["api"])
input UpdateUserInput {
name: String! #rules(apply: ["min:1","max:255", "required"])
lastname: String! #rules(apply: ["min:1", "max:255", "required"])
username: String! #rules(apply: ["min:1","max:12", "unique:users,username", "regex:/^[a-zA-Z0-9]+/"])
email: String! #rules(apply: ["email", "unique:users,email", "required"])
phone: String! #rules(apply: ["required", "unique:users,phone", "regex:/[0-9]{10}/"])
}
used class validation:
final class UpdateUserInputValidator extends Validator
{
public function authorize()
{
return true;
}
/**
* Return the validation rules.
*
* #return array<string, array<mixed>>
*/
public function rules(): array
{
// get user from context
$id = auth()->id();
return [
'name' => ['sometimes', 'string', 'between:2,25'],
'lastname' => ['sometimes', 'string', 'between:2,25'],
'username' => [
'sometimes',
Rule::unique('users', 'username')->ignore($id, 'id'),
],
'email' => [
'sometimes',
'string',
'email',
'max:255',
Rule::unique('users', 'email')->ignore($id, 'id'),
],
'phone' => [
'sometimes',
'regex:/[0-9]{10}/',
Rule::unique('users', 'phone')->ignore($id, 'id'),
],
];
}
}
but user is null there.
There are more options available to do so.
You could create a mutation for every field you want to update. This way you ignore the other fields and just update the field in request. (more work)
You can do it with your defined input but you should remove the ! characters which makes the fields required. The updated input might look like:
input UpdateUserInput {
name: String #rules(apply: ["sometimes", "min:1", "max:255"])
lastname: String #rules(apply: ["sometimes", "min:1", "max:255"])
username: String #rules(apply: ["sometimes", "min:1", "max:12", "unique:users,username", "regex:/^[a-zA-Z0-9]+/"])
email: String #rules(apply: ["sometimes", "email", "unique:users,email"])
phone: String #rules(apply: ["sometimes", "unique:users,phone", "regex:/[0-9]{10}/"])
}
With the second approach you just send the fields you want to change.
You forget to update the upper part
input UpdateUserInput #validator {
name: String
lastname: String
username: String
email: String
phone: String
}
Related
OneToOne case
For one JoinColumn key in TypeORM, the #Extensions below can work with loader and loadkey.
But in this case, there are two JoinColumn keys: postId and version. How to set them into #Extensions?
#OneToOne(() => PostVersion, {
lazy: true,
cascade: true,
createForeignKeyConstraints: false,
})
#JoinColumn([
{ name: 'id', referencedColumnName: 'postId' },
{ name: 'version', referencedColumnName: 'version' },
])
#Field(() => PostVersion, {
middleware: [dataloaderFieldMiddleware],
})
// How to set both postId and version keys below?
#Extensions({
loader: (dataloaders: IDataloaders) => dataloaders.currentPostVersion,
loadKey: (post: Post) => post.id,
})
current: Promise<PostVersion>;
ManyToMany case
For many to many relation, one side can set #Extensions with one key. Is it right to set the other relation key on the other side in #Extensions?
#ManyToMany(() => Type)
#JoinTable({
name: 'types_posts',
joinColumn: {
name: 'post_id',
referencedColumnName: 'id',
},
inverseJoinColumn: {
name: 'type_id',
referencedColumnName: 'id',
},
})
#Field(() => [Type], {
middleware: [dataloaderFieldMiddleware],
})
// How to set many to many relations keys with dataloader here?
#Extensions({
loader: (dataloaders: IDataloaders) => dataloaders.typeByPostId,
loadKey: (post: Post) => post.id,
})
postboards: Promise<Type[]>;
I use Yup for my Formik form validation.
I have the following contact form :
const contactForm =
{
title: undefined,
firstName: '',
lastName: props.contact?.name,
contactGroup: {
phone: props.contact?.phone || '',
email: props.contact?.email || '',
},
};
when I fill in the fields I would like the lastName field to go to error only when one of the other fields is filled in.
Here is the validation scheme I use to handle form errors
const schema = yup
.object()
.shape<any>({
firstName: yup.string().max(256, 'max-length'),
lastName: yup.string().when(['email', 'phone', 'firstName'], {
is: (email: any, phone: any, firstName: any) =>
email && phone && firstName,
then: yup.string().required('empty').max(256, 'max-length'),
}),
contactGroup: yup.object().shape<any>(
{
phone: yup.string().when('email', {
is: (email: any) => !email || email === '',
then: yup.string().max(256, 'max-length'),
}),
email: yup
.string()
.nullable(true)
.when('phone', {
is: (phone: any) => !phone || phone === '',
then: yup.string().email('invalid').max(256, 'max-length'),
}),
},
[
['phone', 'email'],
['email', 'phone'],
]
),
})
.required();
I modified the yup in the following way:
lastName: yup.string().when(['email', 'phone', 'firstName'], {
is: (email: any, phone: any, firstName: any) =>
email && phone && firstName,
then: yup.string().required('empty').max(256, 'max-length'),
}),
here I ask if the fields email, phone and firstName are filled in the field lastName is set in error.
It works if I do it only on firstName, but if I add email and phone it doesn't work anymore.
I think it comes from the fact that phone and email are in a contactGroup subobject
Is there a solution for this problem ?
lastName should only go into error when one of the fields phone, email or firstName is filled in.
I'm trying to update geospatial data in mongodb using laravel version 8, while login i've to update long,lat in geospatial coordinates in mongo database, data how it stored in the database is shown below, here "point" is field name of geospatial coordinates.
point:Object
type:"Point"
coordinates: Array
0:122.00084
1:37.4219983
This is how it stored in database
i'm passing the value in update query as follows
$date = Carbon::now();
$timestamp = $date->getPreciseTimestamp(3);
$updateData = ['user_token' => $token,
'update_at' => $timestamp,
'point' => ['type' => 'Point', 'coordinates' => [$long, $lat]]
];
$q = Post::where('user_id', $userID)->update($updateData);
it throws an error "exception":
"MongoDB\Driver\Exception\BulkWriteException",
{
"message": "Can't extract geo keys: { _id: \"261058c2f2576b4f72365d3ee1d5a03d\", user_id: \"7c2d166e170a6d8457ca16424892b05b\", name: \"qwer\", profile_pic: \"\", email: \"mailid#mail.com\", password: \"e10adc3949b234a59a2134bbe56e0572134f20f883e\", city: \"ind\", state: \"IN\", zip: \"600000\", country: \"India\", user_token: \"asdfadsf\", point: { type: \"Point\", coordinates: [ \"-122.084\", \"37.4219983\" ] }, is_active: true, verifiy_key: \"168d224a36785f00e28e232068qerweadcac8f1fsdfcbeade5f643cbd2e683548fd069200423749b\", email_status: true, is_not",
"exception": "MongoDB\\Driver\\Exception\\BulkWriteException",
"file": "/var/www/html/ce5/vendor/mongodb/mongodb/src/Operation/Update.php",
"line": 200,
}
I am going to preface this with I have NOT done this in a LONG time and my mind is mud. So no picking on me just remind me what I am doing wrong or not remembering.
NOTE: This is VueJS 3 / Tailwind 3 inside a Laravel 9 Jetstream Project
I have a method...
locatorButtonPressed() {
navigator.geolocation.getCurrentPosition(
position => {
console.log(position.coords.latitude);
console.log(position.coords.longitude);
},
error => {
console.log(error.message);
},
)
},
and I have a form data
data() {
return {
form: this.$inertia.form({
name: '',
email: '',
password: '',
password_confirmation: '',
birthdate: '',
user_latitude: '',
user_longitude: '',
user_city: '',
user_region: '',
user_country: '',
location: '',
terms: false,
})
}
}
I want to store position.coords.latitude and position.coords.longitude from the method in the form Data under user_latitude and user_longitude respectfully.
What am I forgetting???
The data properties can be accessed via this.PROPERTY_NAME. For example, to set form.user_latitude, use this.form.user_latitude = newValue.
export default {
methods: {
locatorButtonPressed() {
navigator.geolocation.getCurrentPosition(
position => {
this.form.user_latitude = position.coords.latitude;
this.form.user_longitude = position.coords.longitude;
})
},
}
}
demo
Model.py
class CommentStruct(models.Model):
type = models.CharField(nax_length=100)
value= models.CharField(nax_length=100)
serializer.py
class Meta:
model = CommentStruct
fields = '__all__'
Now I am getting format is
[{ 'type': 'comment', 'value':'hai'},
{ 'type': 'action', 'value':'no action'},
{ 'type': 'comment', 'value':'hallo'},
{ 'type': 'comment', 'value':'hello'},
{ 'type': 'action', 'value':'shake hand'}
]
what I want is
[{ 'comment': ['hai','hallo','hello'], 'action':['no action', 'shake hand']}]
I want to restructure data in the model serializer