-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c90a18
commit 6dc1a3f
Showing
15 changed files
with
196 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,19 @@ | ||
import { | ||
V, | ||
createFormField, | ||
createFormGroup, | ||
createInjectableSignalForm, | ||
} from '@ng-signal-forms'; | ||
import { signal } from '@angular/core'; | ||
import { createFormField, createFormGroup, createInjectableSignalForm, equalsTo, required } from '@ng-signal-forms'; | ||
|
||
export const { injectSignalForm, provideSignalForm } = | ||
createInjectableSignalForm(() => | ||
createFormGroup(() => ({ | ||
step1: createFormGroup(() => ({ | ||
firstName: createFormField('', { validators: [V.required()] }), | ||
lastName: createFormField('', { validators: [V.required()] }), | ||
firstName: createFormField('', { validators: [required()] }), | ||
lastName: createFormField('', { validators: [required()] }), | ||
})), | ||
step2: createFormGroup(() => ({ | ||
street: createFormField('', { validators: [V.required()] }), | ||
street: createFormField('', { validators: [required()] }), | ||
zip: createFormField<number | undefined>(undefined, { | ||
validators: [V.required(), V.equalsTo(signal(1234))], | ||
validators: [required(), equalsTo(1234)], | ||
}), | ||
city: createFormField('', { validators: [V.required()] }), | ||
country: createFormField('', { validators: [V.required()] }), | ||
city: createFormField('', { validators: [required()] }), | ||
country: createFormField('', { validators: [required()] }), | ||
})), | ||
})) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { isSignal, Signal } from '@angular/core'; | ||
import { SetValidationState, ValidatorFn } from '../validation'; | ||
|
||
export function equalsTo<Value>(otherValue: Value | Signal<Value>): ValidatorFn<Value> { | ||
return (value: Value, setState: SetValidationState) => { | ||
const other = isSignal(otherValue) ? otherValue() : otherValue; | ||
const valid = value == null || value === undefined || value === other; | ||
|
||
if (valid) { | ||
setState('VALID'); | ||
} else { | ||
setState('INVALID', { | ||
equalsTo: { | ||
details: { | ||
otherValue: other, | ||
currentValue: value | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export * from './equals-to'; | ||
export * from './length'; | ||
export * from './max'; | ||
export * from './max-length'; | ||
export * from './min'; | ||
export * from './min-length'; | ||
export * from './pattern'; | ||
export * from './required'; | ||
export * from './required-true'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { SetValidationState, ValidatorFn } from '../validation'; | ||
|
||
export function length(length: number): ValidatorFn<string | Array<unknown>> { | ||
return (value: string | Array<unknown>, setState: SetValidationState) => { | ||
const valid = | ||
value === null || value === undefined || value.length === length; | ||
|
||
if (valid) { | ||
setState('VALID'); | ||
} else { | ||
setState('INVALID', { | ||
length: { | ||
details: { | ||
currentLength: value.length, | ||
length: length | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { SetValidationState, ValidatorFn } from '../validation'; | ||
|
||
export function maxLength(length: number): ValidatorFn<string | Array<unknown>> { | ||
return (value: string | Array<unknown>, setState: SetValidationState) => { | ||
const valid = | ||
value === null || value === undefined || value.length <= length; | ||
|
||
if (valid) { | ||
setState('VALID'); | ||
} else { | ||
setState('INVALID', { | ||
minLength: { | ||
details: { | ||
currentLength: value.length, | ||
maxLength: length | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { SetValidationState, ValidatorFn } from '../validation'; | ||
|
||
export function max(maxValue: number): ValidatorFn<string | Array<unknown>> { | ||
return (value: number | unknown, setState: SetValidationState) => { | ||
const valid = | ||
value === null || value === undefined || typeof value !== 'number' || value <= maxValue; | ||
|
||
if (valid) { | ||
setState('VALID'); | ||
} else { | ||
setState('INVALID', { | ||
max: { | ||
details: { | ||
current: value, | ||
max: maxValue | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { SetValidationState, ValidatorFn } from '../validation'; | ||
|
||
export function minLength(length: number): ValidatorFn<string | Array<unknown>> { | ||
return (value: string | Array<unknown>, setState: SetValidationState) => { | ||
const valid = | ||
value === null || value === undefined || value.length >= length; | ||
|
||
if (valid) { | ||
setState('VALID'); | ||
} else { | ||
setState('INVALID', { | ||
minLength: { | ||
details: { | ||
currentLength: value.length, | ||
minLength: length | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { SetValidationState, ValidatorFn } from '../validation'; | ||
|
||
export function min(minValue: number): ValidatorFn<string | Array<unknown>> { | ||
return (value: number | unknown, setState: SetValidationState) => { | ||
const valid = | ||
value === null || value === undefined || typeof value !== 'number' || value >= minValue; | ||
|
||
if (valid) { | ||
setState('VALID'); | ||
} else { | ||
setState('INVALID', { | ||
min: { | ||
details: { | ||
current: value, | ||
min: minValue | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { SetValidationState, ValidatorFn } from '../validation'; | ||
|
||
export function pattern(regexp: RegExp): ValidatorFn { | ||
return (value: unknown, setState: SetValidationState) => { | ||
const valid = value === null || value === undefined || typeof value !== 'string' || regexp.test(value); | ||
if (valid) { | ||
setState('VALID'); | ||
} else { | ||
setState('INVALID', { pattern: { details: { pattern: regexp, currentValue: value } } }); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { SetValidationState, ValidatorFn } from '../validation'; | ||
|
||
export function requiredTrue(): ValidatorFn { | ||
return (value: unknown, setState: SetValidationState) => { | ||
const valid = value === true; | ||
if (valid) { | ||
setState('VALID'); | ||
} else { | ||
setState('INVALID', { requiredTrue: { details: true } }); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { SetValidationState, ValidatorFn } from '../validation'; | ||
|
||
export function required(): ValidatorFn { | ||
return (value: unknown, setState: SetValidationState) => { | ||
const valid = value !== null && value !== undefined && value !== ''; | ||
if (valid) { | ||
setState('VALID'); | ||
} else { | ||
setState('INVALID', { required: { details: true } }); | ||
} | ||
}; | ||
} |