-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
139 additions
and
16 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
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,9 +1,10 @@ | ||
import 'reflect-metadata'; | ||
|
||
export * from './follow'; | ||
export * from './href'; | ||
export * from './models'; | ||
export * from './parse'; | ||
export * from './resolve'; | ||
export * from './serialize'; | ||
export * from './submit'; | ||
export * from './href'; | ||
export * from './validate'; |
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 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,3 @@ | ||
export * from './validation-error'; | ||
export * from './validation-result'; | ||
export * from './validator'; |
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,16 @@ | ||
import 'reflect-metadata'; | ||
|
||
import { Field } from '../models'; | ||
import { noOpValidator } from './no-op-validator'; | ||
import { PositiveValidationResult } from './validation-result'; | ||
|
||
describe('noOpValidator', () => { | ||
it('should always return a positive result', () => { | ||
const invalidField = new Field(); | ||
invalidField.type = 'number'; | ||
invalidField.value = 'NaN'; | ||
expect(noOpValidator([invalidField])).toBeInstanceOf(PositiveValidationResult); | ||
expect(noOpValidator([new Field()])).toBeInstanceOf(PositiveValidationResult); | ||
expect(noOpValidator([])).toBeInstanceOf(PositiveValidationResult); | ||
}); | ||
}); |
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,4 @@ | ||
import { PositiveValidationResult } from './validation-result'; | ||
import { Validator } from './validator'; | ||
|
||
export const noOpValidator: Validator = () => new PositiveValidationResult(); |
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,10 @@ | ||
import { NegativeValidationResult } from './validation-result'; | ||
|
||
/** | ||
* Wraps a {@linkcode NegativeValidationResult} as an `Error` | ||
*/ | ||
export class ValidationError extends Error { | ||
constructor(readonly result: NegativeValidationResult) { | ||
super('Validation failed'); | ||
} | ||
} |
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,19 @@ | ||
/** Base class for representing the result of performing validation */ | ||
export abstract class ValidationResult {} | ||
|
||
/** Represents successful validation */ | ||
export class PositiveValidationResult implements ValidationResult {} | ||
|
||
/** | ||
* Represents failed validation. Inherit from this class for a more detailed | ||
* result. For example: | ||
* | ||
* ```js | ||
* class GoofedValidation extends NegativeValidationResult { | ||
* constructor(readonly invalidFields: Field[]) { | ||
* super(); | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
export class NegativeValidationResult implements ValidationResult {} |
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,7 @@ | ||
import { Field } from '../models'; | ||
import { ValidationResult } from './validation-result'; | ||
|
||
/** | ||
* @returns a {@linkcode ValidationResult} indicating the validity of the given `fields` | ||
*/ | ||
export type Validator = (fields: Field[]) => ValidationResult; |