@nest-my-admin/core 0.7.3 → 0.7.4
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/README.md
CHANGED
|
@@ -328,7 +328,7 @@ export class ProductAdmin extends AdminResourceBase<Product> {
|
|
|
328
328
|
- A widget that does not fit the type (`money` on a boolean) fails at boot.
|
|
329
329
|
- `colors` for badges: gray, red, amber, green, blue, purple or pink.
|
|
330
330
|
- `currency` for money: display only. Money is grouped as you leave the input, and the commas are dropped when saving.
|
|
331
|
-
- **Validation at boot.** Unknown fields, widgets and colours fail at boot with a did-you-mean hint, and so do `showIf`/`slugFrom` names and enum labels for values the column does not have.
|
|
331
|
+
- **Validation at boot.** Unknown fields, widgets and colours fail at boot with a did-you-mean hint, and so do `showIf`/`slugFrom`/`fillFrom` names, defaults that do not fit their field and enum labels for values the column does not have.
|
|
332
332
|
- **Read-only fields.**
|
|
333
333
|
- `readonly: true` shows the field on edit forms as text with a lock and removes it from `form.update`.
|
|
334
334
|
- `readonlyIf(record)` runs on the server for every record. Records carry `_readonly` (the fields it locks), and a PATCH that sends a locked field is a 422 `{ field: ['is read-only'] }` before your `update` runs. That covers inline cell edits too.
|
|
@@ -343,6 +343,8 @@ export class ProductAdmin extends AdminResourceBase<Product> {
|
|
|
343
343
|
- Create ignores it.
|
|
344
344
|
- **`showIf`** hides a field while other fields do not hold the given value (or one of the values), evaluated live in the browser. A hidden field is not sent and not checked. It is presentation, not a permission: the server keeps the stored value.
|
|
345
345
|
- **`slugFrom`** makes the slug widget fill itself from another field. It keeps the letters of every script and stops once someone types in it, or when it already holds a value.
|
|
346
|
+
- **`default`** is the value a new form starts with: an action's input form, or a resource's create form (a duplicate starts from the copied record). It must fit the field (a string for text, enum, date and datetime fields, a number, or `true`/`false`); the DTO still validates whatever is sent.
|
|
347
|
+
- **`fillFrom`** makes a text field fill itself from other fields of the same form, as the user types: `domain: { fillFrom: '{crm}.example.com' }`, `apiDomain: { fillFrom: 'api.{domain}' }` (chains are fine, circles fail at boot). The value is empty while a field it names is empty. It stops following once someone types in it and follows again when they clear it; a field that already holds a value (a stored record) starts as typed. It works in action dialogs too, where `slugFrom` does not.
|
|
346
348
|
- **Layout.**
|
|
347
349
|
- `form.layout` has sections (a title, and 1–3 columns on wide screens, one on phones) and tabs.
|
|
348
350
|
- Every name must be on a form and listed once. Fields left out go into a last section.
|
|
@@ -934,6 +936,10 @@ Things hosts tend to write by hand that the admin already does:
|
|
|
934
936
|
- **Inline children stay out of the sidebar** by default (`nav: false` unless they set `nav`): no extra group to hide them in.
|
|
935
937
|
- **Labels in one language** are plain strings; other languages can come from `translate` (see Languages, theme and branding).
|
|
936
938
|
|
|
939
|
+
## Upgrading to 0.7.4
|
|
940
|
+
|
|
941
|
+
Nothing to change. New: `@AdminField({ default })` starts action input forms and create forms at a value (checked at boot to fit the field), and `@AdminField({ fillFrom: '{crm}.example.com' })` fills a text field from other fields of the same form until someone types in it, in action dialogs too (see Field config).
|
|
942
|
+
|
|
937
943
|
## Upgrading to 0.7.3
|
|
938
944
|
|
|
939
945
|
Nothing to change. Smaller packages: `@nest-my-admin/core`, `/auth` and `/testing` no longer ship source maps (they pointed at sources the packages do not include), so core is about 1.2 MB instead of 1.9 MB; core no longer asks for `rxjs` and `@nest-my-admin/testing` no longer asks for `@nestjs/core`, neither of which they import.
|
package/dist/contract.d.ts
CHANGED
|
@@ -49,6 +49,10 @@ export interface FieldSchema {
|
|
|
49
49
|
showIf?: Record<string, string | number | boolean | null | Array<string | number | boolean | null>>;
|
|
50
50
|
/** The slug widget fills itself from this field until edited. */
|
|
51
51
|
slugFrom?: string;
|
|
52
|
+
/** The value a new form starts with (action inputs and create forms). */
|
|
53
|
+
default?: string | number | boolean;
|
|
54
|
+
/** A template of other fields (`{crm}.example.com`) this text field fills itself from until someone types in it. */
|
|
55
|
+
fillFrom?: string;
|
|
52
56
|
/**
|
|
53
57
|
* `stepper` widget: the steps in order, the values that are not steps (shown as a badge), and whether records carry
|
|
54
58
|
* `_steps` (when each step was reached, from the history child).
|
|
@@ -58,6 +58,18 @@ export interface AdminFieldOptions {
|
|
|
58
58
|
};
|
|
59
59
|
/** The `slug` widget fills itself from this field until someone edits it. */
|
|
60
60
|
slugFrom?: string;
|
|
61
|
+
/**
|
|
62
|
+
* The value a new form starts with: an action's input form or a resource's create form (not a duplicate; settings
|
|
63
|
+
* pages show what is stored). A string for text, enum, date (`YYYY-MM-DD`) and datetime (ISO) fields, a
|
|
64
|
+
* number for number fields, `true`/`false` for booleans. The value is only a starting point: the DTO still validates it.
|
|
65
|
+
*/
|
|
66
|
+
default?: string | number | boolean;
|
|
67
|
+
/**
|
|
68
|
+
* A text field fills itself from other fields of the same form until someone types in it: `'{crm}.example.com'`,
|
|
69
|
+
* `'api.{domain}'` (chains are fine). While a field it names is empty the value is empty too. Clearing the input
|
|
70
|
+
* makes it follow again; a field that already holds a value (a stored record) starts as typed.
|
|
71
|
+
*/
|
|
72
|
+
fillFrom?: string;
|
|
61
73
|
/** The unit shown with the `money` widget (display only): a code such as `USD`, or a unit and its position. Wins over the global `currency`. */
|
|
62
74
|
currency?: string | AdminCurrency;
|
|
63
75
|
/** `file` and `image` widgets: type, size and visibility limits; `richtext`: `maxBytes` and `visibility` of editor images. */
|
|
@@ -105,6 +105,8 @@ export declare function withIdLabel(fields: FieldSchema[], config: Map<string, F
|
|
|
105
105
|
export declare function checkFieldConfig(fields: FieldSchema[], config: Map<string, FieldConfig>, fail: (message: string) => never, allow?: {
|
|
106
106
|
secrets?: boolean;
|
|
107
107
|
}): void;
|
|
108
|
+
/** The names a `fillFrom` template reads: `{crm}.example.com` → `['crm']`. */
|
|
109
|
+
export declare function fillFromNames(template: string): string[];
|
|
108
110
|
/** Fields whose values the audit log never stores: `sensitive`, `secret`, and anything typed into a `password` widget. */
|
|
109
111
|
export declare function sensitiveFields(config: ReadonlyMap<string, FieldConfig>): Set<string>;
|
|
110
112
|
/**
|
|
@@ -136,6 +136,10 @@ export function checkFieldConfig(fields, config, fail, allow = {}) {
|
|
|
136
136
|
if (options.slugFrom !== undefined && !names.includes(options.slugFrom)) {
|
|
137
137
|
fail(`fields.${name}.slugFrom: unknown field "${options.slugFrom}"${didYouMean(options.slugFrom, names)}`);
|
|
138
138
|
}
|
|
139
|
+
if (options.default !== undefined)
|
|
140
|
+
checkDefault(name, field, options, fail);
|
|
141
|
+
if (options.fillFrom !== undefined)
|
|
142
|
+
checkFillFrom(name, field, options, fields, fail);
|
|
139
143
|
if (options.enumLabels && field.enumValues) {
|
|
140
144
|
for (const value of Object.keys(options.enumLabels)) {
|
|
141
145
|
if (!field.enumValues.includes(value))
|
|
@@ -143,6 +147,94 @@ export function checkFieldConfig(fields, config, fail, allow = {}) {
|
|
|
143
147
|
}
|
|
144
148
|
}
|
|
145
149
|
}
|
|
150
|
+
checkFillCycles(config, fail);
|
|
151
|
+
}
|
|
152
|
+
/** The names a `fillFrom` template reads: `{crm}.example.com` → `['crm']`. */
|
|
153
|
+
export function fillFromNames(template) {
|
|
154
|
+
return [...template.matchAll(/\{([^{}]*)\}/g)].map((match) => match[1]);
|
|
155
|
+
}
|
|
156
|
+
const TEXT_TYPES = ['string', 'text', 'uuid', 'other'];
|
|
157
|
+
const NUMBER_TYPES = ['number', 'bigint', 'decimal'];
|
|
158
|
+
const NO_DEFAULT_WIDGETS = ['password', 'file', 'image', 'richtext', 'json'];
|
|
159
|
+
function checkDefault(name, field, options, fail) {
|
|
160
|
+
const value = options.default;
|
|
161
|
+
const where = `fields.${name}.default`;
|
|
162
|
+
if (options.secret)
|
|
163
|
+
fail(`${where}: a \`secret\` field cannot have a default`);
|
|
164
|
+
if (options.widget !== undefined && NO_DEFAULT_WIDGETS.includes(options.widget))
|
|
165
|
+
fail(`${where}: the "${options.widget}" widget takes no default`);
|
|
166
|
+
if (options.fillFrom !== undefined)
|
|
167
|
+
fail(`${where}: a field with \`fillFrom\` fills itself; give it no default`);
|
|
168
|
+
const type = field.type;
|
|
169
|
+
if (TEXT_TYPES.includes(type)) {
|
|
170
|
+
if (typeof value !== 'string')
|
|
171
|
+
fail(`${where}: must be a string for a ${type} field`);
|
|
172
|
+
}
|
|
173
|
+
else if (type === 'enum') {
|
|
174
|
+
if (typeof value !== 'string' || !(field.enumValues ?? []).includes(value))
|
|
175
|
+
fail(`${where}: must be one of ${(field.enumValues ?? []).join(', ')}`);
|
|
176
|
+
}
|
|
177
|
+
else if (NUMBER_TYPES.includes(type)) {
|
|
178
|
+
const ok = (typeof value === 'number' && Number.isFinite(value)) || (type !== 'number' && typeof value === 'string' && /^-?\d+(\.\d+)?$/.test(value));
|
|
179
|
+
if (!ok)
|
|
180
|
+
fail(`${where}: must be a number for a ${type} field`);
|
|
181
|
+
}
|
|
182
|
+
else if (type === 'boolean') {
|
|
183
|
+
if (typeof value !== 'boolean')
|
|
184
|
+
fail(`${where}: must be true or false for a boolean field`);
|
|
185
|
+
}
|
|
186
|
+
else if (type === 'date') {
|
|
187
|
+
if (typeof value !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(value) || Number.isNaN(Date.parse(value)))
|
|
188
|
+
fail(`${where}: must be a date such as "2026-01-31"`);
|
|
189
|
+
}
|
|
190
|
+
else if (type === 'datetime') {
|
|
191
|
+
if (typeof value !== 'string' || Number.isNaN(Date.parse(value)))
|
|
192
|
+
fail(`${where}: must be an ISO date and time such as "2026-01-31T09:30:00Z"`);
|
|
193
|
+
}
|
|
194
|
+
else
|
|
195
|
+
fail(`${where}: ${type} fields take no default`);
|
|
196
|
+
}
|
|
197
|
+
function checkFillFrom(name, field, options, fields, fail) {
|
|
198
|
+
const where = `fields.${name}.fillFrom`;
|
|
199
|
+
const template = options.fillFrom;
|
|
200
|
+
if (typeof template !== 'string')
|
|
201
|
+
fail(`${where}: must be a template such as "{name}.example.com"`);
|
|
202
|
+
if (field.type !== 'string' && field.type !== 'text')
|
|
203
|
+
fail(`${where}: only text fields fill themselves, got ${field.type}`);
|
|
204
|
+
if (options.slugFrom !== undefined)
|
|
205
|
+
fail(`${where}: use either slugFrom or fillFrom`);
|
|
206
|
+
if (options.secret || options.widget === 'password')
|
|
207
|
+
fail(`${where}: a secret or password field cannot fill itself`);
|
|
208
|
+
const used = fillFromNames(template);
|
|
209
|
+
if (used.length === 0)
|
|
210
|
+
fail(`${where}: name at least one field in braces, such as "{name}.example.com"`);
|
|
211
|
+
const names = fields.map((candidate) => candidate.name);
|
|
212
|
+
for (const other of used) {
|
|
213
|
+
if (other === name)
|
|
214
|
+
fail(`${where}: a field cannot fill itself from itself`);
|
|
215
|
+
const source = fields.find((candidate) => candidate.name === other);
|
|
216
|
+
if (!source)
|
|
217
|
+
fail(`${where}: unknown field "${other}"${didYouMean(other, names)}`);
|
|
218
|
+
if (['object', 'relation', 'json'].includes(source.type))
|
|
219
|
+
fail(`${where}: "${other}" is a ${source.type} field; name text, number, enum or date fields`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/** `fillFrom` chains must end: `a` from `{b}` and `b` from `{a}` would never settle. */
|
|
223
|
+
function checkFillCycles(config, fail) {
|
|
224
|
+
const reads = new Map([...config].filter(([, options]) => typeof options.fillFrom === 'string').map(([name, options]) => [name, fillFromNames(options.fillFrom)]));
|
|
225
|
+
const state = new Map();
|
|
226
|
+
const visit = (name, path) => {
|
|
227
|
+
if (state.get(name) === 'done' || !reads.has(name))
|
|
228
|
+
return;
|
|
229
|
+
if (state.get(name) === 'open')
|
|
230
|
+
fail(`fields.${name}.fillFrom: the fields fill each other in a circle (${[...path, name].join(' → ')})`);
|
|
231
|
+
state.set(name, 'open');
|
|
232
|
+
for (const next of reads.get(name))
|
|
233
|
+
visit(next, [...path, name]);
|
|
234
|
+
state.set(name, 'done');
|
|
235
|
+
};
|
|
236
|
+
for (const name of reads.keys())
|
|
237
|
+
visit(name, []);
|
|
146
238
|
}
|
|
147
239
|
function checkFileOptions(name, field, options, fail) {
|
|
148
240
|
const isFile = options.widget === 'file' || options.widget === 'image';
|
|
@@ -197,6 +289,10 @@ export function applyFieldConfig(field, options, fieldText, defaultMaxBytes = DE
|
|
|
197
289
|
out.showIf = { ...options.showIf };
|
|
198
290
|
if (options.slugFrom !== undefined)
|
|
199
291
|
out.slugFrom = options.slugFrom;
|
|
292
|
+
if (options.default !== undefined)
|
|
293
|
+
out.default = options.default;
|
|
294
|
+
if (options.fillFrom !== undefined)
|
|
295
|
+
out.fillFrom = options.fillFrom;
|
|
200
296
|
if (options.warnIf !== undefined)
|
|
201
297
|
out.warnIf = { empty: true, message: text(options.warnIf.message, 'warnIf.message') };
|
|
202
298
|
if (options.widget === 'stepper') {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nest-my-admin/core",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.4",
|
|
4
4
|
"description": "Django-admin-class admin panel for NestJS + TypeORM",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@nest-my-admin/ui": "0.7.
|
|
38
|
+
"@nest-my-admin/ui": "0.7.4"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"@nestjs/common": "^11.0.0 || ^12.0.0",
|