Skip to content
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

:pencil: - chore

## 1.2.1
- :rocket: added capability to pass `softly` prefix and throw `SoftAssertionError`

## 1.2.0
- :rocket: added capability to pass `soft` flag and throw `SoftAssertionError`

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@qavajs/validation",
"version": "1.2.0",
"version": "1.2.1",
"description": "Lib that transform plain english definition to validation functions",
"main": "index.js",
"scripts": {
Expand Down
17 changes: 10 additions & 7 deletions src/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ export const validations = {
const isClause = '(?:is |do |does |to )?';
const notClause = '(?<reverse>not |to not )?';
const toBeClause = '(?:to )?(?:be )?';
const softlyClause = '(?<soft>softly )?';
const validationClause = `(?:(?<validation>${Object.values(validations).join('|')})(?:s|es| to)?)`;

export const validationExtractRegexp = new RegExp(`^${isClause}${notClause}${toBeClause}${validationClause}$`);
export const validationRegexp = new RegExp(`(${isClause}${notClause}${toBeClause}${validationClause})`);
export const validationExtractRegexp = new RegExp(`^${isClause}${notClause}${toBeClause}${softlyClause}${validationClause}$`);
export const validationRegexp = new RegExp(`(${isClause}${notClause}${toBeClause}${softlyClause}${validationClause})`);

type VerifyInput = {
AR: any;
Expand Down Expand Up @@ -123,16 +124,18 @@ export function verify({ AR, ER, validation, reverse, soft }: VerifyInput): void
export function getValidation(validationType: string, options?: { soft: boolean }): (AR: any, ER: any) => void {
const match = validationExtractRegexp.exec(validationType);
if (!match) throw new Error(`Validation '${validationType}' is not supported`);
const { reverse, validation } = match.groups as {[p: string]: string};
const { reverse, validation, soft } = match.groups as {[p: string]: string};
const softProp = options?.soft || !!soft;
return function (AR: any, ER: any) {
verify({ AR, ER, validation, reverse: Boolean(reverse), soft: options?.soft ?? false });
verify({ AR, ER, validation, reverse: Boolean(reverse), soft: softProp });
};
}

export function getPollValidation(validationType: string, opts?: { soft: boolean }): (AR: any, ER: any, options?: { timeout?: number, interval?: number }) => Promise<unknown> {
export function getPollValidation(validationType: string, options?: { soft: boolean }): (AR: any, ER: any, options?: { timeout?: number, interval?: number }) => Promise<unknown> {
const match = validationExtractRegexp.exec(validationType);
if (!match) throw new Error(`Poll validation '${validationType}' is not supported`);
const { reverse, validation } = match.groups as {[p: string]: string};
const { reverse, validation, soft } = match.groups as {[p: string]: string};
const softProp = options?.soft || !!soft;
return async function (AR: any, ER: any, options?: { timeout?: number, interval?: number }) {
const timeout = options?.timeout ?? 5000;
const interval = options?.interval ?? 500;
Expand All @@ -147,7 +150,7 @@ export function getPollValidation(validationType: string, opts?: { soft: boolean
ER,
validation,
reverse: Boolean(reverse),
soft: opts?.soft ?? false
soft: softProp
});
clearInterval(intervalId);
resolve();
Expand Down
8 changes: 7 additions & 1 deletion test/validationTransformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,14 @@ test('should throw AssertionError in case of hard error', () => {
expect(catcher).to.throw(AssertionError, "Fail: expected 1 to equal 2");
});

test('should throw AssertionError in case of soft error', () => {
test('should throw SoftAssertionError in case of soft error', () => {
const validation = getValidation('to equal', { soft: true });
const catcher = () => validation(1, 2);
expect(catcher).to.throw(SoftAssertionError, "Fail: expected 1 to equal 2");
});

test('should throw SoftAssertionError in case softly prefix', () => {
const validation = getValidation('to softly equal', { soft: true });
const catcher = () => validation(1, 2);
expect(catcher).to.throw(SoftAssertionError, "Fail: expected 1 to equal 2");
});