@cratis/scene.engine 4.25.0 → 4.26.0

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 (108) hide show
  1. package/dist/cjs/index.js +41 -0
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/cjs/sampleData/SampleRandom.js +77 -0
  4. package/dist/cjs/sampleData/SampleRandom.js.map +1 -0
  5. package/dist/cjs/sampleData/describeSampleDataCatalog.js +61 -0
  6. package/dist/cjs/sampleData/describeSampleDataCatalog.js.map +1 -0
  7. package/dist/cjs/sampleData/findSampleDataSet.js +18 -0
  8. package/dist/cjs/sampleData/findSampleDataSet.js.map +1 -0
  9. package/dist/cjs/sampleData/findSampleRowLiterals.js +73 -0
  10. package/dist/cjs/sampleData/findSampleRowLiterals.js.map +1 -0
  11. package/dist/cjs/sampleData/generateSample.js +188 -0
  12. package/dist/cjs/sampleData/generateSample.js.map +1 -0
  13. package/dist/cjs/sampleData/inferSampleFieldKind.js +176 -0
  14. package/dist/cjs/sampleData/inferSampleFieldKind.js.map +1 -0
  15. package/dist/cjs/sampleData/resolveSampleData.js +107 -0
  16. package/dist/cjs/sampleData/resolveSampleData.js.map +1 -0
  17. package/dist/cjs/sampleData/sampleDataRules.js +26 -0
  18. package/dist/cjs/sampleData/sampleDataRules.js.map +1 -0
  19. package/dist/cjs/sampleData/sampleLocales.js +359 -0
  20. package/dist/cjs/sampleData/sampleLocales.js.map +1 -0
  21. package/dist/cjs/sampleData/validateSampleDataPack.js +44 -0
  22. package/dist/cjs/sampleData/validateSampleDataPack.js.map +1 -0
  23. package/dist/cjs/sampleData/validateSampleDataPackages.js +71 -0
  24. package/dist/cjs/sampleData/validateSampleDataPackages.js.map +1 -0
  25. package/dist/cjs/sampleData/validateSampleDataSelection.js +52 -0
  26. package/dist/cjs/sampleData/validateSampleDataSelection.js.map +1 -0
  27. package/dist/esm/index.d.ts +1 -0
  28. package/dist/esm/index.d.ts.map +1 -1
  29. package/dist/esm/index.js +12 -0
  30. package/dist/esm/index.js.map +1 -1
  31. package/dist/esm/sampleData/SampleDataPackSource.d.ts +6 -0
  32. package/dist/esm/sampleData/SampleDataPackSource.d.ts.map +1 -0
  33. package/dist/esm/sampleData/SampleDataPackSource.js +2 -0
  34. package/dist/esm/sampleData/SampleDataPackSource.js.map +1 -0
  35. package/dist/esm/sampleData/SampleDataResolution.d.ts +29 -0
  36. package/dist/esm/sampleData/SampleDataResolution.d.ts.map +1 -0
  37. package/dist/esm/sampleData/SampleDataResolution.js +2 -0
  38. package/dist/esm/sampleData/SampleDataResolution.js.map +1 -0
  39. package/dist/esm/sampleData/SampleRandom.d.ts +14 -0
  40. package/dist/esm/sampleData/SampleRandom.d.ts.map +1 -0
  41. package/dist/esm/sampleData/SampleRandom.js +72 -0
  42. package/dist/esm/sampleData/SampleRandom.js.map +1 -0
  43. package/dist/esm/sampleData/describeSampleDataCatalog.d.ts +24 -0
  44. package/dist/esm/sampleData/describeSampleDataCatalog.d.ts.map +1 -0
  45. package/dist/esm/sampleData/describeSampleDataCatalog.js +57 -0
  46. package/dist/esm/sampleData/describeSampleDataCatalog.js.map +1 -0
  47. package/dist/esm/sampleData/findSampleDataSet.d.ts +3 -0
  48. package/dist/esm/sampleData/findSampleDataSet.d.ts.map +1 -0
  49. package/dist/esm/sampleData/findSampleDataSet.js +16 -0
  50. package/dist/esm/sampleData/findSampleDataSet.js.map +1 -0
  51. package/dist/esm/sampleData/findSampleRowLiterals.d.ts +11 -0
  52. package/dist/esm/sampleData/findSampleRowLiterals.d.ts.map +1 -0
  53. package/dist/esm/sampleData/findSampleRowLiterals.js +69 -0
  54. package/dist/esm/sampleData/findSampleRowLiterals.js.map +1 -0
  55. package/dist/esm/sampleData/generateSample.d.ts +6 -0
  56. package/dist/esm/sampleData/generateSample.d.ts.map +1 -0
  57. package/dist/esm/sampleData/generateSample.js +183 -0
  58. package/dist/esm/sampleData/generateSample.js.map +1 -0
  59. package/dist/esm/sampleData/index.d.ts +15 -0
  60. package/dist/esm/sampleData/index.d.ts.map +1 -0
  61. package/dist/esm/sampleData/index.js +15 -0
  62. package/dist/esm/sampleData/index.js.map +1 -0
  63. package/dist/esm/sampleData/inferSampleFieldKind.d.ts +3 -0
  64. package/dist/esm/sampleData/inferSampleFieldKind.d.ts.map +1 -0
  65. package/dist/esm/sampleData/inferSampleFieldKind.js +174 -0
  66. package/dist/esm/sampleData/inferSampleFieldKind.js.map +1 -0
  67. package/dist/esm/sampleData/resolveSampleData.d.ts +3 -0
  68. package/dist/esm/sampleData/resolveSampleData.d.ts.map +1 -0
  69. package/dist/esm/sampleData/resolveSampleData.js +105 -0
  70. package/dist/esm/sampleData/resolveSampleData.js.map +1 -0
  71. package/dist/esm/sampleData/sampleDataRules.d.ts +7 -0
  72. package/dist/esm/sampleData/sampleDataRules.d.ts.map +1 -0
  73. package/dist/esm/sampleData/sampleDataRules.js +19 -0
  74. package/dist/esm/sampleData/sampleDataRules.js.map +1 -0
  75. package/dist/esm/sampleData/sampleLocales.d.ts +13 -0
  76. package/dist/esm/sampleData/sampleLocales.d.ts.map +1 -0
  77. package/dist/esm/sampleData/sampleLocales.js +355 -0
  78. package/dist/esm/sampleData/sampleLocales.js.map +1 -0
  79. package/dist/esm/sampleData/validateSampleDataPack.d.ts +3 -0
  80. package/dist/esm/sampleData/validateSampleDataPack.d.ts.map +1 -0
  81. package/dist/esm/sampleData/validateSampleDataPack.js +42 -0
  82. package/dist/esm/sampleData/validateSampleDataPack.js.map +1 -0
  83. package/dist/esm/sampleData/validateSampleDataPackages.d.ts +3 -0
  84. package/dist/esm/sampleData/validateSampleDataPackages.d.ts.map +1 -0
  85. package/dist/esm/sampleData/validateSampleDataPackages.js +69 -0
  86. package/dist/esm/sampleData/validateSampleDataPackages.js.map +1 -0
  87. package/dist/esm/sampleData/validateSampleDataSelection.d.ts +3 -0
  88. package/dist/esm/sampleData/validateSampleDataSelection.d.ts.map +1 -0
  89. package/dist/esm/sampleData/validateSampleDataSelection.js +50 -0
  90. package/dist/esm/sampleData/validateSampleDataSelection.js.map +1 -0
  91. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  92. package/index.ts +1 -0
  93. package/package.json +3 -3
  94. package/sampleData/SampleDataPackSource.ts +13 -0
  95. package/sampleData/SampleDataResolution.ts +93 -0
  96. package/sampleData/SampleRandom.ts +85 -0
  97. package/sampleData/describeSampleDataCatalog.ts +108 -0
  98. package/sampleData/findSampleDataSet.ts +18 -0
  99. package/sampleData/findSampleRowLiterals.ts +86 -0
  100. package/sampleData/generateSample.ts +156 -0
  101. package/sampleData/index.ts +17 -0
  102. package/sampleData/inferSampleFieldKind.ts +64 -0
  103. package/sampleData/resolveSampleData.ts +96 -0
  104. package/sampleData/sampleDataRules.ts +30 -0
  105. package/sampleData/sampleLocales.ts +91 -0
  106. package/sampleData/validateSampleDataPack.ts +48 -0
  107. package/sampleData/validateSampleDataPackages.ts +79 -0
  108. package/sampleData/validateSampleDataSelection.ts +55 -0
