@contember/schema-utils 1.3.0-alpha.5 → 1.3.0-alpha.7

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.
Files changed (61) hide show
  1. package/dist/src/definition-generator/AclDefinitionCodeGenerator.d.ts +19 -0
  2. package/dist/src/definition-generator/AclDefinitionCodeGenerator.d.ts.map +1 -0
  3. package/dist/src/definition-generator/AclDefinitionCodeGenerator.js +116 -0
  4. package/dist/src/definition-generator/AclDefinitionCodeGenerator.js.map +1 -0
  5. package/dist/src/definition-generator/DefinitionCodeGenerator.d.ts +27 -0
  6. package/dist/src/definition-generator/DefinitionCodeGenerator.d.ts.map +1 -0
  7. package/dist/src/definition-generator/DefinitionCodeGenerator.js +224 -0
  8. package/dist/src/definition-generator/DefinitionCodeGenerator.js.map +1 -0
  9. package/dist/src/definition-generator/DefinitionNamingConventions.d.ts +7 -0
  10. package/dist/src/definition-generator/DefinitionNamingConventions.d.ts.map +1 -0
  11. package/dist/src/definition-generator/DefinitionNamingConventions.js +18 -0
  12. package/dist/src/definition-generator/DefinitionNamingConventions.js.map +1 -0
  13. package/dist/src/definition-generator/TsDefinitionGenerator.d.ts +4 -18
  14. package/dist/src/definition-generator/TsDefinitionGenerator.d.ts.map +1 -1
  15. package/dist/src/definition-generator/TsDefinitionGenerator.js +6 -232
  16. package/dist/src/definition-generator/TsDefinitionGenerator.js.map +1 -1
  17. package/dist/src/definition-generator/index.d.ts +3 -0
  18. package/dist/src/definition-generator/index.d.ts.map +1 -1
  19. package/dist/src/definition-generator/index.js +3 -0
  20. package/dist/src/definition-generator/index.js.map +1 -1
  21. package/dist/src/tsconfig.tsbuildinfo +1 -1
  22. package/dist/src/utils/printJsValue.d.ts +13 -0
  23. package/dist/src/utils/printJsValue.d.ts.map +1 -0
  24. package/dist/src/utils/printJsValue.js +57 -0
  25. package/dist/src/utils/printJsValue.js.map +1 -0
  26. package/dist/src/validation/ModelValidator.js +2 -2
  27. package/dist/src/validation/ModelValidator.js.map +1 -1
  28. package/dist/tests/cases/unit/printJsvalue.test.d.ts +2 -0
  29. package/dist/tests/cases/unit/printJsvalue.test.d.ts.map +1 -0
  30. package/dist/tests/cases/unit/printJsvalue.test.js +90 -0
  31. package/dist/tests/cases/unit/printJsvalue.test.js.map +1 -0
  32. package/dist/tests/cases/unit/schemas/acl.d.ts +13 -0
  33. package/dist/tests/cases/unit/schemas/acl.d.ts.map +1 -0
  34. package/dist/tests/cases/unit/schemas/acl.js +47 -0
  35. package/dist/tests/cases/unit/schemas/acl.js.map +1 -0
  36. package/dist/tests/cases/unit/schemas/basic.d.ts.map +1 -1
  37. package/dist/tests/cases/unit/schemas/basic.js.map +1 -1
  38. package/dist/tests/cases/unit/schemas/enum.d.ts.map +1 -1
  39. package/dist/tests/cases/unit/schemas/enum.js.map +1 -1
  40. package/dist/tests/cases/unit/schemas/relations.d.ts.map +1 -1
  41. package/dist/tests/cases/unit/schemas/relations.js.map +1 -1
  42. package/dist/tests/cases/unit/schemas/unique.d.ts.map +1 -1
  43. package/dist/tests/cases/unit/schemas/unique.js.map +1 -1
  44. package/dist/tests/cases/unit/tsDefinitionGenerator.test.js +5 -3
  45. package/dist/tests/cases/unit/tsDefinitionGenerator.test.js.map +1 -1
  46. package/dist/tests/tsconfig.tsbuildinfo +1 -1
  47. package/package.json +4 -4
  48. package/src/definition-generator/AclDefinitionCodeGenerator.ts +126 -0
  49. package/src/definition-generator/DefinitionCodeGenerator.ts +259 -0
  50. package/src/definition-generator/DefinitionNamingConventions.ts +16 -0
  51. package/src/definition-generator/TsDefinitionGenerator.ts +8 -264
  52. package/src/definition-generator/index.ts +3 -0
  53. package/src/utils/printJsValue.ts +62 -0
  54. package/src/validation/ModelValidator.ts +9 -9
  55. package/tests/cases/unit/printJsvalue.test.ts +100 -0
  56. package/tests/cases/unit/schemas/acl.ts +34 -0
  57. package/tests/cases/unit/schemas/basic.ts +1 -1
  58. package/tests/cases/unit/schemas/enum.ts +1 -1
  59. package/tests/cases/unit/schemas/relations.ts +1 -1
  60. package/tests/cases/unit/schemas/unique.ts +1 -1
  61. package/tests/cases/unit/tsDefinitionGenerator.test.ts +6 -4
