@@ -70,7 +70,6 @@ export class InvalidFieldsException extends ParseError {
7070 }
7171}
7272
73-
7473// Parsing functions
7574
7675export function parseNonNegInt ( val : string | number ) {
@@ -130,6 +129,8 @@ export type generic = string|number|boolean;
130129
131130export type ParseableModel = Dictionary < generic | Dictionary < generic > > ;
132131
132+ // This interface is a general field for a model.
133+
133134export 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
171172The 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
174201export const Model = < Bool extends string , Num extends string , Str extends string , Dic extends string ,
175202 F extends ModelField >
0 commit comments