-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added a new 'requiredBy' rule to configure field dependencies
- Loading branch information
Showing
5 changed files
with
103 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {RuleConfig} from '../config/RuleConfig'; | ||
import {GenericValidator} from './GenericValidator'; | ||
import {Data} from '../Data'; | ||
import {get} from 'lodash'; | ||
|
||
export const rule = (config: RuleConfig) => | ||
new GenericValidator( | ||
config, | ||
(value: string, config: RuleConfig, data: Data) => { | ||
const isEmpty = (val: string): boolean => !val || val.length === 0; | ||
|
||
const parentValue = get(data, config.parent as string) as string; | ||
return isEmpty(parentValue) || !isEmpty(value); | ||
}, | ||
`'%s' is required by ${config.parent}`, | ||
true | ||
); | ||
|
||
export const NAME = 'REQUIREDBY'; |
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,16 @@ | ||
import {RuleConfig} from '../../config/RuleConfig'; | ||
import * as RequiredByRule from '../RequiredByRule'; | ||
|
||
export class Builder { | ||
private readonly config: RuleConfig; | ||
constructor(config: RuleConfig) { | ||
this.config = config; | ||
} | ||
|
||
public readonly build = () => this.config; | ||
} | ||
|
||
export const builder = { | ||
withParent: (parent: string, errorMessage?: string): Builder => | ||
new Builder({type: RequiredByRule.NAME, parent, errorMessage}), | ||
}; |
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,64 @@ | ||
import {testValidator} from '../utils'; | ||
import {RuleBuilder, Validator, validatorBuilder} from '../../src'; | ||
|
||
const validator: Validator = validatorBuilder() | ||
.newRule() | ||
.withField('webpushVapidPublicKey') | ||
.validate(RuleBuilder.matches('^[A-Za-z0-9_-]*$')) | ||
.withField('webpushVapidPrivateKey') | ||
.validate( | ||
RuleBuilder.isRequiredBy.withParent('webpushVapidPublicKey').build() | ||
) | ||
.validate(RuleBuilder.matches('^[A-Za-z0-9_-]*$')) | ||
.withField('webpushAlias') | ||
.validate( | ||
RuleBuilder.required() | ||
.withErrorMessage('Please enter a valid URL or mailto address') | ||
.build() | ||
) | ||
.validate( | ||
RuleBuilder.composite | ||
.any() | ||
.withSubRule( | ||
RuleBuilder.matches( | ||
'^mailto: ?(?:[a-z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\\])$' | ||
) | ||
) | ||
.withSubRule( | ||
RuleBuilder.isValidUrl() | ||
.requireTld(true) | ||
.requireHost(true) | ||
.requireProtocol(true) | ||
.build() | ||
) | ||
.build('Please enter a valid URL or mailto address') | ||
) | ||
.build(); | ||
|
||
describe('RequiredBy Validator Builder', () => { | ||
it('Only name and alias', () => { | ||
testValidator(validator, { | ||
valid: [{name: 'test', webpushAlias: 'mailto:[email protected]'}], | ||
invalid: [{name: 'test'}], | ||
}); | ||
}); | ||
it('Only name and alias and public key', () => { | ||
testValidator(validator, { | ||
valid: [ | ||
{ | ||
name: 'test', | ||
webpushAlias: 'mailto:[email protected]', | ||
webpushVapidPublicKey: 'aaa', | ||
webpushVapidPrivateKey: 'aaa', | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
name: 'test', | ||
webpushAlias: 'mailto:[email protected]', | ||
webpushVapidPublicKey: 'aaa', | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |