Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support fnr validation in other environments than prod #1121

Merged
merged 7 commits into from
Apr 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ describe('Fodselsnummer', () => {

beforeEach(() => {
fnrComp = new NationalIdenityNumber();
fnrComp.options = {
appConfig: { config: { NAIS_CLUSTER_NAME: 'prod-gcp' } },
};
// @ts-ignore
vi.spyOn(NationalIdenityNumber.prototype, 't').mockImplementation((text) => text);
});
Expand Down Expand Up @@ -49,5 +52,19 @@ describe('Fodselsnummer', () => {
it('fails validation for tnr', () => {
expect(fnrComp.validateFnrNew(VALID_TNR)).toEqual(TEXTS.validering.fodselsnummerDNummer);
});

it('succeeds validation for tnr if env is delingslenke', () => {
fnrComp.options = {
appConfig: { config: { isDelingslenke: true, NAIS_CLUSTER_NAME: 'dev-gcp' } },
};
expect(fnrComp.validateFnrNew(VALID_TNR)).toBe(true);
});

it('succeeds validation for tnr if env is development', () => {
fnrComp.options = {
appConfig: { config: { isDevelopment: true, NAIS_CLUSTER_NAME: 'dev-gcp' } },
};
expect(fnrComp.validateFnrNew(VALID_TNR)).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { idnr } from '@navikt/fnrvalidator';
import { TEXTS } from '@navikt/skjemadigitalisering-shared-domain';
import { ConfigType, TEXTS } from '@navikt/skjemadigitalisering-shared-domain';
import TextFieldComponent from 'formiojs/components/textfield/TextField';
import nationalIdentityNumberBuilder from './NationalIdentityNumber.builder';
import nationalIdentityNumberForm from './NationalIdentityNumber.form';

const ALLOWED_TYPES = ['fnr', 'dnr'];
const ALLOWED_TEST_TYPES = ['fnr', 'dnr', 'hnr', 'tnr', 'dnr-and-hnr'];

export default class NationalIdentityNumber extends TextFieldComponent {
options: any;
t: any;

static schema() {
return TextFieldComponent.schema({
label: 'Fødselsnummer eller d-nummer',
Expand Down Expand Up @@ -35,18 +39,34 @@ export default class NationalIdentityNumber extends TextFieldComponent {
return true;
}

const appConfig = this.options?.appConfig?.config as ConfigType;

const inputValueNoSpace = inputValue.replace(' ', '');
const result = idnr(inputValueNoSpace);

// @ts-ignore
const { status, type } = idnr(inputValueNoSpace);
if (!ALLOWED_TYPES.includes(type) || status !== 'valid') {
//translate based on key in validering file.
// @ts-ignore
return this.t('fodselsnummerDNummer') === 'fodselsnummerDNummer'
const errorMessage: string =
this.t('fodselsnummerDNummer') === 'fodselsnummerDNummer'
? TEXTS.validering.fodselsnummerDNummer
: // @ts-ignore
this.t('fodselsnummerDNummer');
: this.t('fodselsnummerDNummer');

if (result.status === 'invalid') {
return errorMessage;
}

if (result.status === 'valid') {
// Allow only fnr and dnr in production
if (ALLOWED_TYPES.includes(result.type)) {
return true;
}

// Allow all types in test environments
if (appConfig?.NAIS_CLUSTER_NAME !== 'prod-gcp') {
return ALLOWED_TEST_TYPES.includes(result.type);
}

return errorMessage;
}

return true;
}
}
2 changes: 1 addition & 1 deletion packages/shared-domain/src/config/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface ConfigType {
FEATURE_TOGGLES: string;
NAIS_CLUSTER_NAME: string;
NAIS_CLUSTER_NAME?: 'dev-gcp' | 'prod-gcp';
isDelingslenke: boolean;
isDevelopment: boolean;
isLoggedIn: boolean;
Expand Down
Loading