Browse Source

Merge pull request #16379 from abpframework/create-unique-char-validator

Create unique char validator
pull/16417/head
Masum ULU 4 years ago
committed by GitHub
parent
commit
036be4528f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      npm/ng-packs/packages/core/src/lib/services/config-state.service.ts
  2. 22
      npm/ng-packs/packages/core/src/lib/tests/validators.spec.ts
  3. 6
      npm/ng-packs/packages/core/src/lib/validators/index.ts
  4. 20
      npm/ng-packs/packages/core/src/lib/validators/unique-character.validator.ts

12
npm/ng-packs/packages/core/src/lib/services/config-state.service.ts

@ -14,18 +14,16 @@ import { InternalStore } from '../utils/internal-store-utils';
providedIn: 'root',
})
export class ConfigStateService {
private updateSubject = new Subject<void>();
private readonly store = new InternalStore({} as ApplicationConfigurationDto);
setState(config: ApplicationConfigurationDto){
this.store.set(config)
setState(config: ApplicationConfigurationDto) {
this.store.set(config);
}
get createOnUpdateStream() {
return this.store.sliceUpdate;
}
private updateSubject = new Subject<void>();
constructor(
private abpConfigService: AbpApplicationConfigurationService,
private abpApplicationLocalizationService: AbpApplicationLocalizationService,
@ -228,4 +226,4 @@ function splitKeys(keys: string[] | string): string[] {
}
return keys;
}
}

22
npm/ng-packs/packages/core/src/lib/tests/validators.spec.ts

@ -5,6 +5,7 @@ import { validateCreditCard } from '../validators/credit-card.validator';
import { validateRequired } from '../validators/required.validator';
import { validateStringLength } from '../validators/string-length.validator';
import { validateUrl } from '../validators/url.validator';
import { validateUniqueCharacter } from '../validators';
const emailAddress = () => Validators.email;
@ -181,4 +182,25 @@ describe('Validators', () => {
expect(control.errors).toEqual(expected);
});
});
describe('Unique Character Validator', () => {
const error = { uniqueCharacter: true };
test.each`
input | expected
${undefined} | ${null}
${null} | ${null}
${''} | ${null}
${'password'} | ${error}
${'123456789'} | ${null}
${'1q2w3E*'} | ${null}
${'password123'} | ${error}
`('should return $expected when input is $input', ({ input, expected }) => {
const control = new FormControl(input, [validateUniqueCharacter()]);
control.markAsDirty({ onlySelf: true });
control.updateValueAndValidity({ onlySelf: true, emitEvent: false });
expect(control.errors).toEqual(expected);
});
});
});

6
npm/ng-packs/packages/core/src/lib/validators/index.ts

@ -4,15 +4,18 @@ import { validateCreditCard } from './credit-card.validator';
import { validateRange } from './range.validator';
import { validateRequired } from './required.validator';
import { validateStringLength } from './string-length.validator';
import { validateUniqueCharacter } from './unique-character.validator';
import { validateUrl } from './url.validator';
import { validateUsername } from './username.validator';
export { validateUniqueCharacter } from './unique-character.validator';
export * from './age.validator';
export * from './credit-card.validator';
export * from './range.validator';
export * from './required.validator';
export * from './string-length.validator';
export * from './url.validator';
export * from './unique-character.validator';
export const AbpValidators = {
creditCard: validateCreditCard,
emailAddress: () => Validators.email,
@ -22,4 +25,5 @@ export const AbpValidators = {
stringLength: validateStringLength,
url: validateUrl,
username: validateUsername,
uniqueCharacter: validateUniqueCharacter,
};

20
npm/ng-packs/packages/core/src/lib/validators/unique-character.validator.ts

@ -0,0 +1,20 @@
import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';
import { isNullOrEmpty } from '../utils';
export interface UniqueCharacterError {
uniqueCharacter: true;
}
export function validateUniqueCharacter(): ValidatorFn {
return (control: AbstractControl): UniqueCharacterError | null => {
if (isNullOrEmpty(control.value)) return null;
return isUnqiueCharacter(control.value) ? null : { uniqueCharacter: true };
};
}
function isUnqiueCharacter(value: string): boolean {
const set = new Set<string>(value.split(''));
return set.size == value.length;
}
Loading…
Cancel
Save