@nixxie-cms/fields-address 1.0.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 +23 -0
- package/README.md +37 -0
- package/dist/declarations/src/index.d.ts +36 -0
- package/dist/declarations/src/index.d.ts.map +1 -0
- package/dist/nixxie-cms-fields-address.cjs.d.ts +2 -0
- package/dist/nixxie-cms-fields-address.cjs.js +109 -0
- package/dist/nixxie-cms-fields-address.esm.js +104 -0
- package/package.json +33 -0
- package/src/index.ts +133 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nixxie International DMCC
|
|
4
|
+
Portions Copyright (c) 2023 Thinkmill Labs Pty Ltd and contributors
|
|
5
|
+
(this software is derived from the KeystoneJS project, https://keystonejs.com)
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# @nixxie-cms/fields-address
|
|
2
|
+
|
|
3
|
+
A structured postal-address field for Nixxie CMS. Stores
|
|
4
|
+
`{ line1?, line2?, city?, region?, postalCode?, country?, lat?, lng? }` as JSON.
|
|
5
|
+
|
|
6
|
+
- Unknown keys are stripped on save.
|
|
7
|
+
- `country` must be an ISO-3166 alpha-2 code and is uppercased on save (`'nl'` → `'NL'`).
|
|
8
|
+
- `lat`/`lng` are validated against `[-90, 90]` / `[-180, 180]` when present.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { address } from '@nixxie-cms/fields-address'
|
|
12
|
+
|
|
13
|
+
fields: {
|
|
14
|
+
shippingAddress: address({
|
|
15
|
+
validation: { isRequired: true, requireCountry: true },
|
|
16
|
+
}),
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`isRequired` accepts an address with at least `line1` + `city`, or any non-empty formatted
|
|
21
|
+
result. `requireCountry` additionally rejects addresses without a country code.
|
|
22
|
+
|
|
23
|
+
## Helpers
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { formatAddress, type AddressValue } from '@nixxie-cms/fields-address'
|
|
27
|
+
|
|
28
|
+
const value: AddressValue = {
|
|
29
|
+
line1: 'Herengracht 1',
|
|
30
|
+
city: 'Amsterdam',
|
|
31
|
+
postalCode: '1015 BG',
|
|
32
|
+
country: 'NL',
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
formatAddress(value) // 'Herengracht 1, Amsterdam, 1015 BG, NL'
|
|
36
|
+
formatAddress(value, { separator: '\n' }) // one part per line
|
|
37
|
+
```
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { JsonFieldConfig } from '@nixxie-cms/core/fields';
|
|
2
|
+
import type { BaseCollectionTypeInfo, FieldTypeFunc } from '@nixxie-cms/core/types';
|
|
3
|
+
/** A structured postal address. All parts are optional; `country` is ISO-3166 alpha-2. */
|
|
4
|
+
export type AddressValue = {
|
|
5
|
+
line1?: string;
|
|
6
|
+
line2?: string;
|
|
7
|
+
city?: string;
|
|
8
|
+
region?: string;
|
|
9
|
+
postalCode?: string;
|
|
10
|
+
/** ISO-3166 alpha-2 country code, stored uppercased (e.g. 'NL'). */
|
|
11
|
+
country?: string;
|
|
12
|
+
lat?: number;
|
|
13
|
+
lng?: number;
|
|
14
|
+
};
|
|
15
|
+
export type AddressFieldConfig<CollectionTypeInfo extends BaseCollectionTypeInfo> = Omit<JsonFieldConfig<CollectionTypeInfo>, 'defaultValue'> & {
|
|
16
|
+
validation?: {
|
|
17
|
+
/** Require an address with at least `line1` + `city`, or any non-empty formatted result. */
|
|
18
|
+
isRequired?: boolean;
|
|
19
|
+
/** Require a `country` code to be present. */
|
|
20
|
+
requireCountry?: boolean;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Join the non-empty parts of an address in display order
|
|
25
|
+
* (line1, line2, city, region, postalCode, country).
|
|
26
|
+
*/
|
|
27
|
+
export declare function formatAddress(value: AddressValue | null | undefined, options?: {
|
|
28
|
+
separator?: string;
|
|
29
|
+
}): string;
|
|
30
|
+
/**
|
|
31
|
+
* A structured postal-address field storing
|
|
32
|
+
* `{ line1?, line2?, city?, region?, postalCode?, country?, lat?, lng? }` as JSON.
|
|
33
|
+
* Unknown keys are stripped, the country code is uppercased, and coordinate ranges are validated.
|
|
34
|
+
*/
|
|
35
|
+
export declare function address<CollectionTypeInfo extends BaseCollectionTypeInfo>(config?: AddressFieldConfig<CollectionTypeInfo>): FieldTypeFunc<CollectionTypeInfo>;
|
|
36
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AAC9D,OAAO,KAAK,EAAE,sBAAsB,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AAEnF,0FAA0F;AAC1F,MAAM,MAAM,YAAY,GAAG;IACzB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,oEAAoE;IACpE,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,MAAM,CAAA;CACb,CAAA;AAMD,MAAM,MAAM,kBAAkB,CAAC,kBAAkB,SAAS,sBAAsB,IAAI,IAAI,CACtF,eAAe,CAAC,kBAAkB,CAAC,EACnC,cAAc,CACf,GAAG;IACF,UAAU,CAAC,EAAE;QACX,4FAA4F;QAC5F,UAAU,CAAC,EAAE,OAAO,CAAA;QACpB,8CAA8C;QAC9C,cAAc,CAAC,EAAE,OAAO,CAAA;KACzB,CAAA;CACF,CAAA;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,YAAY,GAAG,IAAI,GAAG,SAAS,EACtC,OAAO,GAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAA;CAAO,GACnC,MAAM,CAOR;AAMD;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,kBAAkB,SAAS,sBAAsB,EACvE,MAAM,GAAE,kBAAkB,CAAC,kBAAkB,CAAM,GAClD,aAAa,CAAC,kBAAkB,CAAC,CAwEnC"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export * from "./declarations/src/index.js";
|
|
2
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibml4eGllLWNtcy1maWVsZHMtYWRkcmVzcy5janMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4vZGVjbGFyYXRpb25zL3NyYy9pbmRleC5kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var fields = require('@nixxie-cms/core/fields');
|
|
6
|
+
|
|
7
|
+
/** A structured postal address. All parts are optional; `country` is ISO-3166 alpha-2. */
|
|
8
|
+
|
|
9
|
+
const STRING_KEYS = ['line1', 'line2', 'city', 'region', 'postalCode', 'country'];
|
|
10
|
+
const NUMBER_KEYS = ['lat', 'lng'];
|
|
11
|
+
const KNOWN_KEYS = [...STRING_KEYS, ...NUMBER_KEYS];
|
|
12
|
+
/**
|
|
13
|
+
* Join the non-empty parts of an address in display order
|
|
14
|
+
* (line1, line2, city, region, postalCode, country).
|
|
15
|
+
*/
|
|
16
|
+
function formatAddress(value, options = {}) {
|
|
17
|
+
var _options$separator;
|
|
18
|
+
if (!value || typeof value !== 'object') return '';
|
|
19
|
+
const separator = (_options$separator = options.separator) !== null && _options$separator !== void 0 ? _options$separator : ', ';
|
|
20
|
+
return STRING_KEYS.map(key => value[key]).filter(part => typeof part === 'string' && part.trim().length > 0).map(part => part.trim()).join(separator);
|
|
21
|
+
}
|
|
22
|
+
function isPlainObject(v) {
|
|
23
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A structured postal-address field storing
|
|
28
|
+
* `{ line1?, line2?, city?, region?, postalCode?, country?, lat?, lng? }` as JSON.
|
|
29
|
+
* Unknown keys are stripped, the country code is uppercased, and coordinate ranges are validated.
|
|
30
|
+
*/
|
|
31
|
+
function address(config = {}) {
|
|
32
|
+
const {
|
|
33
|
+
validation,
|
|
34
|
+
hooks,
|
|
35
|
+
...rest
|
|
36
|
+
} = config;
|
|
37
|
+
return fields.json({
|
|
38
|
+
...rest,
|
|
39
|
+
defaultValue: null,
|
|
40
|
+
hooks: {
|
|
41
|
+
...hooks,
|
|
42
|
+
resolveInput: args => {
|
|
43
|
+
const value = args.resolvedData[args.fieldKey];
|
|
44
|
+
if (!isPlainObject(value)) return value;
|
|
45
|
+
// Strip unknown keys and normalise the country code to uppercase.
|
|
46
|
+
const cleaned = {};
|
|
47
|
+
for (const key of KNOWN_KEYS) {
|
|
48
|
+
if (value[key] !== undefined) cleaned[key] = value[key];
|
|
49
|
+
}
|
|
50
|
+
if (typeof cleaned.country === 'string') {
|
|
51
|
+
cleaned.country = cleaned.country.toUpperCase();
|
|
52
|
+
}
|
|
53
|
+
return cleaned;
|
|
54
|
+
},
|
|
55
|
+
validate: args => {
|
|
56
|
+
const {
|
|
57
|
+
resolvedData,
|
|
58
|
+
fieldKey,
|
|
59
|
+
addValidationError,
|
|
60
|
+
operation
|
|
61
|
+
} = args;
|
|
62
|
+
if (operation === 'delete') return;
|
|
63
|
+
const value = resolvedData[fieldKey];
|
|
64
|
+
if (value === undefined) return;
|
|
65
|
+
if (value === null) {
|
|
66
|
+
if (validation !== null && validation !== void 0 && validation.isRequired) addValidationError(`${fieldKey} is required`);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (!isPlainObject(value)) {
|
|
70
|
+
addValidationError(`${fieldKey} must be an address object`);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const v = value;
|
|
74
|
+
for (const key of STRING_KEYS) {
|
|
75
|
+
if (v[key] !== undefined && typeof v[key] !== 'string') {
|
|
76
|
+
addValidationError(`${fieldKey}.${key} must be a string`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (const key of NUMBER_KEYS) {
|
|
80
|
+
if (v[key] !== undefined && (typeof v[key] !== 'number' || !Number.isFinite(v[key]))) {
|
|
81
|
+
addValidationError(`${fieldKey}.${key} must be a finite number`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (typeof v.country === 'string' && !/^[A-Z]{2}$/i.test(v.country)) {
|
|
85
|
+
addValidationError(`${fieldKey}.country must be a 2-letter ISO-3166 alpha-2 code (e.g. 'NL')`);
|
|
86
|
+
}
|
|
87
|
+
if (typeof v.lat === 'number' && (v.lat < -90 || v.lat > 90)) {
|
|
88
|
+
addValidationError(`${fieldKey}.lat must be between -90 and 90`);
|
|
89
|
+
}
|
|
90
|
+
if (typeof v.lng === 'number' && (v.lng < -180 || v.lng > 180)) {
|
|
91
|
+
addValidationError(`${fieldKey}.lng must be between -180 and 180`);
|
|
92
|
+
}
|
|
93
|
+
if (validation !== null && validation !== void 0 && validation.requireCountry && !(typeof v.country === 'string' && v.country.trim())) {
|
|
94
|
+
addValidationError(`${fieldKey}.country is required`);
|
|
95
|
+
}
|
|
96
|
+
if (validation !== null && validation !== void 0 && validation.isRequired) {
|
|
97
|
+
var _v$line, _v$city;
|
|
98
|
+
const hasLine1AndCity = Boolean((_v$line = v.line1) === null || _v$line === void 0 ? void 0 : _v$line.trim()) && Boolean((_v$city = v.city) === null || _v$city === void 0 ? void 0 : _v$city.trim());
|
|
99
|
+
if (!hasLine1AndCity && formatAddress(v).length === 0) {
|
|
100
|
+
addValidationError(`${fieldKey} must have at least line1 and city, or some content`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
exports.address = address;
|
|
109
|
+
exports.formatAddress = formatAddress;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { json } from '@nixxie-cms/core/fields';
|
|
2
|
+
|
|
3
|
+
/** A structured postal address. All parts are optional; `country` is ISO-3166 alpha-2. */
|
|
4
|
+
|
|
5
|
+
const STRING_KEYS = ['line1', 'line2', 'city', 'region', 'postalCode', 'country'];
|
|
6
|
+
const NUMBER_KEYS = ['lat', 'lng'];
|
|
7
|
+
const KNOWN_KEYS = [...STRING_KEYS, ...NUMBER_KEYS];
|
|
8
|
+
/**
|
|
9
|
+
* Join the non-empty parts of an address in display order
|
|
10
|
+
* (line1, line2, city, region, postalCode, country).
|
|
11
|
+
*/
|
|
12
|
+
function formatAddress(value, options = {}) {
|
|
13
|
+
var _options$separator;
|
|
14
|
+
if (!value || typeof value !== 'object') return '';
|
|
15
|
+
const separator = (_options$separator = options.separator) !== null && _options$separator !== void 0 ? _options$separator : ', ';
|
|
16
|
+
return STRING_KEYS.map(key => value[key]).filter(part => typeof part === 'string' && part.trim().length > 0).map(part => part.trim()).join(separator);
|
|
17
|
+
}
|
|
18
|
+
function isPlainObject(v) {
|
|
19
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A structured postal-address field storing
|
|
24
|
+
* `{ line1?, line2?, city?, region?, postalCode?, country?, lat?, lng? }` as JSON.
|
|
25
|
+
* Unknown keys are stripped, the country code is uppercased, and coordinate ranges are validated.
|
|
26
|
+
*/
|
|
27
|
+
function address(config = {}) {
|
|
28
|
+
const {
|
|
29
|
+
validation,
|
|
30
|
+
hooks,
|
|
31
|
+
...rest
|
|
32
|
+
} = config;
|
|
33
|
+
return json({
|
|
34
|
+
...rest,
|
|
35
|
+
defaultValue: null,
|
|
36
|
+
hooks: {
|
|
37
|
+
...hooks,
|
|
38
|
+
resolveInput: args => {
|
|
39
|
+
const value = args.resolvedData[args.fieldKey];
|
|
40
|
+
if (!isPlainObject(value)) return value;
|
|
41
|
+
// Strip unknown keys and normalise the country code to uppercase.
|
|
42
|
+
const cleaned = {};
|
|
43
|
+
for (const key of KNOWN_KEYS) {
|
|
44
|
+
if (value[key] !== undefined) cleaned[key] = value[key];
|
|
45
|
+
}
|
|
46
|
+
if (typeof cleaned.country === 'string') {
|
|
47
|
+
cleaned.country = cleaned.country.toUpperCase();
|
|
48
|
+
}
|
|
49
|
+
return cleaned;
|
|
50
|
+
},
|
|
51
|
+
validate: args => {
|
|
52
|
+
const {
|
|
53
|
+
resolvedData,
|
|
54
|
+
fieldKey,
|
|
55
|
+
addValidationError,
|
|
56
|
+
operation
|
|
57
|
+
} = args;
|
|
58
|
+
if (operation === 'delete') return;
|
|
59
|
+
const value = resolvedData[fieldKey];
|
|
60
|
+
if (value === undefined) return;
|
|
61
|
+
if (value === null) {
|
|
62
|
+
if (validation !== null && validation !== void 0 && validation.isRequired) addValidationError(`${fieldKey} is required`);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (!isPlainObject(value)) {
|
|
66
|
+
addValidationError(`${fieldKey} must be an address object`);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const v = value;
|
|
70
|
+
for (const key of STRING_KEYS) {
|
|
71
|
+
if (v[key] !== undefined && typeof v[key] !== 'string') {
|
|
72
|
+
addValidationError(`${fieldKey}.${key} must be a string`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
for (const key of NUMBER_KEYS) {
|
|
76
|
+
if (v[key] !== undefined && (typeof v[key] !== 'number' || !Number.isFinite(v[key]))) {
|
|
77
|
+
addValidationError(`${fieldKey}.${key} must be a finite number`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (typeof v.country === 'string' && !/^[A-Z]{2}$/i.test(v.country)) {
|
|
81
|
+
addValidationError(`${fieldKey}.country must be a 2-letter ISO-3166 alpha-2 code (e.g. 'NL')`);
|
|
82
|
+
}
|
|
83
|
+
if (typeof v.lat === 'number' && (v.lat < -90 || v.lat > 90)) {
|
|
84
|
+
addValidationError(`${fieldKey}.lat must be between -90 and 90`);
|
|
85
|
+
}
|
|
86
|
+
if (typeof v.lng === 'number' && (v.lng < -180 || v.lng > 180)) {
|
|
87
|
+
addValidationError(`${fieldKey}.lng must be between -180 and 180`);
|
|
88
|
+
}
|
|
89
|
+
if (validation !== null && validation !== void 0 && validation.requireCountry && !(typeof v.country === 'string' && v.country.trim())) {
|
|
90
|
+
addValidationError(`${fieldKey}.country is required`);
|
|
91
|
+
}
|
|
92
|
+
if (validation !== null && validation !== void 0 && validation.isRequired) {
|
|
93
|
+
var _v$line, _v$city;
|
|
94
|
+
const hasLine1AndCity = Boolean((_v$line = v.line1) === null || _v$line === void 0 ? void 0 : _v$line.trim()) && Boolean((_v$city = v.city) === null || _v$city === void 0 ? void 0 : _v$city.trim());
|
|
95
|
+
if (!hasLine1AndCity && formatAddress(v).length === 0) {
|
|
96
|
+
addValidationError(`${fieldKey} must have at least line1 and city, or some content`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export { address, formatAddress };
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nixxie-cms/fields-address",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "dist/nixxie-cms-fields-address.cjs.js",
|
|
6
|
+
"module": "dist/nixxie-cms-fields-address.esm.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/nixxie-cms-fields-address.cjs.js",
|
|
10
|
+
"module": "./dist/nixxie-cms-fields-address.esm.js",
|
|
11
|
+
"default": "./dist/nixxie-cms-fields-address.cjs.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@babel/runtime": "^7.24.7"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@nixxie-cms/core": "^1.1.0"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@nixxie-cms/core": "^1.0.1"
|
|
23
|
+
},
|
|
24
|
+
"preconstruct": {
|
|
25
|
+
"entrypoints": [
|
|
26
|
+
"index.ts"
|
|
27
|
+
]
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/nixxiecms/nixxie/tree/main/packages/fields-address"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { json } from '@nixxie-cms/core/fields'
|
|
2
|
+
import type { JsonFieldConfig } from '@nixxie-cms/core/fields'
|
|
3
|
+
import type { BaseCollectionTypeInfo, FieldTypeFunc } from '@nixxie-cms/core/types'
|
|
4
|
+
|
|
5
|
+
/** A structured postal address. All parts are optional; `country` is ISO-3166 alpha-2. */
|
|
6
|
+
export type AddressValue = {
|
|
7
|
+
line1?: string
|
|
8
|
+
line2?: string
|
|
9
|
+
city?: string
|
|
10
|
+
region?: string
|
|
11
|
+
postalCode?: string
|
|
12
|
+
/** ISO-3166 alpha-2 country code, stored uppercased (e.g. 'NL'). */
|
|
13
|
+
country?: string
|
|
14
|
+
lat?: number
|
|
15
|
+
lng?: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const STRING_KEYS = ['line1', 'line2', 'city', 'region', 'postalCode', 'country'] as const
|
|
19
|
+
const NUMBER_KEYS = ['lat', 'lng'] as const
|
|
20
|
+
const KNOWN_KEYS: readonly string[] = [...STRING_KEYS, ...NUMBER_KEYS]
|
|
21
|
+
|
|
22
|
+
export type AddressFieldConfig<CollectionTypeInfo extends BaseCollectionTypeInfo> = Omit<
|
|
23
|
+
JsonFieldConfig<CollectionTypeInfo>,
|
|
24
|
+
'defaultValue'
|
|
25
|
+
> & {
|
|
26
|
+
validation?: {
|
|
27
|
+
/** Require an address with at least `line1` + `city`, or any non-empty formatted result. */
|
|
28
|
+
isRequired?: boolean
|
|
29
|
+
/** Require a `country` code to be present. */
|
|
30
|
+
requireCountry?: boolean
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Join the non-empty parts of an address in display order
|
|
36
|
+
* (line1, line2, city, region, postalCode, country).
|
|
37
|
+
*/
|
|
38
|
+
export function formatAddress(
|
|
39
|
+
value: AddressValue | null | undefined,
|
|
40
|
+
options: { separator?: string } = {}
|
|
41
|
+
): string {
|
|
42
|
+
if (!value || typeof value !== 'object') return ''
|
|
43
|
+
const separator = options.separator ?? ', '
|
|
44
|
+
return STRING_KEYS.map(key => value[key])
|
|
45
|
+
.filter((part): part is string => typeof part === 'string' && part.trim().length > 0)
|
|
46
|
+
.map(part => part.trim())
|
|
47
|
+
.join(separator)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isPlainObject(v: unknown): v is Record<string, unknown> {
|
|
51
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A structured postal-address field storing
|
|
56
|
+
* `{ line1?, line2?, city?, region?, postalCode?, country?, lat?, lng? }` as JSON.
|
|
57
|
+
* Unknown keys are stripped, the country code is uppercased, and coordinate ranges are validated.
|
|
58
|
+
*/
|
|
59
|
+
export function address<CollectionTypeInfo extends BaseCollectionTypeInfo>(
|
|
60
|
+
config: AddressFieldConfig<CollectionTypeInfo> = {}
|
|
61
|
+
): FieldTypeFunc<CollectionTypeInfo> {
|
|
62
|
+
const { validation, hooks, ...rest } = config
|
|
63
|
+
|
|
64
|
+
return json<CollectionTypeInfo>({
|
|
65
|
+
...(rest as JsonFieldConfig<CollectionTypeInfo>),
|
|
66
|
+
defaultValue: null,
|
|
67
|
+
hooks: {
|
|
68
|
+
...hooks,
|
|
69
|
+
resolveInput: (args: any) => {
|
|
70
|
+
const value = args.resolvedData[args.fieldKey]
|
|
71
|
+
if (!isPlainObject(value)) return value
|
|
72
|
+
// Strip unknown keys and normalise the country code to uppercase.
|
|
73
|
+
const cleaned: Record<string, unknown> = {}
|
|
74
|
+
for (const key of KNOWN_KEYS) {
|
|
75
|
+
if (value[key] !== undefined) cleaned[key] = value[key]
|
|
76
|
+
}
|
|
77
|
+
if (typeof cleaned.country === 'string') {
|
|
78
|
+
cleaned.country = cleaned.country.toUpperCase()
|
|
79
|
+
}
|
|
80
|
+
return cleaned
|
|
81
|
+
},
|
|
82
|
+
validate: (args: any) => {
|
|
83
|
+
const { resolvedData, fieldKey, addValidationError, operation } = args
|
|
84
|
+
if (operation === 'delete') return
|
|
85
|
+
const value = resolvedData[fieldKey]
|
|
86
|
+
if (value === undefined) return
|
|
87
|
+
if (value === null) {
|
|
88
|
+
if (validation?.isRequired) addValidationError(`${fieldKey} is required`)
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
if (!isPlainObject(value)) {
|
|
92
|
+
addValidationError(`${fieldKey} must be an address object`)
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
const v = value as AddressValue
|
|
96
|
+
|
|
97
|
+
for (const key of STRING_KEYS) {
|
|
98
|
+
if (v[key] !== undefined && typeof v[key] !== 'string') {
|
|
99
|
+
addValidationError(`${fieldKey}.${key} must be a string`)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
for (const key of NUMBER_KEYS) {
|
|
103
|
+
if (v[key] !== undefined && (typeof v[key] !== 'number' || !Number.isFinite(v[key]))) {
|
|
104
|
+
addValidationError(`${fieldKey}.${key} must be a finite number`)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (typeof v.country === 'string' && !/^[A-Z]{2}$/i.test(v.country)) {
|
|
109
|
+
addValidationError(
|
|
110
|
+
`${fieldKey}.country must be a 2-letter ISO-3166 alpha-2 code (e.g. 'NL')`
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
if (typeof v.lat === 'number' && (v.lat < -90 || v.lat > 90)) {
|
|
114
|
+
addValidationError(`${fieldKey}.lat must be between -90 and 90`)
|
|
115
|
+
}
|
|
116
|
+
if (typeof v.lng === 'number' && (v.lng < -180 || v.lng > 180)) {
|
|
117
|
+
addValidationError(`${fieldKey}.lng must be between -180 and 180`)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (validation?.requireCountry && !(typeof v.country === 'string' && v.country.trim())) {
|
|
121
|
+
addValidationError(`${fieldKey}.country is required`)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (validation?.isRequired) {
|
|
125
|
+
const hasLine1AndCity = Boolean(v.line1?.trim()) && Boolean(v.city?.trim())
|
|
126
|
+
if (!hasLine1AndCity && formatAddress(v).length === 0) {
|
|
127
|
+
addValidationError(`${fieldKey} must have at least line1 and city, or some content`)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
})
|
|
133
|
+
}
|