Skip to content

Commit cd47a39

Browse files
author
VuXfi
committed
refactor: update Settings model to use env method for ALLOW_UNSAFE_SETTINGS and disable jsonSchema validation
1 parent 511e026 commit cd47a39

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

libs/adminpanel/ProductCatalog/ProductCatalog.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,17 @@ export class Group<GroupProductItem extends Item> extends BaseModelItem<GroupPro
116116
data: {
117117
model: this.model,
118118
labels: {
119+
//@ts-ignore
119120
title: req.i18n.__('Add Group'),
121+
//@ts-ignore
120122
save: req.i18n.__('Save'),
121123
},
122124
},
123125
};
124126
}
125127

126128
async getEditTemplate(id: string | number, catalogId: string, req: any): Promise<any> {
127-
const item = await this.find(id);
129+
const item = await this.find(id, catalogId);
128130
return {
129131
type: 'model',
130132
data: {
@@ -158,7 +160,9 @@ export class Product<T extends Item> extends BaseModelItem<T> {
158160
data: {
159161
model: this.model,
160162
labels: {
163+
//@ts-ignore
161164
title: req.i18n.__('Add Product'),
165+
//@ts-ignore
162166
save: req.i18n.__('Save'),
163167
}
164168
}
@@ -176,12 +180,19 @@ export class Product<T extends Item> extends BaseModelItem<T> {
176180
modelId: item.id
177181
},
178182
labels: {
183+
//@ts-ignore
179184
title: req.i18n.__('Add Product'),
185+
//@ts-ignore
180186
save: req.i18n.__('Save'),
181187
}
182188
}
183189
})
184190
}
191+
192+
public updateModelItems(modelId: string | number, data: any, catalogId: string): Promise<T> {
193+
// For Product, no specific update needed for navigation items
194+
return Promise.resolve(null as T);
195+
}
185196
}
186197

187198
export class ProductCatalog extends AbstractCatalog {

models/Settings.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ let Model = {
9797
try {
9898
value = JSON.parse(process.env[key]);
9999
// if value was parsed, check that given json matches the schema (if !ALLOW_UNSAFE_SETTINGS)
100-
if (!(await Settings.get("ALLOW_UNSAFE_SETTINGS") ?? false)) {
100+
if (!(Settings.env("ALLOW_UNSAFE_SETTINGS") ?? false)) {
101101
const ajv = new ajv_1.default();
102102
const validate = ajv.compile(setting.jsonSchema);
103103
if (!validate(value)) {
@@ -137,6 +137,9 @@ let Model = {
137137
// sails.log.error(`Settings get: Requested setting [${key}] was not declared by specification`);
138138
// return;
139139
// }
140+
// if(process.env[key] !== undefined) {
141+
// return cleanValue(process.env[key])
142+
// }
140143
if (settings[_key] !== undefined) {
141144
//@ts-ignore
142145
return cleanValue(settings[_key]);
@@ -232,7 +235,7 @@ let Model = {
232235
}
233236
}
234237
// check that value and defaultValue match the schema for json type (if !ALLOW_UNSAFE_SETTINGS)
235-
if (settingType === "json" && !(await Settings.get("ALLOW_UNSAFE_SETTINGS"))) {
238+
if (settingType === "json" && !Settings.env("ALLOW_UNSAFE_SETTINGS")) {
236239
const ajv = new ajv_1.default();
237240
const validate = ajv.compile(settingsSetInput.jsonSchema);
238241
// undefined if value is from input, null if value is from origSettings
@@ -289,6 +292,22 @@ let Model = {
289292
sails.log.error("CORE > Settings > set DB error: ", settingsSetInput, e);
290293
throw `Error Set settings in DB`;
291294
}
295+
},
296+
env(key) {
297+
const envValue = process.env[key];
298+
if (envValue === undefined) {
299+
return undefined;
300+
}
301+
// For ALLOW_UNSAFE_SETTINGS, we know it's boolean
302+
if (key === "ALLOW_UNSAFE_SETTINGS") {
303+
return ["yes", "YES", "Yes", "1", "true", "TRUE", "True"].includes(envValue);
304+
}
305+
// For other keys, try to parse as JSON, fallback to string
306+
try {
307+
return JSON.parse(envValue);
308+
} catch {
309+
return envValue;
310+
}
292311
}
293312
};
294313
module.exports = {

models/Settings.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ let Model = {
135135
value = JSON.parse(process.env[key]);
136136

137137
// if value was parsed, check that given json matches the schema (if !ALLOW_UNSAFE_SETTINGS)
138-
if (!(await Settings.get("ALLOW_UNSAFE_SETTINGS") ?? false)) {
138+
if (!(Settings.env("ALLOW_UNSAFE_SETTINGS") ?? false)) {
139139
const ajv = new Ajv();
140140
const validate = ajv.compile(setting.jsonSchema);
141141
if (!validate(value)) {
@@ -283,7 +283,7 @@ let Model = {
283283
}
284284

285285
// check that value and defaultValue match the schema for json type (if !ALLOW_UNSAFE_SETTINGS)
286-
if (settingType === "json" && !(await Settings.get("ALLOW_UNSAFE_SETTINGS"))) {
286+
if (false) { // Disabled jsonSchema validation in Settings.set, similar to Setting mode
287287
const ajv = new Ajv();
288288
const validate = ajv.compile(settingsSetInput.jsonSchema);
289289

@@ -342,6 +342,23 @@ let Model = {
342342
sails.log.error("CORE > Settings > set DB error: ", settingsSetInput, e);
343343
throw `Error Set settings in DB`
344344
}
345+
},
346+
347+
env<K extends keyof SettingList>(key: K): SettingList[K] | undefined {
348+
const envValue = process.env[key as string];
349+
if (envValue === undefined) {
350+
return undefined;
351+
}
352+
// For ALLOW_UNSAFE_SETTINGS, we know it's boolean
353+
if (key === "ALLOW_UNSAFE_SETTINGS") {
354+
return (["yes", "YES", "Yes", "1", "true", "TRUE", "True"].includes(envValue)) as SettingList[K];
355+
}
356+
// For other keys, try to parse as JSON, fallback to string
357+
try {
358+
return JSON.parse(envValue) as SettingList[K];
359+
} catch {
360+
return envValue as SettingList[K];
361+
}
345362
}
346363
};
347364

0 commit comments

Comments
 (0)