-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added firestore key validators
- Loading branch information
Showing
8 changed files
with
170 additions
and
12 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
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,2 +1,3 @@ | ||
export * from './model.loader'; | ||
export * from './model.param'; | ||
export * from './model.validator'; |
52 changes: 52 additions & 0 deletions
52
packages/firebase/src/lib/common/model/model/model.validator.spec.ts
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,52 @@ | ||
import { Expose } from 'class-transformer'; | ||
import { IsOptional, validate } from 'class-validator'; | ||
import { FirestoreModelId, FirestoreModelKey } from '../../firestore/collection/collection'; | ||
import { IsFirestoreModelId, IsFirestoreModelKey } from './model.validator'; | ||
|
||
class TestModelClass { | ||
@Expose() | ||
@IsOptional() | ||
@IsFirestoreModelId() | ||
id!: FirestoreModelId; | ||
|
||
@Expose() | ||
@IsOptional() | ||
@IsFirestoreModelKey() | ||
key!: FirestoreModelKey; | ||
} | ||
|
||
describe('IsFirestoreModelKey', () => { | ||
it('should pass valid keys', async () => { | ||
const instance = new TestModelClass(); | ||
instance.key = 'valid/key'; | ||
|
||
const result = await validate(instance); | ||
expect(result.length).toBe(0); | ||
}); | ||
|
||
it('should fail on invalid keys', async () => { | ||
const instance = new TestModelClass(); | ||
instance.key = 'invalid'; | ||
|
||
const result = await validate(instance); | ||
expect(result.length).toBe(1); | ||
}); | ||
}); | ||
|
||
describe('IsFirestoreModelId', () => { | ||
it('should pass valid ids', async () => { | ||
const instance = new TestModelClass(); | ||
instance.id = 'validid'; | ||
|
||
const result = await validate(instance); | ||
expect(result.length).toBe(0); | ||
}); | ||
|
||
it('should fail on invalid ids', async () => { | ||
const instance = new TestModelClass(); | ||
instance.id = 'invalid/id'; | ||
|
||
const result = await validate(instance); | ||
expect(result.length).toBe(1); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
packages/firebase/src/lib/common/model/model/model.validator.ts
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,42 @@ | ||
import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments, ValidationOptions, registerDecorator } from 'class-validator'; | ||
import { isFirestoreModelId, isFirestoreModelKey } from '../../firestore/collection/collection'; | ||
|
||
/** | ||
* isFirestoreModelKey validator | ||
*/ | ||
export function IsFirestoreModelKey(validationOptions?: ValidationOptions) { | ||
return function (object: Object, propertyName: string) { | ||
registerDecorator({ | ||
name: 'isFirestoreModelKey', | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
options: validationOptions, | ||
validator: { | ||
validate: isFirestoreModelKey, | ||
defaultMessage(args: ValidationArguments) { | ||
return `"${args.value}" is not a FirestoreModelKey.`; | ||
} | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
/** | ||
* isFirestoreModelId validator | ||
*/ | ||
export function IsFirestoreModelId(validationOptions?: ValidationOptions) { | ||
return function (object: Object, propertyName: string) { | ||
registerDecorator({ | ||
name: 'isFirestoreModelId', | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
options: validationOptions, | ||
validator: { | ||
validate: isFirestoreModelId, | ||
defaultMessage(args: ValidationArguments) { | ||
return `"${args.value}" is not a FirestoreModelId.`; | ||
} | ||
} | ||
}); | ||
}; | ||
} |
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