diff --git a/CHANGELOG.md b/CHANGELOG.md index 80f718a..3cefae2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/package-lock.json b/package-lock.json index 521c613..3ce053d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@qavajs/validation", - "version": "1.2.0", + "version": "1.2.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@qavajs/validation", - "version": "1.2.0", + "version": "1.2.1", "license": "MIT", "dependencies": { "ajv": "^8.17.1", diff --git a/package.json b/package.json index 0fb8730..b63e18a 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/verify.ts b/src/verify.ts index 34e4388..c0820bd 100644 --- a/src/verify.ts +++ b/src/verify.ts @@ -71,10 +71,11 @@ export const validations = { const isClause = '(?:is |do |does |to )?'; const notClause = '(?not |to not )?'; const toBeClause = '(?:to )?(?:be )?'; +const softlyClause = '(?softly )?'; const validationClause = `(?:(?${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; @@ -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 { +export function getPollValidation(validationType: string, options?: { soft: boolean }): (AR: any, ER: any, options?: { timeout?: number, interval?: number }) => Promise { 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; @@ -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(); diff --git a/test/validationTransformer.spec.ts b/test/validationTransformer.spec.ts index 054318a..4f95eb2 100644 --- a/test/validationTransformer.spec.ts +++ b/test/validationTransformer.spec.ts @@ -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"); }); \ No newline at end of file