Skip to content

Commit

Permalink
fix: fixed required optionality error-response
Browse files Browse the repository at this point in the history
  • Loading branch information
BlvckBytes committed Nov 25, 2021
1 parent 45e3ce2 commit 6e5d216
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .env-presets
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CTOR_ENSURE_ENSURE_ISDATE_DESC=valid JS date object
CTOR_ENSURE_ENSURE_MINMAXDATE_DESC=opt:"at least {min}":{hasMin}opt:" and ":{hasBoth}opt:"up to {max}":{hasMax}
CTOR_ENSURE_ENSURE_MINMAXNUMBER_DESC=opt:"at least {min}":{hasMin}opt:" and ":{hasBoth}opt:"up to {max}":{hasMax}
CTOR_ENSURE_OPTIONALITY_DESC=no trn:{nullable}:"undefined":"null" values allowed
CTOR_ENSURE_OPTIONALITY_DESC=no opt:"null":{nonNull}opt:" or ":{both}opt:"undefined":{nonOmit} values allowed

# German (DE)
CTOR_ENSURE_ENSURE_ALPHA--DE_DESC=nur alphabetische Zeichen opt:"ohne Abstände":{nospaces}
Expand Down Expand Up @@ -52,4 +53,5 @@ CTOR_ENSURE_ENSURE_ISARRAY--DE_DESC=trn:{positive}:"array von Werten":"Skalarwer
CTOR_ENSURE_ENSURE_ISDATE--DE_DESC=valides JS date-Objekt
CTOR_ENSURE_ENSURE_MINMAXDATE--DE_DESC=opt:"mindestens {min}":{hasMin}opt:" und ":{hasBoth}opt:"bis zu {max}":{hasMax}
CTOR_ENSURE_ENSURE_MINMAXNUMBER--DE_DESC=opt:"mindestens {min}":{hasMin}opt:" und ":{hasBoth}opt:"bis zu {max}":{hasMax}
CTOR_ENSURE_OPTIONALITY--DE_DESC=kein trn:{nullable}:"undefinierter":"null" Wert erlaubt
CTOR_ENSURE_OPTIONALITY--DE_DESC=kein trn:{nullable}:"undefinierter":"null" Wert erlaubt
CTOR_ENSURE_OPTIONALITY--DE_DESC=keine opt:"null":{nonNull}opt:" oder ":{both}opt:"undefined":{nonOmit} Werte erlaubt
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ctor-ensure",
"author": "BlvckBytes",
"version": "1.0.11",
"version": "1.0.12",
"license": "GPL-3.0",
"repository": {
"type": "git",
Expand Down
11 changes: 9 additions & 2 deletions src/ctor-ensure.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ const checkOptionality = (value: any, optionality: Optionality): [string | null,
case Optionality.IRRELEVANT:
return [null, !(value === null || value === undefined)];

case Optionality.REQUIRED:
if (value !== undefined && value !== null) return [null, true];
break;

// Non-registered case, just throw an error
default:
return [null, true];
break;
}

const nullable = optionality === Optionality.NULLABLE;
const omittable = optionality === Optionality.OMITTABLE;
return [template('OPTIONALITY', {
nullable: optionality === Optionality.NULLABLE,
nonNull: !nullable, nonOmit: !omittable, both: !nullable && !omittable,
}), false];
};

Expand Down
16 changes: 13 additions & 3 deletions test/ctor-ensure.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { checkEnsureArgErrors, genModelName } from './test-util';

describe('@CtorEnsure', () => {

const optionalityDesc = (nullable = true) => `no ${nullable ? 'undefined' : 'null'} values allowed`;
const optionalityDesc = (nonNull: boolean, nonOmit: boolean) => `no ${strOpt('null', nonNull)}${strOpt(' or ', nonNull && nonOmit)}${strOpt('undefined', nonOmit)} values allowed`;

it('should apply the displayname metadata', () => {
// Create test class and apply decorator
Expand Down Expand Up @@ -113,14 +113,14 @@ describe('@CtorEnsure', () => {
expect(mkOptionalityCase(Optionality.NULLABLE, null)).not.to.throw;
expect(mkOptionalityCase(Optionality.NULLABLE, undefined))
.to.throw(CtorEnsureException.message)
.property('errors').satisfy(checkEnsureArgErrors(optionalityDesc(true), undefined));
.property('errors').satisfy(checkEnsureArgErrors(optionalityDesc(false, true), undefined));
});

it('should allow undefined optionality but disallow null', () => {
expect(mkOptionalityCase(Optionality.OMITTABLE, undefined)).not.to.throw;
expect(mkOptionalityCase(Optionality.OMITTABLE, null))
.to.throw(CtorEnsureException.message)
.property('errors').satisfy(checkEnsureArgErrors(optionalityDesc(false), null));
.property('errors').satisfy(checkEnsureArgErrors(optionalityDesc(true, false), null));
});

it('should allow irrelevant optionality and pass values on to validation', () => {
Expand All @@ -131,6 +131,16 @@ describe('@CtorEnsure', () => {
.property('errors').satisfy(checkEnsureArgErrors(optCaseDesc, 'value'));
});

it('shouldn\'t allow undefined or null on required field', () => {
expect(mkOptionalityCase(Optionality.REQUIRED, undefined, true))
.to.throw(CtorEnsureException.message)
.property('errors').satisfy(checkEnsureArgErrors(optionalityDesc(true, true), undefined));

expect(mkOptionalityCase(Optionality.REQUIRED, null, true))
.to.throw(CtorEnsureException.message)
.property('errors').satisfy(checkEnsureArgErrors(optionalityDesc(true, true), null));
});

it('should forward values to ensures', () => {
expect(mkOptionalityCase(Optionality.REQUIRED, 'value', true))
.to.throw(CtorEnsureException.message)
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/list-ensures.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import { CtorEnsure, CtorEnsureException, ENSURE_ARRAYSIZEMAX, ENSURE_ARRAYSIZEMIN, ENSURE_ENUM, ENSURE_ISARRAY, ValidatedArg } from '../../src';
import { checkExceptionHasFields, genModelName } from '../test-util';
import { genModelName } from '../test-util';

describe('list-ensure E2E', () => {
enum Skill {
Expand Down Expand Up @@ -28,7 +28,7 @@ describe('list-ensure E2E', () => {
it('should not accept missing parameter', () => {
expect(() => new Test(undefined as any))
.to.throw(CtorEnsureException.message)
.satisfy((e: CtorEnsureException) => (e.errors.filter(it => it.field === 'skills').length === 3));
.satisfy((e: CtorEnsureException) => (e.errors.filter(it => it.field === 'skills').length === 1));
});

it('should not accept empty array', () => {
Expand Down

0 comments on commit 6e5d216

Please sign in to comment.