import { describe, it, before } from "node:test" import assert from "node:assert" import { PlataModels } from ".." import { PlataGlobalRuntime } from "../libs/runtime" describe('PlataModels', () => { const plataPromise = (async () => { const g = global as any g.Plata = new PlataGlobalRuntime() })() before(async () => { await plataPromise }) const objOkay: typeof modelValidations.type = { enum: 0, stringDateTime: '2022-12-04T02:06:22', dateTime: new Date(), decimal: 99.99, intMin: 5, intMax: 5, intRequired: 1, smallDateTime: '2022-12-04', telefone: '99-999999999', telefoneDDI: '+055 99-999999999', varCharMin: '1', varChar: '1', boolean: false, useModel: { array: [], array_objects: [], object: { param: new Date() }, param: 1, trim: '123', subModel: { required: 1, optional: null }, subModelArray: [] } } const objNotOkay = { enum: 2, stringDateTime: '', dateTime: '', decimal: 5.666666666666, intMin: 1, intMax: 6, intRequired: null, smallDateTime: '', telefone: '', telefoneDDI: '', varCharMin: '', varChar: '11', useModel: {} } const modelStruct = new PlataModels.ModelTemplate({ param: [ PlataModels.Required(), PlataModels.Int() ], object: { param: [ PlataModels.Required(), PlataModels.DateTime() ], }, subModel: [ PlataModels.useModel(new PlataModels.ModelTemplate({ optional: [ PlataModels.Optional(), PlataModels.Int() ], required: [ PlataModels.Required(), PlataModels.Int() ], } as const , {})) ], subModelArray: [[ PlataModels.Required(), PlataModels.useModel(new PlataModels.ModelTemplate({ optional: [ PlataModels.Optional(), PlataModels.Int() ], required: [ PlataModels.Required(), PlataModels.Int() ], } as const , {})) ]], trim: [ PlataModels.VarChar(30) ], array: [ [ PlataModels.VarChar(30) ] ], array_objects: [{ param: [ PlataModels.Optional(), PlataModels.Decimal(10,2) ] }] } as const, {}) modelStruct.trimModel() const modelValidations = new PlataModels.ModelTemplate({ enum: [ PlataModels.Enum([ 0,1 ]) ], stringDateTime: [ PlataModels.StringDateTime() ], dateTime: [ PlataModels.DateTime() ], decimal: [ PlataModels.Decimal(2,2) ], intMin: [ PlataModels.Int(5) ], intMax: [ PlataModels.Int(undefined, 5) ], intRequired: [ PlataModels.Required(), PlataModels.Int() ], smallDateTime: [ PlataModels.SmallDateTime() ], telefone: [ PlataModels.Telefone() ], telefoneDDI: [ PlataModels.TelefoneDDI() ], varCharMin: [ PlataModels.VarChar(1, 1) ], varChar: [ PlataModels.VarChar(1) ], boolean: [ PlataModels.Boolean() ], useModel: [ PlataModels.useModel(modelStruct) ], } as const, {}) it('Struct Filter', () => { const InitObj = { array_objects: [{ param: '123' }], subModelArray: [{ required: 1 }], subModel: {}, trim: ' 123 ', } const FilteredObj = modelStruct.filter(InitObj) assert.deepEqual(FilteredObj, { param: null, object: { param: null, }, subModel: { required: null, optional: null, }, subModelArray: [{ required: 1, optional: null, }], trim: '123', array: [], array_objects: [{ param: '123' }] }) }) it('Validations (Okay)', async () => { const { errors, value } = await modelValidations.validate(objOkay) assert.strictEqual(errors, undefined) assert.deepEqual(value, objOkay) }) it('Validations (Not Okay)', async () => { const { errors, value } = await modelValidations.validate(objNotOkay) assert.notStrictEqual(errors, undefined) assert.strictEqual( errors?.length, Object.keys(modelValidations.template).length, errors?.map(e => `${e.errorID} - ${e.msg}`).join('\n') ) assert.deepEqual(value, objNotOkay) }) })