Skip to content

Commit 6d80cf4

Browse files
committed
minor fix and additional documentation
1 parent fd571de commit 6d80cf4

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

.eslintrc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const baseRules = {
1919
'one-var': 'off',
2020
'no-void': 'off',
2121
'multiline-ternary': 'off',
22-
'@typescript-eslint/restrict-template-expressions': ['error', {allowBoolean: true, allNumbers: true }],
2322

2423
// allow console and debugger during development only
2524
'no-console': process.env.NODE_ENV === 'development' ? 'off' : 'error',
@@ -47,7 +46,7 @@ module.exports = {
4746
__statics: 'readonly',
4847
process: 'readonly'
4948
},
50-
49+
rules: baseRules,
5150
overrides: [{
5251
files: ['*.ts', '*.tsx', '*.vue'],
5352

@@ -81,6 +80,7 @@ module.exports = {
8180
// TypeScript
8281
'@typescript-eslint/explicit-function-return-type': 'off',
8382
'@typescript-eslint/explicit-module-boundary-types': 'off',
83+
'@typescript-eslint/restrict-template-expressions': ['error', {allowBoolean: true, allNumbers: true }],
8484
}
8585

8686
}],

src/store/models/index.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ export class InvalidFieldsException extends ParseError {
7070
}
7171
}
7272

73-
7473
// Parsing functions
7574

7675
export function parseNonNegInt(val: string | number) {
@@ -130,6 +129,8 @@ export type generic = string|number|boolean;
130129

131130
export type ParseableModel = Dictionary<generic|Dictionary<generic>>;
132131

132+
// This interface is a general field for a model.
133+
133134
export interface ModelField {
134135
[k: string]: {
135136
field_type: 'string'|'boolean'|'number'|'non_neg_int'|'username'|'email'|'role';
@@ -141,7 +142,7 @@ export interface ModelField {
141142
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access,
142143
@typescript-eslint/no-explicit-any */
143144

144-
export function parseParams(_params: Dictionary<generic>, _fields: ModelField){
145+
export function parseParams(_params: Dictionary<generic | Dictionary<generic>>, _fields: ModelField){
145146
const output_params: Dictionary<generic> = {};
146147
Object.keys(_fields).forEach((key: string) => {
147148
if ((_params as any)[key] == null && _fields[key].default_value !== undefined) {
@@ -169,7 +170,33 @@ export function parseParams(_params: Dictionary<generic>, _fields: ModelField){
169170
/* This creates a general Model to be used for all others (Course, User, etc.)
170171
171172
The original structure of this was from a SO answer at
172-
https://stackoverflow.com/questions/69590729/creating-a-class-using-typescript-with-specific-fields */
173+
https://stackoverflow.com/questions/69590729/creating-a-class-using-typescript-with-specific-fields
174+
175+
This is a factory that will build a new class. It takes 5 arguments. The first is each is an array of strings.
176+
* array of boolean field names
177+
* array of number field names
178+
* array of string field names
179+
* array of dictionary field names
180+
* dictionary of field types (see ModelField above)
181+
182+
To create a new class, extend Model.
183+
184+
For example a User model will be
185+
186+
export class User extends Model(
187+
['is_admin'], ['user_id'], ['username', 'email', 'first_name', 'last_name', 'student_id'], [],
188+
{
189+
username: { field_type: 'username', required: true },
190+
email: { field_type: 'email' },
191+
user_id: { field_type: 'non_neg_int', default_value: 0 },
192+
first_name: { field_type: 'string' },
193+
last_name: { field_type: 'string' },
194+
is_admin: { field_type: 'boolean', default_value: false },
195+
student_id: { field_type: 'string' }
196+
})
197+
)
198+
199+
*/
173200

174201
export const Model = <Bool extends string, Num extends string, Str extends string, Dic extends string,
175202
F extends ModelField>

0 commit comments

Comments
 (0)