@@ -0,0 +1,96 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { SampleDataPack } from '@cratis/scene.model';
5
+ import { generateSample } from './generateSample';
6
+ import { defaultSampleLocale } from './sampleLocales';
7
+ import { findSampleDataSet } from './findSampleDataSet';
8
+ import { SampleDataContext, SampleDataDiagnostic, SampleDataRequest, SampleDataResolution } from './SampleDataResolution';
9
+
10
+ /**
11
+ * Resolves the rows to show for a read model or shape at design or preview time, by the order of spec §11.2:
12
+ *
13
+ * 1. the pack bound to the target (`profile.sampleData.bindings`, a rare override);
14
+ * 2. the application's active pack: the UI profile's `sampleData.activePack`, chosen once for the whole
15
+ * application, or the session-only override (`context.sessionActivePack`) the preview strip sets;
16
+ * 3. the default pack of the blueprint the template came from (`blueprintDefaultPack`);
17
+ * 4. generated data typed by field kind ({@link generateSample}), when the request carries a shape.
18
+ *
19
+ * A pack supplies rows when it has a set for the target, by an explicit `shape → read model` mapping or else by
20
+ * name ({@link findSampleDataSet}). A rung that cannot supply rows falls through to the next; an unknown pack
21
+ * id, or a bound pack without the target's shape, is reported in `diagnostics` (an active or blueprint pack
22
+ * that merely lacks the shape is normal and not reported). The locale carried is the supplying pack's; for
23
+ * generated data it is the first existing bound, active or blueprint pack's, else `context.locale`.
24
+ *
25
+ * Outside design and preview (`context.isDesignOrPreview` not true) nothing resolves (spec SD6).
26
+ */
27
+ export function resolveSampleData(request: SampleDataRequest, context: SampleDataContext): SampleDataResolution {
28
+ const target = request.target ?? request.shape?.name ?? '';
29
+ const fallbackLocale = context.locale ?? defaultSampleLocale;
30
+ const diagnostics: SampleDataDiagnostic[] = [];
31
+
32
+ if (context.isDesignOrPreview !== true) {
33
+ return { source: 'disabled', locale: fallbackLocale, rows: [], diagnostics };
34
+ }
35
+
36
+ const packs = context.packs ?? [];
37
+ const selection = context.profile?.sampleData;
38
+ const mappings = selection?.mappings ?? [];
39
+ const find = (id: string | null | undefined): SampleDataPack | undefined => (id === undefined || id === null ? undefined : packs.find(pack => pack.id === id));
40
+ const missing = (id: string, role: string): void => {
41
+ diagnostics.push({ code: 'packNotFound', message: `The ${role} sample data pack '${id}' is not available` });
42
+ };
43
+
44
+ const bindingPackId = (selection?.bindings ?? []).find(binding => binding.target === target)?.pack;
45
+ const bindingPack = find(bindingPackId);
46
+ if (bindingPackId !== undefined && bindingPack === undefined) {
47
+ missing(bindingPackId, `bound`);
48
+ } else if (bindingPack !== undefined) {
49
+ const set = findSampleDataSet(bindingPack, target, mappings);
50
+ if (set !== undefined) return fromSet('binding', bindingPack, set.shape, set.rows, diagnostics);
51
+ diagnostics.push({ code: 'shapeNotInPack', message: `The bound sample data pack '${bindingPack.id}' has no rows for '${target}'` });
52
+ }
53
+
54
+ const sessionPackId = context.sessionActivePack;
55
+ const chosenPackId = sessionPackId !== undefined ? sessionPackId : selection?.activePack;
56
+ const activePackId = chosenPackId === '' ? undefined : chosenPackId;
57
+ const activePack = find(activePackId);
58
+ if (activePackId !== undefined && activePackId !== null && activePack === undefined) {
59
+ missing(activePackId, 'active');
60
+ } else if (activePack !== undefined) {
61
+ const set = findSampleDataSet(activePack, target, mappings);
62
+ if (set !== undefined) return fromSet('activePack', activePack, set.shape, set.rows, diagnostics);
63
+ }
64
+
65
+ const blueprintPackId = context.blueprintDefaultPack;
66
+ const blueprintPack = find(blueprintPackId);
67
+ if (blueprintPackId !== undefined && blueprintPackId !== null && blueprintPack === undefined) {
68
+ missing(blueprintPackId, 'blueprint default');
69
+ } else if (blueprintPack !== undefined) {
70
+ const set = findSampleDataSet(blueprintPack, target, mappings);
71
+ if (set !== undefined) return fromSet('blueprintDefault', blueprintPack, set.shape, set.rows, diagnostics);
72
+ }
73
+
74
+ const locale = bindingPack?.locale ?? activePack?.locale ?? blueprintPack?.locale ?? fallbackLocale;
75
+ if (request.shape === undefined) {
76
+ return { source: 'none', locale, shape: target, rows: [], diagnostics };
77
+ }
78
+
79
+ return {
80
+ source: 'generated',
81
+ locale,
82
+ shape: request.shape.name,
83
+ rows: generateSample(request.shape, context.seed, context.rowCount, locale),
84
+ diagnostics,
85
+ };
86
+ }
87
+
88
+ function fromSet(
89
+ source: 'binding' | 'activePack' | 'blueprintDefault',
90
+ pack: SampleDataPack,
91
+ shape: string,
92
+ rows: Record<string, unknown>[],
93
+ diagnostics: SampleDataDiagnostic[],
94
+ ): SampleDataResolution {
95
+ return { source, pack: pack.id, locale: pack.locale, shape, rows: structuredClone(rows), diagnostics };
96
+ }
@@ -0,0 +1,30 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ /** A pack id: lower-case letters and digits in groups separated by `.`, `_` or `-` (`crm-nordic-smb`). */
5
+ export const sampleDataIdPattern = /^[a-z0-9]+([._-][a-z0-9]+)*$/;
6
+
7
+ /** A BCP 47 style locale tag: a 2-3 letter language followed by alphanumeric subtags (`nb-NO`, `en-GB`, `zh-Hant-TW`). */
8
+ export const sampleDataLocalePattern = /^[A-Za-z]{2,3}(-[A-Za-z0-9]{2,8})*$/;
9
+
10
+ /** The owner of a pack the author created. */
11
+ export const userSampleDataOwner = 'user';
12
+
13
+ /** The prefix of the owner of a pack a blueprint ships (`blueprint:crm`). */
14
+ export const blueprintSampleDataOwnerPrefix = 'blueprint:';
15
+
16
+ /** Whether an owner is `user` or `blueprint:<id>` with a non-empty id. */
17
+ export function isValidSampleDataOwner(owner: string | undefined): boolean {
18
+ if (owner === undefined || owner === null) return false;
19
+ return owner === userSampleDataOwner || (owner.startsWith(blueprintSampleDataOwnerPrefix) && owner.length > blueprintSampleDataOwnerPrefix.length && owner.trim() === owner);
20
+ }
21
+
22
+ /** The path separator, built from its code so the seam rule against route literals (I4) does not mistake it for a URL. */
23
+ const pathSeparator = String.fromCharCode(47);
24
+
25
+ /** Whether a package-relative pack path is `sampleData/<name>.json` with no `..`, `\\` or nested leading `/`. */
26
+ export function isValidSampleDataPath(path: string | undefined): boolean {
27
+ if (path === undefined || path === null) return false;
28
+ if (!path.startsWith('sampleData/') || !path.endsWith('.json') || path.length <= 'sampleData/.json'.length) return false;
29
+ return !path.includes('..') && !path.includes('\\') && !path.split(pathSeparator).includes('');
30
+ }
@@ -0,0 +1,91 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ /** The value pools and formats generated data draws from for one locale. */
5
+ export interface SampleLocale {
6
+ firstNames: string[];
7
+ lastNames: string[];
8
+ companies: string[];
9
+ cities: string[];
10
+ domains: string[];
11
+ /** `#` is replaced by a random digit. */
12
+ phoneTemplate: string;
13
+ words: string[];
14
+ }
15
+
16
+ const englishWords = ['review', 'quarterly', 'update', 'invoice', 'customer', 'delivery', 'schedule', 'renewal', 'report', 'follow', 'up', 'meeting'];
17
+
18
+ const locales = new Map<string, SampleLocale>(Object.entries({
19
+ 'en-GB': {
20
+ firstNames: ['Oliver', 'Amelia', 'George', 'Isla', 'Harry', 'Ava', 'Jack', 'Freya', 'Arthur', 'Poppy'],
21
+ lastNames: ['Smith', 'Jones', 'Taylor', 'Brown', 'Wilson', 'Evans', 'Thomas', 'Roberts', 'Walker', 'Wright'],
22
+ companies: ['Northgate Ltd', 'Albion Supplies Ltd', 'Thames Logistics Ltd', 'Pennine Foods Ltd', 'Harbour Digital Ltd', 'Cotswold Tools Ltd'],
23
+ cities: ['London', 'Manchester', 'Leeds', 'Bristol', 'Glasgow', 'Cardiff', 'Sheffield', 'Newcastle'],
24
+ domains: ['example.co.uk', 'example.org.uk', 'sample.co.uk'],
25
+ phoneTemplate: '+44 7### ######',
26
+ words: englishWords,
27
+ },
28
+ 'en-US': {
29
+ firstNames: ['James', 'Emma', 'Michael', 'Olivia', 'William', 'Sophia', 'Benjamin', 'Mia', 'Henry', 'Grace'],
30
+ lastNames: ['Johnson', 'Miller', 'Davis', 'Garcia', 'Anderson', 'Martin', 'Clark', 'Lewis', 'Young', 'Hall'],
31
+ companies: ['Summit Supply Inc', 'Bluebird Software Inc', 'Redwood Freight Inc', 'Lakeside Foods Inc', 'Cardinal Tools Inc', 'Pioneer Digital Inc'],
32
+ cities: ['New York', 'Chicago', 'Austin', 'Seattle', 'Denver', 'Boston', 'Atlanta', 'Portland'],
33
+ domains: ['example.com', 'example.net', 'sample.com'],
34
+ phoneTemplate: '+1 (555) ###-####',
35
+ words: englishWords,
36
+ },
37
+ 'nb-NO': {
38
+ firstNames: ['Lars', 'Ingrid', 'Ola', 'Kari', 'Henrik', 'Sigrid', 'Magnus', 'Åse', 'Bjørn', 'Mari'],
39
+ lastNames: ['Hansen', 'Johansen', 'Olsen', 'Larsen', 'Andersen', 'Pedersen', 'Nilsen', 'Kristiansen', 'Berg', 'Haugen'],
40
+ companies: ['Fjordline AS', 'Nordlys Handel AS', 'Fjellvik Maskin AS', 'Bergen Logistikk AS', 'Havbruk Partner AS', 'Skagerrak Digital AS'],
41
+ cities: ['Oslo', 'Bergen', 'Trondheim', 'Stavanger', 'Tromsø', 'Drammen', 'Kristiansand', 'Ålesund'],
42
+ domains: ['eksempel.no', 'prove.no', 'demo.no'],
43
+ phoneTemplate: '+47 9## ## ###',
44
+ words: ['gjennomgang', 'kvartal', 'oppdatering', 'faktura', 'kunde', 'levering', 'avtale', 'fornyelse', 'rapport', 'oppfølging', 'møte', 'bestilling'],
45
+ },
46
+ 'sv-SE': {
47
+ firstNames: ['Erik', 'Astrid', 'Lars', 'Maja', 'Anders', 'Elsa', 'Johan', 'Linnéa', 'Gustav', 'Åsa'],
48
+ lastNames: ['Andersson', 'Johansson', 'Karlsson', 'Nilsson', 'Eriksson', 'Larsson', 'Olsson', 'Persson', 'Svensson', 'Gustafsson'],
49
+ companies: ['Norrlands Handel AB', 'Östersund Teknik AB', 'Göta Logistik AB', 'Skärgården Livs AB', 'Malmö Digital AB', 'Vättern Verktyg AB'],
50
+ cities: ['Stockholm', 'Göteborg', 'Malmö', 'Uppsala', 'Västerås', 'Örebro', 'Linköping', 'Umeå'],
51
+ domains: ['exempel.se', 'prov.se', 'demo.se'],
52
+ phoneTemplate: '+46 70 ### ## ##',
53
+ words: ['granskning', 'kvartal', 'uppdatering', 'faktura', 'kund', 'leverans', 'avtal', 'förnyelse', 'rapport', 'uppföljning', 'möte', 'beställning'],
54
+ },
55
+ 'de-DE': {
56
+ firstNames: ['Lukas', 'Anna', 'Felix', 'Marie', 'Jonas', 'Sophie', 'Maximilian', 'Lena', 'Paul', 'Jördis'],
57
+ lastNames: ['Müller', 'Schmidt', 'Schneider', 'Fischer', 'Weber', 'Meyer', 'Wagner', 'Becker', 'Schulz', 'Hoffmann'],
58
+ companies: ['Rheinwerk GmbH', 'Nordsee Handel GmbH', 'Isar Logistik GmbH', 'Schwarzwald Technik GmbH', 'Elbe Digital GmbH', 'Bayern Werkzeug GmbH'],
59
+ cities: ['Berlin', 'Hamburg', 'München', 'Köln', 'Frankfurt', 'Stuttgart', 'Düsseldorf', 'Leipzig'],
60
+ domains: ['beispiel.de', 'muster.de', 'demo.de'],
61
+ phoneTemplate: '+49 151 ########',
62
+ words: ['prüfung', 'quartal', 'aktualisierung', 'rechnung', 'kunde', 'lieferung', 'vertrag', 'verlängerung', 'bericht', 'nachfassen', 'termin', 'bestellung'],
63
+ },
64
+ } as Record<string, SampleLocale>));
65
+
66
+ const languageDefaults = new Map<string, string>([
67
+ ['en', 'en-GB'],
68
+ ['nb', 'nb-NO'],
69
+ ['nn', 'nb-NO'],
70
+ ['no', 'nb-NO'],
71
+ ['sv', 'sv-SE'],
72
+ ['de', 'de-DE'],
73
+ ]);
74
+
75
+ /** The locale used when a tag matches no pool. */
76
+ export const defaultSampleLocale = 'en-GB';
77
+
78
+ /** The locale tags with their own pools. */
79
+ export const sampleLocaleTags: string[] = [...locales.keys()];
80
+
81
+ /**
82
+ * Resolves a BCP 47 tag to a pool: the exact tag (language lower case, region upper case), else the
83
+ * language's default pool, else {@link defaultSampleLocale}.
84
+ */
85
+ export function sampleLocaleFor(tag: string | undefined): SampleLocale {
86
+ const parts = (tag ?? '').trim().split('-');
87
+ const language = parts[0].toLowerCase();
88
+ const region = parts.length > 1 ? parts[1].toUpperCase() : '';
89
+ const exact = region === '' ? undefined : locales.get(`${language}-${region}`);
90
+ return exact ?? locales.get(languageDefaults.get(language) ?? defaultSampleLocale)!;
91
+ }
@@ -0,0 +1,48 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { SampleDataPack } from '@cratis/scene.model';
5
+ import { isValidSampleDataOwner, sampleDataIdPattern, sampleDataLocalePattern } from './sampleDataRules';
6
+
7
+ /**
8
+ * Checks a pack's schema: an id of the allowed form, a title, a well-formed locale tag, an owner that is
9
+ * `user` or `blueprint:<id>`, and sets with a shape each, none repeated. The C# engine
10
+ * (`SampleDataValidation.ValidatePack`) reports the same problems in the same order; both are asserted against
11
+ * `sample-data-resolution-fixtures.json`.
12
+ */
13
+ export function validateSampleDataPack(pack: SampleDataPack): string[] {
14
+ const problems: string[] = [];
15
+ const id = pack.id ?? '';
16
+ const label = id.trim() === '' ? 'Sample data pack' : `Sample data pack '${id}'`;
17
+
18
+ if (id.trim() === '') {
19
+ problems.push('Sample data pack declares no id');
20
+ } else if (!sampleDataIdPattern.test(id)) {
21
+ problems.push(`${label} declares an invalid id; use lower-case letters, digits and '.', '_' or '-' separators`);
22
+ }
23
+
24
+ if ((pack.title ?? '').trim() === '') problems.push(`${label} declares no title`);
25
+
26
+ if ((pack.locale ?? '').trim() === '') {
27
+ problems.push(`${label} declares no locale`);
28
+ } else if (!sampleDataLocalePattern.test(pack.locale)) {
29
+ problems.push(`${label} declares an invalid locale '${pack.locale}'`);
30
+ }
31
+
32
+ if (!isValidSampleDataOwner(pack.owner)) {
33
+ problems.push(`${label} declares owner '${pack.owner ?? ''}', which is neither 'user' nor 'blueprint:<id>'`);
34
+ }
35
+
36
+ const seen = new Set<string>();
37
+ for (const set of pack.sets ?? []) {
38
+ if ((set.shape ?? '').trim() === '') {
39
+ problems.push(`${label} declares a set without a shape`);
40
+ } else if (seen.has(set.shape)) {
41
+ problems.push(`${label} declares shape '${set.shape}' more than once`);
42
+ } else {
43
+ seen.add(set.shape);
44
+ }
45
+ }
46
+
47
+ return problems;
48
+ }
@@ -0,0 +1,79 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { PackageKind } from '@cratis/scene.model';
5
+ import { SampleDataPackSource } from './SampleDataPackSource';
6
+ import { isValidSampleDataOwner, isValidSampleDataPath, sampleDataIdPattern, sampleDataLocalePattern } from './sampleDataRules';
7
+ import { validateSampleDataPack } from './validateSampleDataPack';
8
+
9
+ /**
10
+ * Checks the sample data the given packages ship, in package then reference order: only a Blueprint package may
11
+ * ship packs; each reference needs a valid id, a title, a locale tag and a `sampleData/*.json` path, with no id
12
+ * or path repeated; and each loaded pack must be valid, match its reference's id and locale, be owned by a
13
+ * blueprint, and be one the manifest references. The C# engine (`SampleDataValidation.ValidatePackages`)
14
+ * reports the same problems in the same order.
15
+ */
16
+ export function validateSampleDataPackages(sources: readonly SampleDataPackSource[]): string[] {
17
+ const problems: string[] = [];
18
+
19
+ for (const source of sources) {
20
+ const manifest = source.manifest;
21
+ const references = manifest.sampleData ?? [];
22
+ const owner = `Package '${manifest.name}'`;
23
+
24
+ if (references.length > 0 && manifest.kind !== PackageKind.Blueprint) {
25
+ problems.push(`${owner} ships sample data but is not a Blueprint package`);
26
+ }
27
+
28
+ const ids = new Set<string>();
29
+ const paths = new Set<string>();
30
+ for (const reference of references) {
31
+ const label = `${owner} sample data pack '${reference.id ?? ''}'`;
32
+ if ((reference.id ?? '').trim() === '') {
33
+ problems.push(`${owner} declares a sample data pack without an id`);
34
+ } else if (!sampleDataIdPattern.test(reference.id)) {
35
+ problems.push(`${label} has an invalid id`);
36
+ } else if (ids.has(reference.id)) {
37
+ problems.push(`${label} is declared more than once`);
38
+ } else {
39
+ ids.add(reference.id);
40
+ }
41
+
42
+ if ((reference.title ?? '').trim() === '') problems.push(`${label} declares no title`);
43
+
44
+ if ((reference.locale ?? '').trim() === '') {
45
+ problems.push(`${label} declares no locale`);
46
+ } else if (!sampleDataLocalePattern.test(reference.locale)) {
47
+ problems.push(`${label} declares an invalid locale '${reference.locale}'`);
48
+ }
49
+
50
+ if (!isValidSampleDataPath(reference.path)) {
51
+ problems.push(`${label} declares path '${reference.path ?? ''}', which is not 'sampleData/<name>.json'`);
52
+ } else if (paths.has(reference.path)) {
53
+ problems.push(`${label} reuses the path '${reference.path}'`);
54
+ } else {
55
+ paths.add(reference.path);
56
+ }
57
+ }
58
+
59
+ for (const pack of source.packs ?? []) {
60
+ const label = `${owner} sample data pack '${pack.id ?? ''}'`;
61
+ problems.push(...validateSampleDataPack(pack));
62
+ const reference = references.find(candidate => candidate.id === pack.id);
63
+ if (reference === undefined) {
64
+ problems.push(`${label} is not referenced by the package's sampleData`);
65
+ continue;
66
+ }
67
+
68
+ if (reference.locale !== pack.locale) {
69
+ problems.push(`${label} has locale '${pack.locale}' but its reference declares '${reference.locale}'`);
70
+ }
71
+
72
+ if (isValidSampleDataOwner(pack.owner) && pack.owner === 'user') {
73
+ problems.push(`${label} must be owned by a blueprint, not 'user'`);
74
+ }
75
+ }
76
+ }
77
+
78
+ return problems;
79
+ }
@@ -0,0 +1,55 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { SampleDataPack, SampleDataSelection } from '@cratis/scene.model';
5
+
6
+ /**
7
+ * Checks an application's sample data selection against the packs available: the active pack and every bound
8
+ * pack must exist, mappings and bindings must be complete and not repeated, and one read model must not be fed
9
+ * by two different shapes. The C# engine (`SampleDataValidation.ValidateSelection`) reports the same problems in
10
+ * the same order.
11
+ */
12
+ export function validateSampleDataSelection(selection: SampleDataSelection, packs: readonly SampleDataPack[]): string[] {
13
+ const problems: string[] = [];
14
+ const known = new Set(packs.map(pack => pack.id));
15
+
16
+ if (selection.activePack !== undefined && selection.activePack !== null && !known.has(selection.activePack)) {
17
+ problems.push(`The active sample data pack '${selection.activePack}' is not available`);
18
+ }
19
+
20
+ const pairs = new Set<string>();
21
+ const shapeByReadModel = new Map<string, string>();
22
+ for (const mapping of selection.mappings ?? []) {
23
+ if ((mapping.shape ?? '').trim() === '') {
24
+ problems.push('A sample data mapping declares no shape');
25
+ } else if ((mapping.readModel ?? '').trim() === '') {
26
+ problems.push(`The sample data mapping for shape '${mapping.shape}' declares no read model`);
27
+ } else if (pairs.has(`${mapping.shape}\u0000${mapping.readModel}`)) {
28
+ problems.push(`The sample data mapping '${mapping.shape}' to '${mapping.readModel}' is declared more than once`);
29
+ } else {
30
+ pairs.add(`${mapping.shape}\u0000${mapping.readModel}`);
31
+ const existing = shapeByReadModel.get(mapping.readModel);
32
+ if (existing === undefined) {
33
+ shapeByReadModel.set(mapping.readModel, mapping.shape);
34
+ } else {
35
+ problems.push(`The read model '${mapping.readModel}' is mapped from both shape '${existing}' and shape '${mapping.shape}'`);
36
+ }
37
+ }
38
+ }
39
+
40
+ const targets = new Set<string>();
41
+ for (const binding of selection.bindings ?? []) {
42
+ if ((binding.target ?? '').trim() === '') {
43
+ problems.push('A sample data binding declares no target');
44
+ } else if (targets.has(binding.target)) {
45
+ problems.push(`The sample data target '${binding.target}' is bound more than once`);
46
+ } else {
47
+ targets.add(binding.target);
48
+ if (!known.has(binding.pack)) {
49
+ problems.push(`The sample data binding for '${binding.target}' names pack '${binding.pack ?? ''}', which is not available`);
50
+ }
51
+ }
52
+ }
53
+
54
+ return problems;
55
+ }