Skip to content

Validation rules #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/app/core/interfaces/session.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export interface SessionState {
formErrors: IValidationError | null;
}

export type IValidationError = {
message: string;
errors: {
[key: string]: string[];
};
status_code: number;
};
};
3 changes: 2 additions & 1 deletion src/app/core/store/app.store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { routerReducer, RouterState } from '@ngrx/router-store';
import { Action, ActionReducerMap } from '@ngrx/store';

import { sessionFeatureKey, sessionReducer, SessionState } from './session/session.reducer';
import { SessionState } from '../interfaces/session.interface';
import { sessionFeatureKey, sessionReducer } from './session/session.reducer';

export interface AppState {
router: RouterState;
Expand Down
6 changes: 1 addition & 5 deletions src/app/core/store/session/session.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { createReducer, on } from '@ngrx/store';

import { IValidationError } from '@app/core/interfaces/session.interface';
import { SessionState } from '@app/core/interfaces/session.interface';
import * as SessionActions from './session.actions';

export interface SessionState {
formErrors: IValidationError | null;
}

export const sessionFeatureKey = 'session';

export const sessionState: SessionState = {
Expand Down
3 changes: 2 additions & 1 deletion src/app/core/store/session/session.selectors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createFeatureSelector, createSelector } from '@ngrx/store';

import { sessionFeatureKey, SessionState } from './session.reducer';
import { SessionState } from '@app/core/interfaces/session.interface';
import { sessionFeatureKey } from './session.reducer';

export const sessionFeatureSelector =
createFeatureSelector<SessionState>(sessionFeatureKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Store } from '@ngrx/store';
import { map, Observable } from 'rxjs';

import { PasswordRules } from '@app/shared/rules/password.rule';
import {
selectError,
selectLoading,
selectMessage,
selectResetPasswordToken,
} from '../../store/auth.selectors';
import { resetPasswordAction } from '../../store/auth.actions';
import { PasswordRules } from '../../rules/password.rules';

@Component({
templateUrl: './reset-password.component.html',
Expand All @@ -29,11 +29,8 @@ export class ResetPasswordComponent {
);

public error$: Observable<string | null> = this.store.select(selectError);

public message$: Observable<string | null> = this.store.select(selectMessage);

public loading$: Observable<boolean> = this.store.select(selectLoading);

public token$: Observable<string | null> = this.store.select(
selectResetPasswordToken
);
Expand Down
14 changes: 7 additions & 7 deletions src/app/shared/interfaces/response.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export type IFilterParams = {

export interface Pagination {
total: number;
per_page: number;
current_page: number;
next_page: string | null;
prev_page: string | null;
first_page: string | null;
last_page: string | null;
perPage: number;
currentPage: number;
nextPage: string | null;
prevPage: string | null;
firstPage: string | null;
lastPage: string | null;
from: number;
to: number;
total_pages: number;
totalPages: number;
}
4 changes: 3 additions & 1 deletion src/app/shared/interfaces/values.interface.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Pagination } from './response.interface';

/**
* This pagination Object is the default structure of
* a Custom Laravel Eloquent Pagination Response
*
* @see https://laravel.com/docs/eloquent-resources#pagination
*/
export const pagination = {
export const pagination: Pagination = {
total: 0,
perPage: 0,
currentPage: 0,
Expand Down
12 changes: 12 additions & 0 deletions src/app/shared/rules/whitespace.rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AbstractControl, ValidatorFn } from '@angular/forms';

export class WhiteSpaceRule {
static noWhitespace(controlName: string): ValidatorFn {
return (controls: AbstractControl) => {
const control = controls.get(controlName);
const isWhitespace = (control?.value || '').trim().length === 0;
const isValid = !isWhitespace;
return isValid ? null : { whitespace: true };
};
}
}