question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unique field validation

See original GitHub issue

refers 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:open
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
nanovcommented, Feb 10, 2020

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

1reaction
adraicommented, Feb 10, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found