pwi-plata-type 0.4.165 → 0.4.166
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 +2 -2
- package/__BUILD__/libs/sql.js +11 -4
- package/__BUILD__/libs/sql.js.map +1 -1
- package/package.json +53 -53
- package/src/@types/build.d.ts +26 -26
- package/src/@types/cluster.actions.d.ts +27 -27
- package/src/@types/cluster.d.ts +55 -55
- package/src/@types/create-cli.d.ts +8 -8
- package/src/@types/exported.d.ts +31 -31
- package/src/@types/fs.d.ts +6 -6
- package/src/@types/global.d.ts +113 -113
- package/src/@types/knex.d.ts +13 -13
- package/src/@types/model.d.ts +143 -143
- package/src/@types/router.d.ts +107 -107
- package/src/@types/sql.d.ts +75 -75
- package/src/@types/swagger.d.ts +22 -22
- package/src/bin/extras/create-api-cli.ts +218 -218
- package/src/bin/extras/create-cluster-cli.ts +160 -160
- package/src/bin/extras/create-lib-cli.ts +84 -84
- package/src/bin/extras/plata-create-tools.ts +97 -97
- package/src/bin/plata-api.ts +189 -189
- package/src/bin/plata-build.ts +17 -17
- package/src/bin/plata-cluster.ts +42 -42
- package/src/bin/plata-cmd.ts +18 -18
- package/src/bin/plata-create.ts +49 -49
- package/src/bin/plata-runtime.ts +131 -131
- package/src/bin/plata-swagger.ts +99 -99
- package/src/bin/runtime/_setupRuntime.ts +48 -48
- package/src/bin/runtime/_types.d.ts +7 -7
- package/src/bin/runtime/class.ts +20 -20
- package/src/bin/runtime/dirs.ts +25 -25
- package/src/bin/runtime/effect.ts +60 -60
- package/src/bin/runtime/fs.ts +10 -10
- package/src/bin/runtime/promises.ts +18 -18
- package/src/bin/runtime/require.ts +113 -113
- package/src/bin/runtime/sql.ts +12 -12
- package/src/bin/runtime/switch.ts +60 -60
- package/src/index.ts +19 -19
- package/src/libs/axios.ts +51 -51
- package/src/libs/build.ts +162 -162
- package/src/libs/cluster/cluster-controller.ts +93 -93
- package/src/libs/cluster.ts +330 -330
- package/src/libs/custom.ts +15 -15
- package/src/libs/env.ts +10 -10
- package/src/libs/fs.ts +399 -399
- package/src/libs/hot-reload.ts +63 -63
- package/src/libs/models/index.ts +4 -4
- package/src/libs/models/model-template-combine.ts +93 -93
- package/src/libs/models/model-template-env.ts +58 -58
- package/src/libs/models/model-template-union.ts +83 -83
- package/src/libs/models/model-template.ts +107 -107
- package/src/libs/models/model-validations.ts +443 -443
- package/src/libs/models/tools.ts +194 -194
- package/src/libs/router.ts +185 -185
- package/src/libs/sql.ts +788 -778
- package/src/libs/swagger.ts +81 -81
- package/src/libs/tools.ts +240 -240
- package/src/scripts/post-install/api.ts +32 -32
- package/src/scripts/post-install/cluster.ts +28 -28
- package/src/scripts/post-install/lib.ts +25 -25
- package/src/scripts/post-install.ts +64 -64
- package/src/scripts/pre-test.ts +33 -33
- package/src/tests/assets/sql/example.sql +76 -76
- package/src/tests/config.test.ts +12 -12
- package/src/tests/effect.test.ts +62 -62
- package/src/tests/model.env.test.ts +66 -66
- package/src/tests/model.test.ts +141 -141
- package/src/tests/model.union.test.ts +160 -160
- package/src/tests/router.test.ts +44 -44
- package/src/tests/sql.test.ts +502 -502
- package/src/tests/switch.test.ts +76 -76
- package/templates/create-cli/class.ts +13 -13
- package/templates/create-cli/cluster.ts +14 -14
- package/templates/create-cli/cmd.ts +3 -3
- package/templates/create-cli/config.ts +2 -2
- package/templates/create-cli/lib.ts +3 -3
- package/templates/create-cli/model.ts +14 -14
- package/templates/create-cli/route.ts +8 -8
- package/templates/create-cli/task.ts +13 -13
- package/templates/postinstall/api/.vscode/launch.json +20 -20
- package/templates/postinstall/api/Dockerfile +24 -24
- package/templates/postinstall/api/configs/env.ts +2 -2
- package/templates/postinstall/api/envs/debug.env +15 -15
- package/templates/postinstall/api/envs/homolog.env +15 -15
- package/templates/postinstall/api/envs/prod.env +15 -15
- package/templates/postinstall/api/tsconfig.json +24 -24
- package/templates/postinstall/cluster/.vscode/launch.json +20 -20
- package/templates/postinstall/cluster/Dockerfile +24 -24
- package/templates/postinstall/cluster/configs/env.ts +2 -2
- package/templates/postinstall/cluster/envs/debug.env +10 -10
- package/templates/postinstall/cluster/envs/homolog.env +10 -10
- package/templates/postinstall/cluster/envs/prod.env +10 -10
- package/templates/postinstall/cluster/tsconfig.json +24 -24
- package/templates/postinstall/lib/postinstall-tools.mjs +111 -111
- package/templates/postinstall/lib/postinstall.js +33 -33
- package/templates/postinstall/lib/tsconfig.json +22 -22
- package/templates/swagger-cli/header.json +21 -21
- package/templates/swagger-cli/route/body.json +14 -14
- package/templates/swagger-cli/route/header.json +18 -18
- package/templates/swagger-cli/route/response.json +53 -53
- package/tsconfig.json +28 -28
package/src/tests/effect.test.ts
CHANGED
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
import { describe, it, before } from "node:test"
|
|
2
|
-
import assert from "node:assert"
|
|
3
|
-
import { loadRuntimeLibs } from "../bin/runtime/_setupRuntime"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
describe('PromiseAll', () => {
|
|
7
|
-
const plataPromise = (async () => {
|
|
8
|
-
const g = global as any
|
|
9
|
-
|
|
10
|
-
g.Plata = await loadRuntimeLibs()
|
|
11
|
-
})()
|
|
12
|
-
|
|
13
|
-
before(async () => {
|
|
14
|
-
await plataPromise
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
it('should resolve an array of promises', async () => {
|
|
18
|
-
const promises = [
|
|
19
|
-
Plata.FastPromise(async (): Promise<1> => 1),
|
|
20
|
-
Plata.FastPromise(async (): Promise<2> => 2),
|
|
21
|
-
Plata.FastPromise(async (): Promise<3> => 3),
|
|
22
|
-
] as const
|
|
23
|
-
|
|
24
|
-
const result = await Plata.PromiseAll(promises)
|
|
25
|
-
|
|
26
|
-
assert.strictEqual(result.errorID, undefined)
|
|
27
|
-
|
|
28
|
-
const type: [ 1, 2, 3 ] = result
|
|
29
|
-
assert.deepStrictEqual(result, [1, 2, 3])
|
|
30
|
-
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
it('should return an empty array if input is an empty array', async () => {
|
|
34
|
-
const promises: [] = []
|
|
35
|
-
|
|
36
|
-
const result = await Plata.PromiseAll(promises)
|
|
37
|
-
|
|
38
|
-
assert.strictEqual(result.errorID, undefined)
|
|
39
|
-
|
|
40
|
-
const type: unknown[] = result
|
|
41
|
-
assert.deepStrictEqual(result, [])
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
it('should handle promises that reject', async () => {
|
|
45
|
-
const promises = [
|
|
46
|
-
Plata.FastPromise(() => Promise.resolve(1)),
|
|
47
|
-
Plata.FastPromise(() => Promise.resolve(Plata.BuildPlataError({
|
|
48
|
-
errorID: 'a',
|
|
49
|
-
msg: '',
|
|
50
|
-
error: ''
|
|
51
|
-
}))),
|
|
52
|
-
Plata.FastPromise(() => Promise.resolve(3)),
|
|
53
|
-
]
|
|
54
|
-
|
|
55
|
-
const result = await Plata.PromiseAll(promises)
|
|
56
|
-
|
|
57
|
-
if (result.errorID === undefined) {
|
|
58
|
-
const type: number[] = result
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
assert.strictEqual(result.errorID, 'a')
|
|
62
|
-
})
|
|
1
|
+
import { describe, it, before } from "node:test"
|
|
2
|
+
import assert from "node:assert"
|
|
3
|
+
import { loadRuntimeLibs } from "../bin/runtime/_setupRuntime"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
describe('PromiseAll', () => {
|
|
7
|
+
const plataPromise = (async () => {
|
|
8
|
+
const g = global as any
|
|
9
|
+
|
|
10
|
+
g.Plata = await loadRuntimeLibs()
|
|
11
|
+
})()
|
|
12
|
+
|
|
13
|
+
before(async () => {
|
|
14
|
+
await plataPromise
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('should resolve an array of promises', async () => {
|
|
18
|
+
const promises = [
|
|
19
|
+
Plata.FastPromise(async (): Promise<1> => 1),
|
|
20
|
+
Plata.FastPromise(async (): Promise<2> => 2),
|
|
21
|
+
Plata.FastPromise(async (): Promise<3> => 3),
|
|
22
|
+
] as const
|
|
23
|
+
|
|
24
|
+
const result = await Plata.PromiseAll(promises)
|
|
25
|
+
|
|
26
|
+
assert.strictEqual(result.errorID, undefined)
|
|
27
|
+
|
|
28
|
+
const type: [ 1, 2, 3 ] = result
|
|
29
|
+
assert.deepStrictEqual(result, [1, 2, 3])
|
|
30
|
+
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should return an empty array if input is an empty array', async () => {
|
|
34
|
+
const promises: [] = []
|
|
35
|
+
|
|
36
|
+
const result = await Plata.PromiseAll(promises)
|
|
37
|
+
|
|
38
|
+
assert.strictEqual(result.errorID, undefined)
|
|
39
|
+
|
|
40
|
+
const type: unknown[] = result
|
|
41
|
+
assert.deepStrictEqual(result, [])
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('should handle promises that reject', async () => {
|
|
45
|
+
const promises = [
|
|
46
|
+
Plata.FastPromise(() => Promise.resolve(1)),
|
|
47
|
+
Plata.FastPromise(() => Promise.resolve(Plata.BuildPlataError({
|
|
48
|
+
errorID: 'a',
|
|
49
|
+
msg: '',
|
|
50
|
+
error: ''
|
|
51
|
+
}))),
|
|
52
|
+
Plata.FastPromise(() => Promise.resolve(3)),
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
const result = await Plata.PromiseAll(promises)
|
|
56
|
+
|
|
57
|
+
if (result.errorID === undefined) {
|
|
58
|
+
const type: number[] = result
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
assert.strictEqual(result.errorID, 'a')
|
|
62
|
+
})
|
|
63
63
|
})
|
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
import { describe, it, before } from "node:test"
|
|
2
|
-
import assert from "node:assert"
|
|
3
|
-
import { loadRuntimeLibs } from "../bin/runtime/_setupRuntime"
|
|
4
|
-
import { PlataModels } from ".."
|
|
5
|
-
|
|
6
|
-
describe('PlataModels.env', () => {
|
|
7
|
-
const plataPromise = (async () => {
|
|
8
|
-
const g = global as any
|
|
9
|
-
|
|
10
|
-
g.Plata = await loadRuntimeLibs()
|
|
11
|
-
})()
|
|
12
|
-
|
|
13
|
-
before(async () => {
|
|
14
|
-
await plataPromise
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
const model = new PlataModels.ModelTemplateEnv({
|
|
18
|
-
ENV_enum: [ PlataModels.Enum([ 0, 1 ]) ],
|
|
19
|
-
ENV_decimal: [ PlataModels.Decimal(2,2) ],
|
|
20
|
-
ENV_intMin: [ PlataModels.Int(5) ],
|
|
21
|
-
ENV_intMax: [ PlataModels.Int(undefined, 5) ],
|
|
22
|
-
ENV_intRequired: [ PlataModels.Required(), PlataModels.Int() ],
|
|
23
|
-
ENV_smallDateTime: [ PlataModels.SmallDateTime() ],
|
|
24
|
-
ENV_varChar: [ PlataModels.VarChar(1) ],
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
const getOkayObj = () => ({
|
|
28
|
-
ENV_enum: `${0}`,
|
|
29
|
-
ENV_decimal: `${22.22}`,
|
|
30
|
-
ENV_intMin: `${5}`,
|
|
31
|
-
ENV_intMax: `${5}`,
|
|
32
|
-
ENV_intRequired: `${6}`,
|
|
33
|
-
ENV_smallDateTime: '2022-12-04',
|
|
34
|
-
ENV_varChar: '1',
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
const getNotOkayObj = () => ({
|
|
38
|
-
ENV_enum: `${2}`,
|
|
39
|
-
ENV_decimal: `${222.222}`,
|
|
40
|
-
ENV_intMin: `${4}`,
|
|
41
|
-
ENV_intMax: `${6}`,
|
|
42
|
-
ENV_smallDateTime: '2022-12--04',
|
|
43
|
-
ENV_varChar: '12',
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
it('Validations (Okay)', async () => {
|
|
47
|
-
const okayObj = getOkayObj()
|
|
48
|
-
|
|
49
|
-
const value = await model.validate(okayObj)
|
|
50
|
-
|
|
51
|
-
assert.strictEqual(value.errors, undefined)
|
|
52
|
-
assert.deepEqual(value.value, okayObj)
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
it('Validations (Not Okay)', async () => {
|
|
56
|
-
const notOkayObj = getNotOkayObj()
|
|
57
|
-
|
|
58
|
-
const value = await model.validate(notOkayObj)
|
|
59
|
-
|
|
60
|
-
assert.notStrictEqual(value.errors, undefined)
|
|
61
|
-
assert.strictEqual(
|
|
62
|
-
value.errors?.length,
|
|
63
|
-
Object.keys(model.template).length,
|
|
64
|
-
value.errors?.map(e => `${e.errorID} - ${e.msg}`).join('\n')
|
|
65
|
-
)
|
|
66
|
-
})
|
|
1
|
+
import { describe, it, before } from "node:test"
|
|
2
|
+
import assert from "node:assert"
|
|
3
|
+
import { loadRuntimeLibs } from "../bin/runtime/_setupRuntime"
|
|
4
|
+
import { PlataModels } from ".."
|
|
5
|
+
|
|
6
|
+
describe('PlataModels.env', () => {
|
|
7
|
+
const plataPromise = (async () => {
|
|
8
|
+
const g = global as any
|
|
9
|
+
|
|
10
|
+
g.Plata = await loadRuntimeLibs()
|
|
11
|
+
})()
|
|
12
|
+
|
|
13
|
+
before(async () => {
|
|
14
|
+
await plataPromise
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
const model = new PlataModels.ModelTemplateEnv({
|
|
18
|
+
ENV_enum: [ PlataModels.Enum([ 0, 1 ]) ],
|
|
19
|
+
ENV_decimal: [ PlataModels.Decimal(2,2) ],
|
|
20
|
+
ENV_intMin: [ PlataModels.Int(5) ],
|
|
21
|
+
ENV_intMax: [ PlataModels.Int(undefined, 5) ],
|
|
22
|
+
ENV_intRequired: [ PlataModels.Required(), PlataModels.Int() ],
|
|
23
|
+
ENV_smallDateTime: [ PlataModels.SmallDateTime() ],
|
|
24
|
+
ENV_varChar: [ PlataModels.VarChar(1) ],
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const getOkayObj = () => ({
|
|
28
|
+
ENV_enum: `${0}`,
|
|
29
|
+
ENV_decimal: `${22.22}`,
|
|
30
|
+
ENV_intMin: `${5}`,
|
|
31
|
+
ENV_intMax: `${5}`,
|
|
32
|
+
ENV_intRequired: `${6}`,
|
|
33
|
+
ENV_smallDateTime: '2022-12-04',
|
|
34
|
+
ENV_varChar: '1',
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const getNotOkayObj = () => ({
|
|
38
|
+
ENV_enum: `${2}`,
|
|
39
|
+
ENV_decimal: `${222.222}`,
|
|
40
|
+
ENV_intMin: `${4}`,
|
|
41
|
+
ENV_intMax: `${6}`,
|
|
42
|
+
ENV_smallDateTime: '2022-12--04',
|
|
43
|
+
ENV_varChar: '12',
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('Validations (Okay)', async () => {
|
|
47
|
+
const okayObj = getOkayObj()
|
|
48
|
+
|
|
49
|
+
const value = await model.validate(okayObj)
|
|
50
|
+
|
|
51
|
+
assert.strictEqual(value.errors, undefined)
|
|
52
|
+
assert.deepEqual(value.value, okayObj)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('Validations (Not Okay)', async () => {
|
|
56
|
+
const notOkayObj = getNotOkayObj()
|
|
57
|
+
|
|
58
|
+
const value = await model.validate(notOkayObj)
|
|
59
|
+
|
|
60
|
+
assert.notStrictEqual(value.errors, undefined)
|
|
61
|
+
assert.strictEqual(
|
|
62
|
+
value.errors?.length,
|
|
63
|
+
Object.keys(model.template).length,
|
|
64
|
+
value.errors?.map(e => `${e.errorID} - ${e.msg}`).join('\n')
|
|
65
|
+
)
|
|
66
|
+
})
|
|
67
67
|
})
|
package/src/tests/model.test.ts
CHANGED
|
@@ -1,142 +1,142 @@
|
|
|
1
|
-
import { describe, it, before } from "node:test"
|
|
2
|
-
import assert from "node:assert"
|
|
3
|
-
import { PlataModels } from ".."
|
|
4
|
-
import { loadRuntimeLibs } from "../bin/runtime/_setupRuntime"
|
|
5
|
-
|
|
6
|
-
describe('PlataModels', () => {
|
|
7
|
-
const plataPromise = (async () => {
|
|
8
|
-
const g = global as any
|
|
9
|
-
|
|
10
|
-
g.Plata = await loadRuntimeLibs()
|
|
11
|
-
})()
|
|
12
|
-
|
|
13
|
-
before(async () => {
|
|
14
|
-
await plataPromise
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
const objOkay: typeof modelValidations.type = {
|
|
18
|
-
enum: 0,
|
|
19
|
-
stringDateTime: '2022-12-04T02:06:22',
|
|
20
|
-
dateTime: new Date(),
|
|
21
|
-
decimal: 99.99,
|
|
22
|
-
intMin: 5,
|
|
23
|
-
intMax: 5,
|
|
24
|
-
intRequired: 1,
|
|
25
|
-
smallDateTime: '2022-12-04',
|
|
26
|
-
telefone: '99-999999999',
|
|
27
|
-
telefoneDDI: '+055 99-999999999',
|
|
28
|
-
varCharMin: '1',
|
|
29
|
-
varChar: '1',
|
|
30
|
-
useModel: {
|
|
31
|
-
array: [],
|
|
32
|
-
array_objects: [],
|
|
33
|
-
object: {
|
|
34
|
-
param: new Date()
|
|
35
|
-
},
|
|
36
|
-
param: 1,
|
|
37
|
-
subModel: {
|
|
38
|
-
required: 1,
|
|
39
|
-
optional: null
|
|
40
|
-
},
|
|
41
|
-
subModelArray: []
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const objNotOkay = {
|
|
46
|
-
enum: 2,
|
|
47
|
-
stringDateTime: '',
|
|
48
|
-
dateTime: '',
|
|
49
|
-
decimal: 5.666666666666,
|
|
50
|
-
intMin: 1,
|
|
51
|
-
intMax: 6,
|
|
52
|
-
intRequired: null,
|
|
53
|
-
smallDateTime: '',
|
|
54
|
-
telefone: '',
|
|
55
|
-
telefoneDDI: '',
|
|
56
|
-
varCharMin: '',
|
|
57
|
-
varChar: '11',
|
|
58
|
-
useModel: {}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const modelStruct = new PlataModels.ModelTemplate({
|
|
62
|
-
param: [ PlataModels.Required(), PlataModels.Int() ],
|
|
63
|
-
object: {
|
|
64
|
-
param: [ PlataModels.Required(), PlataModels.DateTime() ],
|
|
65
|
-
},
|
|
66
|
-
subModel: [ PlataModels.useModel(new PlataModels.ModelTemplate({
|
|
67
|
-
optional: [ PlataModels.Optional(), PlataModels.Int() ],
|
|
68
|
-
required: [ PlataModels.Required(), PlataModels.Int() ],
|
|
69
|
-
} as const , {})) ],
|
|
70
|
-
subModelArray: [[ PlataModels.useModel(new PlataModels.ModelTemplate({
|
|
71
|
-
optional: [ PlataModels.Optional(), PlataModels.Int() ],
|
|
72
|
-
required: [ PlataModels.Required(), PlataModels.Int() ],
|
|
73
|
-
} as const , {})) ]],
|
|
74
|
-
array: [ [ PlataModels.VarChar(30) ] ],
|
|
75
|
-
array_objects: [{
|
|
76
|
-
param: [ PlataModels.Optional(), PlataModels.Decimal(10,2) ]
|
|
77
|
-
}]
|
|
78
|
-
} as const, {})
|
|
79
|
-
|
|
80
|
-
const modelValidations = new PlataModels.ModelTemplate({
|
|
81
|
-
enum: [ PlataModels.Enum([ 0,1 ]) ],
|
|
82
|
-
stringDateTime: [ PlataModels.StringDateTime() ],
|
|
83
|
-
dateTime: [ PlataModels.DateTime() ],
|
|
84
|
-
decimal: [ PlataModels.Decimal(2,2) ],
|
|
85
|
-
intMin: [ PlataModels.Int(5) ],
|
|
86
|
-
intMax: [ PlataModels.Int(undefined, 5) ],
|
|
87
|
-
intRequired: [ PlataModels.Required(), PlataModels.Int() ],
|
|
88
|
-
smallDateTime: [ PlataModels.SmallDateTime() ],
|
|
89
|
-
telefone: [ PlataModels.Telefone() ],
|
|
90
|
-
telefoneDDI: [ PlataModels.TelefoneDDI() ],
|
|
91
|
-
varCharMin: [ PlataModels.VarChar(1, 1) ],
|
|
92
|
-
varChar: [ PlataModels.VarChar(1) ],
|
|
93
|
-
useModel: [ PlataModels.useModel(modelStruct) ],
|
|
94
|
-
} as const, {})
|
|
95
|
-
|
|
96
|
-
it('Struct Filter', () => {
|
|
97
|
-
const InitObj = {
|
|
98
|
-
array_objects: [{ param: '123' }],
|
|
99
|
-
subModelArray: [{ required: 1 }]
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const FilteredObj = modelStruct.filter(InitObj)
|
|
103
|
-
|
|
104
|
-
assert.deepEqual(FilteredObj, {
|
|
105
|
-
param: null,
|
|
106
|
-
object: {
|
|
107
|
-
param: null,
|
|
108
|
-
},
|
|
109
|
-
subModel: {
|
|
110
|
-
required: null,
|
|
111
|
-
optional: null,
|
|
112
|
-
},
|
|
113
|
-
subModelArray: [{
|
|
114
|
-
required: 1,
|
|
115
|
-
optional: null,
|
|
116
|
-
}],
|
|
117
|
-
array: [],
|
|
118
|
-
array_objects: [{
|
|
119
|
-
param: '123'
|
|
120
|
-
}]
|
|
121
|
-
})
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
it('Validations (Okay)', async () => {
|
|
125
|
-
const { errors, value } = await modelValidations.validate(objOkay)
|
|
126
|
-
|
|
127
|
-
assert.strictEqual(errors, undefined)
|
|
128
|
-
assert.deepEqual(value, objOkay)
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
it('Validations (Not Okay)', async () => {
|
|
132
|
-
const { errors, value } = await modelValidations.validate(objNotOkay)
|
|
133
|
-
|
|
134
|
-
assert.notStrictEqual(errors, undefined)
|
|
135
|
-
assert.strictEqual(
|
|
136
|
-
errors?.length,
|
|
137
|
-
Object.keys(modelValidations.template).length,
|
|
138
|
-
errors?.map(e => `${e.errorID} - ${e.msg}`).join('\n')
|
|
139
|
-
)
|
|
140
|
-
assert.deepEqual(value, objNotOkay)
|
|
141
|
-
})
|
|
1
|
+
import { describe, it, before } from "node:test"
|
|
2
|
+
import assert from "node:assert"
|
|
3
|
+
import { PlataModels } from ".."
|
|
4
|
+
import { loadRuntimeLibs } from "../bin/runtime/_setupRuntime"
|
|
5
|
+
|
|
6
|
+
describe('PlataModels', () => {
|
|
7
|
+
const plataPromise = (async () => {
|
|
8
|
+
const g = global as any
|
|
9
|
+
|
|
10
|
+
g.Plata = await loadRuntimeLibs()
|
|
11
|
+
})()
|
|
12
|
+
|
|
13
|
+
before(async () => {
|
|
14
|
+
await plataPromise
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
const objOkay: typeof modelValidations.type = {
|
|
18
|
+
enum: 0,
|
|
19
|
+
stringDateTime: '2022-12-04T02:06:22',
|
|
20
|
+
dateTime: new Date(),
|
|
21
|
+
decimal: 99.99,
|
|
22
|
+
intMin: 5,
|
|
23
|
+
intMax: 5,
|
|
24
|
+
intRequired: 1,
|
|
25
|
+
smallDateTime: '2022-12-04',
|
|
26
|
+
telefone: '99-999999999',
|
|
27
|
+
telefoneDDI: '+055 99-999999999',
|
|
28
|
+
varCharMin: '1',
|
|
29
|
+
varChar: '1',
|
|
30
|
+
useModel: {
|
|
31
|
+
array: [],
|
|
32
|
+
array_objects: [],
|
|
33
|
+
object: {
|
|
34
|
+
param: new Date()
|
|
35
|
+
},
|
|
36
|
+
param: 1,
|
|
37
|
+
subModel: {
|
|
38
|
+
required: 1,
|
|
39
|
+
optional: null
|
|
40
|
+
},
|
|
41
|
+
subModelArray: []
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const objNotOkay = {
|
|
46
|
+
enum: 2,
|
|
47
|
+
stringDateTime: '',
|
|
48
|
+
dateTime: '',
|
|
49
|
+
decimal: 5.666666666666,
|
|
50
|
+
intMin: 1,
|
|
51
|
+
intMax: 6,
|
|
52
|
+
intRequired: null,
|
|
53
|
+
smallDateTime: '',
|
|
54
|
+
telefone: '',
|
|
55
|
+
telefoneDDI: '',
|
|
56
|
+
varCharMin: '',
|
|
57
|
+
varChar: '11',
|
|
58
|
+
useModel: {}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const modelStruct = new PlataModels.ModelTemplate({
|
|
62
|
+
param: [ PlataModels.Required(), PlataModels.Int() ],
|
|
63
|
+
object: {
|
|
64
|
+
param: [ PlataModels.Required(), PlataModels.DateTime() ],
|
|
65
|
+
},
|
|
66
|
+
subModel: [ PlataModels.useModel(new PlataModels.ModelTemplate({
|
|
67
|
+
optional: [ PlataModels.Optional(), PlataModels.Int() ],
|
|
68
|
+
required: [ PlataModels.Required(), PlataModels.Int() ],
|
|
69
|
+
} as const , {})) ],
|
|
70
|
+
subModelArray: [[ PlataModels.useModel(new PlataModels.ModelTemplate({
|
|
71
|
+
optional: [ PlataModels.Optional(), PlataModels.Int() ],
|
|
72
|
+
required: [ PlataModels.Required(), PlataModels.Int() ],
|
|
73
|
+
} as const , {})) ]],
|
|
74
|
+
array: [ [ PlataModels.VarChar(30) ] ],
|
|
75
|
+
array_objects: [{
|
|
76
|
+
param: [ PlataModels.Optional(), PlataModels.Decimal(10,2) ]
|
|
77
|
+
}]
|
|
78
|
+
} as const, {})
|
|
79
|
+
|
|
80
|
+
const modelValidations = new PlataModels.ModelTemplate({
|
|
81
|
+
enum: [ PlataModels.Enum([ 0,1 ]) ],
|
|
82
|
+
stringDateTime: [ PlataModels.StringDateTime() ],
|
|
83
|
+
dateTime: [ PlataModels.DateTime() ],
|
|
84
|
+
decimal: [ PlataModels.Decimal(2,2) ],
|
|
85
|
+
intMin: [ PlataModels.Int(5) ],
|
|
86
|
+
intMax: [ PlataModels.Int(undefined, 5) ],
|
|
87
|
+
intRequired: [ PlataModels.Required(), PlataModels.Int() ],
|
|
88
|
+
smallDateTime: [ PlataModels.SmallDateTime() ],
|
|
89
|
+
telefone: [ PlataModels.Telefone() ],
|
|
90
|
+
telefoneDDI: [ PlataModels.TelefoneDDI() ],
|
|
91
|
+
varCharMin: [ PlataModels.VarChar(1, 1) ],
|
|
92
|
+
varChar: [ PlataModels.VarChar(1) ],
|
|
93
|
+
useModel: [ PlataModels.useModel(modelStruct) ],
|
|
94
|
+
} as const, {})
|
|
95
|
+
|
|
96
|
+
it('Struct Filter', () => {
|
|
97
|
+
const InitObj = {
|
|
98
|
+
array_objects: [{ param: '123' }],
|
|
99
|
+
subModelArray: [{ required: 1 }]
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const FilteredObj = modelStruct.filter(InitObj)
|
|
103
|
+
|
|
104
|
+
assert.deepEqual(FilteredObj, {
|
|
105
|
+
param: null,
|
|
106
|
+
object: {
|
|
107
|
+
param: null,
|
|
108
|
+
},
|
|
109
|
+
subModel: {
|
|
110
|
+
required: null,
|
|
111
|
+
optional: null,
|
|
112
|
+
},
|
|
113
|
+
subModelArray: [{
|
|
114
|
+
required: 1,
|
|
115
|
+
optional: null,
|
|
116
|
+
}],
|
|
117
|
+
array: [],
|
|
118
|
+
array_objects: [{
|
|
119
|
+
param: '123'
|
|
120
|
+
}]
|
|
121
|
+
})
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('Validations (Okay)', async () => {
|
|
125
|
+
const { errors, value } = await modelValidations.validate(objOkay)
|
|
126
|
+
|
|
127
|
+
assert.strictEqual(errors, undefined)
|
|
128
|
+
assert.deepEqual(value, objOkay)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('Validations (Not Okay)', async () => {
|
|
132
|
+
const { errors, value } = await modelValidations.validate(objNotOkay)
|
|
133
|
+
|
|
134
|
+
assert.notStrictEqual(errors, undefined)
|
|
135
|
+
assert.strictEqual(
|
|
136
|
+
errors?.length,
|
|
137
|
+
Object.keys(modelValidations.template).length,
|
|
138
|
+
errors?.map(e => `${e.errorID} - ${e.msg}`).join('\n')
|
|
139
|
+
)
|
|
140
|
+
assert.deepEqual(value, objNotOkay)
|
|
141
|
+
})
|
|
142
142
|
})
|