Unique field validation
See original GitHub issuerefers to https://github.com/adrai/node-cqrs-domain/issues/70, https://github.com/adrai/node-cqrs-domain/issues/91
Hi, I have the similar question: how to make checks for uniqueness in domain? I tried to check it using eventDenormalizer by saving unique values (emails) into VM by listen domain events and then check it in preCondition:
import * as domain from "cqrs-domain";
export = domain.definePreCondition({
// @ts-ignore
name: ['createUser', 'updateUser'],
priority: 1,
payload: 'payload',
}, (data, aggregate, callback) => {
const usersRepo = require('../viewBuilders/user/usersCollection');
usersRepo.findViewModels({email: data.email}, (err, items) => {
if(items.length){
callback("Provided email is already exist in our database!");
} else {
callback(null);
}
});
});
but this way is not atomic, theoretically there can be situation when 2 users can create their accounts with the same emails. How to avoid this?
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Using the unique validation rule in a Laravel Form Request
Using the unique validation rule in Laravel to ensure that a field is unique in the database, such as a post title.
Read more >Unique content field validation | Drupal.org
This module allows you to require that the content supplied for entity fields, node titles or taxonomy terms names will unique if so ......
Read more >unique=True - Django Built-in Field Validation - GeeksforGeeks
unique =True sets the field to be unique i.e. once entered a value in a field, the same value can not be entered...
Read more >Laravel update model with unique validation rule for attribute
$validator = Validator::make($input, User::rules($id));. Validation on update, with some additional rules: $extend_rules = [ 'password' => 'required ...
Read more >How to validate a field as a unique field? - Everest Forms ...
Go to the Field Options of the desired form field. Look out for the 'Validate as unique' option and enable it. You can...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Having what @adrai said in mind, there are a few patterns you can apply to solve your problem, after you have decided that cqrs/es is suitable for youe user/account management:
in your case you can set the aggregate id as the email, and give the command a rule that the aggregate does not exists.
you may take a look on this answer that i gave for similar question - as last resort this is a solution. In your case
Hehe… welcome to the cqrs world… Do not mix domain and eventDenormalizer stuff that way. To solve this you really need 1 aggregate. To make this more efficient when having too much events, there are snapshots.