@@ -0,0 +1,100 @@
1
+ import { beforeAll, describe, expect, test } from 'vitest'
2
+ import { printJsValue } from '../../../src/utils/printJsValue'
3
+
4
+ beforeAll(() => {
5
+ expect.addSnapshotSerializer({
6
+ test: val => typeof val === 'string',
7
+ print: val => String(val),
8
+ })
9
+ })
10
+
11
+ describe('printJsValue', () => {
12
+ test('string', () => {
13
+ expect(printJsValue('foobar')).toMatchInlineSnapshot(`'foobar'`)
14
+ })
15
+
16
+ test('string escape', () => {
17
+ expect(printJsValue('foo\'bar')).toMatchInlineSnapshot(`'foo\\'bar'`)
18
+ })
19
+
20
+ test('number', () => {
21
+ expect(printJsValue(11)).toMatchInlineSnapshot(`11`)
22
+ })
23
+
24
+ test('boolean', () => {
25
+ expect(printJsValue(true)).toMatchInlineSnapshot(`true`)
26
+ })
27
+
28
+ test('null', () => {
29
+ expect(printJsValue(null)).toMatchInlineSnapshot(`null`)
30
+ })
31
+
32
+ test('inline array', () => {
33
+ expect(printJsValue([1, 2, 3])).toMatchInlineSnapshot(`[1, 2, 3]`)
34
+ })
35
+ test('block array', () => {
36
+ expect(printJsValue([1, 2, 3], () => true)).toMatchInlineSnapshot(`
37
+ [
38
+ 1,
39
+ 2,
40
+ 3,
41
+ ]
42
+ `)
43
+ })
44
+
45
+ test('inline object', () => {
46
+ expect(printJsValue({ a: 1, b: 2 })).toMatchInlineSnapshot('{ a: 1, b: 2 }')
47
+ })
48
+
49
+ test('block object', () => {
50
+ expect(printJsValue({ a: 1, b: 2 }, () => true)).toMatchInlineSnapshot(`
51
+ {
52
+ a: 1,
53
+ b: 2,
54
+ }
55
+ `)
56
+ })
57
+
58
+ test('nested block object', () => {
59
+ expect(printJsValue({ a: 1, b: { c: { d: { e: 1 } } } }, () => true)).toMatchInlineSnapshot(`
60
+ {
61
+ a: 1,
62
+ b: {
63
+ c: {
64
+ d: {
65
+ e: 1,
66
+ },
67
+ },
68
+ },
69
+ }
70
+ `)
71
+ })
72
+
73
+
74
+ test('one level nested block object', () => {
75
+ expect(printJsValue({ a: 1, b: { c: { d: { e: 1 } } } }, (_, path) => path.length === 0)).toMatchInlineSnapshot(`
76
+ {
77
+ a: 1,
78
+ b: { c: { d: { e: 1 } } },
79
+ }
80
+ `)
81
+ })
82
+
83
+ test('nested block object and arr', () => {
84
+ expect(printJsValue({ a: 1, b: [2, { c: 4 }, [5, 6]] }, () => true)).toMatchInlineSnapshot(`
85
+ {
86
+ a: 1,
87
+ b: [
88
+ 2,
89
+ {
90
+ c: 4,
91
+ },
92
+ [
93
+ 5,
94
+ 6,
95
+ ],
96
+ ],
97
+ }
98
+ `)
99
+ })
100
+ })
@@ -0,0 +1,34 @@
1
+ import { SchemaDefinition as def, AclDefinition as acl } from '@contember/schema-definition'
2
+
3
+ export const readerRole = acl.createRole('reader', { stages: '*' })
4
+
5
+ export const editorRole = acl.createRole('editor', { stages: '*' })
6
+
7
+ export const categoryEditorVariable = acl.createEntityVariable('category', 'Category', editorRole)
8
+
9
+
10
+ @acl.allow(readerRole, {
11
+ read: true,
12
+ })
13
+ @acl.allow(editorRole, {
14
+ when: { category: { id: categoryEditorVariable } },
15
+ read: true,
16
+ create: ['title', 'category'],
17
+ update: ['title', 'category'],
18
+ })
19
+ export class Article {
20
+ title = def.stringColumn()
21
+ deletedAt = def.dateTimeColumn()
22
+ category = def.manyHasOne(Category)
23
+ }
24
+
25
+
26
+ @acl.allow(readerRole, {
27
+ read: true,
28
+ })
29
+ @acl.allow(editorRole, {
30
+ read: true,
31
+ })
32
+ export class Category {
33
+ name = def.stringColumn()
34
+ }
@@ -1,4 +1,4 @@
1
- import { SchemaDefinition as def } from '@contember/schema-definition'
1
+ import { SchemaDefinition as def, AclDefinition as acl } from '@contember/schema-definition'
2
2
 
