@meith/profile-fields 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +165 -0
- package/package.json +25 -0
- package/src/index.ts +19 -0
- package/src/resolve.ts +70 -0
- package/src/service.ts +224 -0
- package/src/types.ts +78 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@meith/profile-fields",
|
|
3
|
+
"version": "0.16.0",
|
|
4
|
+
"license": "LGPL-3.0-or-later",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/meith-dev/meith.git",
|
|
8
|
+
"directory": "packages/profile-fields"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./src/index.ts",
|
|
12
|
+
"types": "./src/index.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"!src/**/*.test.*",
|
|
16
|
+
"!src/**/*.type-test.*"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@meith/core": "0.16.0",
|
|
23
|
+
"@meith/i18n": "0.16.0"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export {
|
|
2
|
+
editableFields,
|
|
3
|
+
type FieldAccess,
|
|
4
|
+
maxLengthFor,
|
|
5
|
+
resolveAccess,
|
|
6
|
+
visibleFields,
|
|
7
|
+
} from './resolve'
|
|
8
|
+
export { type ProfileFieldContext, ProfileFieldService } from './service'
|
|
9
|
+
export {
|
|
10
|
+
DEFAULT_MAX_LENGTH,
|
|
11
|
+
FIELD_TYPES,
|
|
12
|
+
type FieldType,
|
|
13
|
+
type ProfileFieldDefinition,
|
|
14
|
+
type ProfileFieldGroupRule,
|
|
15
|
+
type ProfileFieldRepository,
|
|
16
|
+
type ProfileFieldValue,
|
|
17
|
+
parseFieldType,
|
|
18
|
+
type ResolvedProfileField,
|
|
19
|
+
} from './types'
|
package/src/resolve.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_MAX_LENGTH,
|
|
3
|
+
type ProfileFieldDefinition,
|
|
4
|
+
type ProfileFieldGroupRule,
|
|
5
|
+
type ResolvedProfileField,
|
|
6
|
+
} from './types'
|
|
7
|
+
|
|
8
|
+
export interface FieldAccess {
|
|
9
|
+
readonly canView: boolean
|
|
10
|
+
readonly canEdit: boolean
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function resolveAccess(
|
|
14
|
+
field: ProfileFieldDefinition,
|
|
15
|
+
applicable: readonly ProfileFieldGroupRule[],
|
|
16
|
+
): FieldAccess {
|
|
17
|
+
const mine = applicable.filter((rule) => rule.fieldId === field.id)
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
canView: combine(
|
|
21
|
+
mine.map((rule) => rule.canView),
|
|
22
|
+
field.defaultVisible,
|
|
23
|
+
),
|
|
24
|
+
canEdit: combine(
|
|
25
|
+
mine.map((rule) => rule.canEdit),
|
|
26
|
+
field.defaultEditable,
|
|
27
|
+
),
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function combine(answers: readonly (boolean | null)[], fallback: boolean): boolean {
|
|
32
|
+
const stated = answers.filter((answer): answer is boolean => answer !== null)
|
|
33
|
+
if (stated.length === 0) return fallback
|
|
34
|
+
return stated.some((answer) => answer)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function visibleFields(input: {
|
|
38
|
+
readonly fields: readonly ProfileFieldDefinition[]
|
|
39
|
+
readonly applicable: readonly ProfileFieldGroupRule[]
|
|
40
|
+
readonly values: ReadonlyMap<number, string>
|
|
41
|
+
}): readonly ResolvedProfileField[] {
|
|
42
|
+
return input.fields
|
|
43
|
+
.filter((field) => field.isActive)
|
|
44
|
+
.map((field) => ({
|
|
45
|
+
field,
|
|
46
|
+
value: input.values.get(field.id) ?? '',
|
|
47
|
+
...resolveAccess(field, input.applicable),
|
|
48
|
+
}))
|
|
49
|
+
.filter((resolved) => resolved.canView && resolved.value !== '')
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function editableFields(input: {
|
|
53
|
+
readonly fields: readonly ProfileFieldDefinition[]
|
|
54
|
+
readonly applicable: readonly ProfileFieldGroupRule[]
|
|
55
|
+
readonly values: ReadonlyMap<number, string>
|
|
56
|
+
}): readonly ResolvedProfileField[] {
|
|
57
|
+
return input.fields
|
|
58
|
+
.filter((field) => field.isActive)
|
|
59
|
+
.map((field) => ({
|
|
60
|
+
field,
|
|
61
|
+
value: input.values.get(field.id) ?? '',
|
|
62
|
+
...resolveAccess(field, input.applicable),
|
|
63
|
+
}))
|
|
64
|
+
.filter((resolved) => resolved.canEdit)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function maxLengthFor(field: ProfileFieldDefinition): number {
|
|
68
|
+
if (field.maxLength !== null && field.maxLength > 0) return field.maxLength
|
|
69
|
+
return field.type === null ? DEFAULT_MAX_LENGTH.text : DEFAULT_MAX_LENGTH[field.type]
|
|
70
|
+
}
|
package/src/service.ts
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { ValidationError } from '@meith/core'
|
|
2
|
+
import { msg } from '@meith/i18n'
|
|
3
|
+
|
|
4
|
+
import { editableFields, maxLengthFor, visibleFields } from './resolve'
|
|
5
|
+
import type {
|
|
6
|
+
ProfileFieldDefinition,
|
|
7
|
+
ProfileFieldGroupRule,
|
|
8
|
+
ProfileFieldRepository,
|
|
9
|
+
ProfileFieldValue,
|
|
10
|
+
ResolvedProfileField,
|
|
11
|
+
} from './types'
|
|
12
|
+
|
|
13
|
+
export interface ProfileFieldContext {
|
|
14
|
+
readonly applicable: readonly ProfileFieldGroupRule[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class ProfileFieldService {
|
|
18
|
+
private readonly repository: ProfileFieldRepository
|
|
19
|
+
|
|
20
|
+
constructor(deps: { fields: ProfileFieldRepository }) {
|
|
21
|
+
this.repository = deps.fields
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async listAll(): Promise<readonly ProfileFieldDefinition[]> {
|
|
25
|
+
return this.repository.listFields()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async listGroupRules(): Promise<readonly ProfileFieldGroupRule[]> {
|
|
29
|
+
return this.repository.listGroupRules()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async visibleFor(
|
|
33
|
+
userId: number,
|
|
34
|
+
context: ProfileFieldContext,
|
|
35
|
+
): Promise<readonly ResolvedProfileField[]> {
|
|
36
|
+
const [fields, values] = await Promise.all([
|
|
37
|
+
this.repository.listFields(),
|
|
38
|
+
this.repository.valuesFor(userId),
|
|
39
|
+
])
|
|
40
|
+
|
|
41
|
+
return visibleFields({
|
|
42
|
+
fields,
|
|
43
|
+
applicable: context.applicable,
|
|
44
|
+
values: byField(values),
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async editableFor(
|
|
49
|
+
userId: number,
|
|
50
|
+
context: ProfileFieldContext,
|
|
51
|
+
): Promise<readonly ResolvedProfileField[]> {
|
|
52
|
+
const [fields, values] = await Promise.all([
|
|
53
|
+
this.repository.listFields(),
|
|
54
|
+
this.repository.valuesFor(userId),
|
|
55
|
+
])
|
|
56
|
+
|
|
57
|
+
return editableFields({
|
|
58
|
+
fields,
|
|
59
|
+
applicable: context.applicable,
|
|
60
|
+
values: byField(values),
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async requiredAtRegistration(
|
|
65
|
+
context: ProfileFieldContext,
|
|
66
|
+
): Promise<readonly ProfileFieldDefinition[]> {
|
|
67
|
+
const fields = await this.repository.listFields()
|
|
68
|
+
|
|
69
|
+
return editableFields({
|
|
70
|
+
fields,
|
|
71
|
+
applicable: context.applicable,
|
|
72
|
+
values: new Map(),
|
|
73
|
+
})
|
|
74
|
+
.filter((resolved) => resolved.field.requiredAtRegistration)
|
|
75
|
+
.map((resolved) => resolved.field)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async save(input: {
|
|
79
|
+
readonly userId: number
|
|
80
|
+
readonly submitted: ReadonlyMap<string, string>
|
|
81
|
+
readonly context: ProfileFieldContext
|
|
82
|
+
}): Promise<void> {
|
|
83
|
+
const editable = await this.editableFor(input.userId, input.context)
|
|
84
|
+
|
|
85
|
+
const values: ProfileFieldValue[] = []
|
|
86
|
+
for (const resolved of editable) {
|
|
87
|
+
const raw = input.submitted.get(resolved.field.key)
|
|
88
|
+
if (raw === undefined) continue
|
|
89
|
+
values.push({ fieldId: resolved.field.id, value: validate(resolved.field, raw) })
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (values.length > 0) {
|
|
93
|
+
await this.repository.saveValues({ userId: input.userId, values })
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async validateRegistration(input: {
|
|
98
|
+
readonly submitted: ReadonlyMap<string, string>
|
|
99
|
+
readonly context: ProfileFieldContext
|
|
100
|
+
}): Promise<readonly ProfileFieldValue[]> {
|
|
101
|
+
const required = await this.requiredAtRegistration(input.context)
|
|
102
|
+
|
|
103
|
+
return required.map((field) => {
|
|
104
|
+
const raw = (input.submitted.get(field.key) ?? '').trim()
|
|
105
|
+
if (raw === '')
|
|
106
|
+
throw new ValidationError(msg('error.profile-fields.required', { label: field.label }))
|
|
107
|
+
return { fieldId: field.id, value: validate(field, raw) }
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async applyRegistration(userId: number, values: readonly ProfileFieldValue[]): Promise<void> {
|
|
112
|
+
if (values.length === 0) return
|
|
113
|
+
await this.repository.saveValues({ userId, values })
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async create(input: {
|
|
117
|
+
readonly key: string
|
|
118
|
+
readonly label: string
|
|
119
|
+
readonly type: string
|
|
120
|
+
readonly options: readonly string[]
|
|
121
|
+
readonly requiredAtRegistration?: boolean
|
|
122
|
+
readonly showInPostbit?: boolean
|
|
123
|
+
readonly displayOrder?: number
|
|
124
|
+
}): Promise<ProfileFieldDefinition> {
|
|
125
|
+
const key = input.key.trim().toLowerCase()
|
|
126
|
+
if (!/^[a-z][a-z0-9_]{1,39}$/.test(key)) {
|
|
127
|
+
throw new ValidationError(msg('error.profile-fields.field-key-must-2-40'))
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const label = input.label.trim()
|
|
131
|
+
if (label === '') throw new ValidationError(msg('error.profile-fields.field-needs-label'))
|
|
132
|
+
|
|
133
|
+
const type = parseType(input.type)
|
|
134
|
+
if (type === 'select' && input.options.length === 0) {
|
|
135
|
+
throw new ValidationError(msg('error.profile-fields.dropdown-needs-at-least-one'))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (await this.repository.findByKey(key)) {
|
|
139
|
+
throw new ValidationError(`A field with the key "${key}" already exists.`)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return this.repository.create({
|
|
143
|
+
key,
|
|
144
|
+
label,
|
|
145
|
+
type,
|
|
146
|
+
options: input.options,
|
|
147
|
+
requiredAtRegistration: input.requiredAtRegistration ?? false,
|
|
148
|
+
showInPostbit: input.showInPostbit ?? false,
|
|
149
|
+
displayOrder: input.displayOrder ?? 0,
|
|
150
|
+
})
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async remove(key: string): Promise<boolean> {
|
|
154
|
+
return this.repository.remove(key.trim().toLowerCase())
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function byField(values: readonly ProfileFieldValue[]): ReadonlyMap<number, string> {
|
|
159
|
+
return new Map(values.map((value) => [value.fieldId, value.value]))
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function parseType(value: string): ProfileFieldDefinition['type'] & string {
|
|
163
|
+
const type = value.trim().toLowerCase()
|
|
164
|
+
if (
|
|
165
|
+
type === 'text' ||
|
|
166
|
+
type === 'textarea' ||
|
|
167
|
+
type === 'select' ||
|
|
168
|
+
type === 'checkbox' ||
|
|
169
|
+
type === 'url' ||
|
|
170
|
+
type === 'number'
|
|
171
|
+
) {
|
|
172
|
+
return type
|
|
173
|
+
}
|
|
174
|
+
throw new ValidationError(`"${value}" is not a field type.`)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function validate(field: ProfileFieldDefinition, raw: string): string {
|
|
178
|
+
const value = raw.trim()
|
|
179
|
+
if (value === '') return ''
|
|
180
|
+
|
|
181
|
+
const limit = maxLengthFor(field)
|
|
182
|
+
if (value.length > limit) {
|
|
183
|
+
throw new ValidationError(`${field.label} may be at most ${limit} characters.`)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
switch (field.type) {
|
|
187
|
+
case 'select':
|
|
188
|
+
if (!field.options.includes(value)) {
|
|
189
|
+
throw new ValidationError(`${field.label} must be one of the offered choices.`)
|
|
190
|
+
}
|
|
191
|
+
return value
|
|
192
|
+
|
|
193
|
+
case 'checkbox':
|
|
194
|
+
return 'yes'
|
|
195
|
+
|
|
196
|
+
case 'number':
|
|
197
|
+
if (!/^-?\d+(\.\d+)?$/.test(value)) {
|
|
198
|
+
throw new ValidationError(`${field.label} must be a number.`)
|
|
199
|
+
}
|
|
200
|
+
return value
|
|
201
|
+
|
|
202
|
+
case 'url':
|
|
203
|
+
return normaliseUrl(field, value)
|
|
204
|
+
|
|
205
|
+
default:
|
|
206
|
+
return value
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function normaliseUrl(field: ProfileFieldDefinition, value: string): string {
|
|
211
|
+
const candidate = /^[a-z][a-z0-9+.-]*:/i.test(value) ? value : `https://${value}`
|
|
212
|
+
|
|
213
|
+
let url: URL
|
|
214
|
+
try {
|
|
215
|
+
url = new URL(candidate)
|
|
216
|
+
} catch {
|
|
217
|
+
throw new ValidationError(`${field.label} must be a web address.`)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
221
|
+
throw new ValidationError(`${field.label} must start with http:// or https://.`)
|
|
222
|
+
}
|
|
223
|
+
return url.toString()
|
|
224
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export const FIELD_TYPES = ['text', 'textarea', 'select', 'checkbox', 'url', 'number'] as const
|
|
2
|
+
|
|
3
|
+
export type FieldType = (typeof FIELD_TYPES)[number]
|
|
4
|
+
|
|
5
|
+
export function parseFieldType(value: string): FieldType | null {
|
|
6
|
+
return FIELD_TYPES.includes(value as FieldType) ? (value as FieldType) : null
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_MAX_LENGTH: Readonly<Record<FieldType, number>> = {
|
|
10
|
+
text: 200,
|
|
11
|
+
textarea: 2000,
|
|
12
|
+
select: 100,
|
|
13
|
+
checkbox: 5,
|
|
14
|
+
url: 300,
|
|
15
|
+
number: 20,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ProfileFieldDefinition {
|
|
19
|
+
readonly id: number
|
|
20
|
+
readonly key: string
|
|
21
|
+
readonly label: string
|
|
22
|
+
readonly description: string | null
|
|
23
|
+
readonly type: FieldType | null
|
|
24
|
+
readonly options: readonly string[]
|
|
25
|
+
readonly maxLength: number | null
|
|
26
|
+
readonly displayOrder: number
|
|
27
|
+
readonly isActive: boolean
|
|
28
|
+
readonly requiredAtRegistration: boolean
|
|
29
|
+
readonly defaultVisible: boolean
|
|
30
|
+
readonly defaultEditable: boolean
|
|
31
|
+
readonly showInPostbit: boolean
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface ProfileFieldGroupRule {
|
|
35
|
+
readonly fieldId: number
|
|
36
|
+
readonly groupId: number
|
|
37
|
+
readonly canView: boolean | null
|
|
38
|
+
readonly canEdit: boolean | null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface ProfileFieldValue {
|
|
42
|
+
readonly fieldId: number
|
|
43
|
+
readonly value: string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface ResolvedProfileField {
|
|
47
|
+
readonly field: ProfileFieldDefinition
|
|
48
|
+
readonly value: string
|
|
49
|
+
readonly canView: boolean
|
|
50
|
+
readonly canEdit: boolean
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ProfileFieldRepository {
|
|
54
|
+
listFields(): Promise<readonly ProfileFieldDefinition[]>
|
|
55
|
+
|
|
56
|
+
listGroupRules(): Promise<readonly ProfileFieldGroupRule[]>
|
|
57
|
+
|
|
58
|
+
valuesFor(userId: number): Promise<readonly ProfileFieldValue[]>
|
|
59
|
+
|
|
60
|
+
saveValues(input: {
|
|
61
|
+
readonly userId: number
|
|
62
|
+
readonly values: readonly ProfileFieldValue[]
|
|
63
|
+
}): Promise<void>
|
|
64
|
+
|
|
65
|
+
findByKey(key: string): Promise<ProfileFieldDefinition | null>
|
|
66
|
+
|
|
67
|
+
create(input: {
|
|
68
|
+
readonly key: string
|
|
69
|
+
readonly label: string
|
|
70
|
+
readonly type: FieldType
|
|
71
|
+
readonly options: readonly string[]
|
|
72
|
+
readonly requiredAtRegistration: boolean
|
|
73
|
+
readonly showInPostbit: boolean
|
|
74
|
+
readonly displayOrder: number
|
|
75
|
+
}): Promise<ProfileFieldDefinition>
|
|
76
|
+
|
|
77
|
+
remove(key: string): Promise<boolean>
|
|
78
|
+
}
|