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

✨ [#2] Implement types for bsn #21

Merged
merged 1 commit into from
Nov 13, 2023
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
30 changes: 30 additions & 0 deletions src/formio/components/bsn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {InputComponentSchema, MultipleCapable, PrefillConfig} from '..';

type Validator = 'required';
type TranslatableKeys = 'label' | 'description' | 'tooltip';

export type BsnInputSchema = InputComponentSchema<string, Validator, TranslatableKeys>;

/**
* @group Form.io components
* @category Base types
*/
export interface BsnProperties {
type: 'bsn';
inputMask: '999999999';
validateOn: 'blur';
}

/**
* @group Form.io components
* @category Base types
*/
export type BaseBsnComponentSchema = Omit<BsnInputSchema, 'hideLabel' | 'placeholder'> &
BsnProperties &
PrefillConfig;

/**
* @group Form.io components
* @category Concrete types
*/
export type BsnComponentSchema = MultipleCapable<BaseBsnComponentSchema>;
1 change: 1 addition & 0 deletions src/formio/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './phonenumber';
export * from './postcode';
export * from './iban';
export * from './licenseplate';
export * from './bsn';
export * from './number';
export * from './checkbox';
export * from './selectboxes';
Expand Down
2 changes: 2 additions & 0 deletions src/formio/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BsnComponentSchema,
CheckboxComponentSchema,
ContentComponentSchema,
CurrencyComponentSchema,
Expand Down Expand Up @@ -58,6 +59,7 @@ export type AnyComponentSchema =
// special types
| IbanComponentSchema
| LicensePlateComponentSchema
| BsnComponentSchema
// layout
| ContentComponentSchema;

Expand Down
177 changes: 177 additions & 0 deletions test-d/formio/components/bsn.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import {expectAssignable, expectNotAssignable} from 'tsd';

import {BsnComponentSchema} from '../../../lib/';

// minimal bsn component schema
expectAssignable<BsnComponentSchema>({
id: 'yejak',
type: 'bsn',
key: 'someInput',
label: 'Some input',
inputMask: '999999999',
validateOn: 'blur',
});

// multiple false and appropriate default value type
expectAssignable<BsnComponentSchema>({
id: 'yejak',
type: 'bsn',
key: 'someInput',
label: 'Some input',
inputMask: '999999999',
validateOn: 'blur',
multiple: false,
defaultValue: '123456789',
});

// multiple true and appropriate default value type
expectAssignable<BsnComponentSchema>({
id: 'yejak',
type: 'bsn',
key: 'someInput',
label: 'Some input',
inputMask: '999999999',
validateOn: 'blur',
multiple: true,
defaultValue: ['123456789'],
});

// full, correct schema
expectAssignable<BsnComponentSchema>({
id: 'yejak',
type: 'bsn',
validateOn: 'blur',
// basic tab in builder form
label: 'Some input',
inputMask: '999999999',
key: 'someInput',
description: 'A description',
tooltip: 'A tooltip',
showInSummary: true,
showInEmail: false,
showInPDF: true,
multiple: false,
hidden: false,
clearOnHide: true,
isSensitiveData: true,
defaultValue: '',
disabled: false,
// advanced tab in builder form
conditional: {
show: undefined,
when: undefined,
eq: undefined,
},
// validation tab in builder form
validate: {
required: false,
plugins: [],
},
translatedErrors: {
nl: {
required: 'Je moet een waarde opgeven!!!',
},
},
errors: {
// translatedErrors is converted into errors by the backend
required: 'Je moet een waarde opgeven!!!',
},
// registration tab in builder form
registration: {
attribute: '',
},
// prefill tab in builder form
prefill: {
plugin: '',
attribute: '',
identifierRole: 'main',
},
// translations tab in builder form
openForms: {
translations: {
nl: {
label: 'foo',
description: 'bar',
},
},
},
});

// different component type
expectNotAssignable<BsnComponentSchema>({
id: 'yejak',
type: 'textfield',
key: 'someInput',
label: 'Some input',
inputMask: '999999999',
validateOn: 'blur',
} as const);

// using unsupported properties
expectNotAssignable<BsnComponentSchema>({
id: 'yejak',
type: 'bsn',
key: 'someInput',
label: 'Some input',
inputMask: '999999999',
validateOn: 'blur',
placeholder: 'no placeholder',
} as const);

// incorrect, invalid validator key
expectNotAssignable<BsnComponentSchema>({
id: 'yejak',
type: 'bsn',
key: 'someInput',
label: 'Some input',
inputMask: '999999999',
validate: {
maxLength: 7,
},
validateOn: 'blur',
} as const);

// invalid, multiple true and non-array default value
expectNotAssignable<BsnComponentSchema>({
id: 'yejak',
type: 'bsn',
key: 'someInput',
label: 'Some input',
inputMask: '999999999',
validateOn: 'blur',
multiple: true,
defaultValue: '',
} as const);

// invalid, multiple false and array default value
expectNotAssignable<BsnComponentSchema>({
id: 'yejak',
type: 'bsn',
key: 'someInput',
label: 'Some input',
inputMask: '999999999',
validateOn: 'blur',
multiple: false,
defaultValue: [''],
} as const);

// invalid, multiple true and wrong default value in array element
expectNotAssignable<BsnComponentSchema>({
id: 'yejak',
type: 'bsn',
key: 'someInput',
label: 'Some input',
inputMask: '999999999',
validateOn: 'blur',
multiple: true,
defaultValue: [123],
} as const);

// missing validateOn
expectNotAssignable<BsnComponentSchema>({
id: 'yejak',
type: 'bsn',
key: 'someInput',
label: 'Some input',
inputMask: '999999999',
});