3
3
  export class Article {
4
4
  title = def.stringColumn()
@@ -1,4 +1,4 @@
1
- import { SchemaDefinition as def } from '@contember/schema-definition'
1
+ import { SchemaDefinition as def, AclDefinition as acl } from '@contember/schema-definition'
2
2
 
3
3
  export const articleState = def.createEnum('draft', 'published')
4
4
 
@@ -1,4 +1,4 @@
1
- import { SchemaDefinition as def } from '@contember/schema-definition'
1
+ import { SchemaDefinition as def, AclDefinition as acl } from '@contember/schema-definition'
2
2
 
3
3
  export class Article {
4
4
  title = def.stringColumn()
@@ -1,4 +1,4 @@
1
- import { SchemaDefinition as def } from '@contember/schema-definition'
1
+ import { SchemaDefinition as def, AclDefinition as acl } from '@contember/schema-definition'
2
2
 
3
3
  @def.Unique('title')
4
4
  export class Article {
@@ -1,12 +1,13 @@
1
1
  import { createSchema } from '@contember/schema-definition'
2
- import { test, expect } from 'vitest'
2
+ import { expect, test } from 'vitest'
3
3
  import * as basic from './schemas/basic'
4
4
  import * as relations from './schemas/relations'
5
5
  import * as unique from './schemas/unique'
6
6
  import * as enum_ from './schemas/enum'
7
+ import * as acl from './schemas/acl'
7
8
  import { readFile, writeFile } from 'fs/promises'
8
9
  import { join } from 'path'
9
- import { TsDefinitionGenerator } from '../../../src'
10
+ import { DefinitionCodeGenerator } from '../../../src/definition-generator/DefinitionCodeGenerator'
10
11
 
11
12
 
12
13
  const tests = [
@@ -14,13 +15,14 @@ const tests = [
14
15
  ['relations', relations],
15
16
  ['unique', unique],
16
17
  ['enum', enum_],
18
+ ['acl', acl],
17
19
  ] as const
18
20
  for (const [name, def] of tests) {
19
21
  test(`generate schema: ${name}`, async () => {
20
22
  const schema = createSchema(def)
21
- const generator = new TsDefinitionGenerator(schema)
23
+ const generator = new DefinitionCodeGenerator()
22
24
  const content = await readFile(join(__dirname, `schemas/${name}.ts`), 'utf-8')
23
- const generated = generator.generate()
25
+ const generated = generator.generate(schema)
24
26
  try {
25
27
  expect(generated).toBe(content)
26
28
  } catch (e) {