@travetto/schema-faker 6.0.0 → 7.0.0-rc.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.
- package/README.md +2 -2
- package/package.json +3 -3
- package/src/faker.ts +15 -17
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ By default all types are mapped as-is:
|
|
|
28
28
|
* telephone
|
|
29
29
|
* postalCode
|
|
30
30
|
|
|
31
|
-
* Sub-schemas as registered via [@Schema](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/schema.ts#
|
|
31
|
+
* Sub-schemas as registered via [@Schema](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/schema.ts#L19) decorators
|
|
32
32
|
|
|
33
33
|
In addition to the general types, the code relies upon name matching to provide additional refinement:
|
|
34
34
|
|
|
@@ -45,7 +45,7 @@ static #namesToType = {
|
|
|
45
45
|
[/l(ast)?name/, faker.person.lastName],
|
|
46
46
|
[/^ip(add(ress)?)?$/, faker.internet.ip],
|
|
47
47
|
[/^ip(add(ress)?)?(v?)6$/, faker.internet.ipv6],
|
|
48
|
-
[/^username$/, faker.internet.
|
|
48
|
+
[/^username$/, faker.internet.username],
|
|
49
49
|
[/^domain(name)?$/, faker.internet.domainName],
|
|
50
50
|
[/^file(path|name)?$/, faker.system.filePath],
|
|
51
51
|
[/^street(1)?$/, faker.location.streetAddress],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/schema-faker",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0-rc.0",
|
|
4
4
|
"description": "Data generation for schema-registered objects.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"faker",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"directory": "module/schema-faker"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@faker-js/faker": "^
|
|
29
|
-
"@travetto/schema": "^
|
|
28
|
+
"@faker-js/faker": "^10.1.0",
|
|
29
|
+
"@travetto/schema": "^7.0.0-rc.0"
|
|
30
30
|
},
|
|
31
31
|
"travetto": {
|
|
32
32
|
"displayName": "Schema Faker"
|
package/src/faker.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { faker } from '@faker-js/faker';
|
|
2
2
|
|
|
3
3
|
import { Class, TimeUtil } from '@travetto/runtime';
|
|
4
|
-
import { BindUtil,
|
|
4
|
+
import { BindUtil, SchemaFieldConfig, CommonRegExp, SchemaRegistryIndex } from '@travetto/schema';
|
|
5
|
+
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Provide a faker utility for generating content
|
|
@@ -29,7 +30,7 @@ export class SchemaFaker {
|
|
|
29
30
|
[/l(ast)?name/, faker.person.lastName],
|
|
30
31
|
[/^ip(add(ress)?)?$/, faker.internet.ip],
|
|
31
32
|
[/^ip(add(ress)?)?(v?)6$/, faker.internet.ipv6],
|
|
32
|
-
[/^username$/, faker.internet.
|
|
33
|
+
[/^username$/, faker.internet.username],
|
|
33
34
|
[/^domain(name)?$/, faker.internet.domainName],
|
|
34
35
|
[/^file(path|name)?$/, faker.system.filePath],
|
|
35
36
|
[/^street(1)?$/, faker.location.streetAddress],
|
|
@@ -62,7 +63,7 @@ export class SchemaFaker {
|
|
|
62
63
|
* Get an array of values
|
|
63
64
|
* @param cfg Field configuration
|
|
64
65
|
*/
|
|
65
|
-
static #array(cfg:
|
|
66
|
+
static #array(cfg: SchemaFieldConfig): unknown[] {
|
|
66
67
|
const min = cfg.minlength ? cfg.minlength.n : 0;
|
|
67
68
|
const max = cfg.maxlength ? cfg.maxlength.n : 10;
|
|
68
69
|
const size = faker.number.int({ min, max });
|
|
@@ -77,14 +78,12 @@ export class SchemaFaker {
|
|
|
77
78
|
* Get a new number value
|
|
78
79
|
* @param cfg Number config
|
|
79
80
|
*/
|
|
80
|
-
static #number(cfg:
|
|
81
|
+
static #number(cfg: SchemaFieldConfig): number {
|
|
81
82
|
let min = cfg.min && typeof cfg.min.n === 'number' ? cfg.min.n : undefined;
|
|
82
83
|
let max = cfg.max && typeof cfg.max.n === 'number' ? cfg.max.n : undefined;
|
|
83
84
|
let precision = cfg.precision;
|
|
84
85
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (/(price|amt|amount)$/.test(name)) {
|
|
86
|
+
if (/(price|amt|amount)$/i.test(cfg.name.toString())) {
|
|
88
87
|
precision = [13, 2];
|
|
89
88
|
}
|
|
90
89
|
|
|
@@ -112,8 +111,8 @@ export class SchemaFaker {
|
|
|
112
111
|
* Get a date value
|
|
113
112
|
* @param cfg Field config
|
|
114
113
|
*/
|
|
115
|
-
static #date(cfg:
|
|
116
|
-
const name = cfg.name.
|
|
114
|
+
static #date(cfg: SchemaFieldConfig): Date {
|
|
115
|
+
const name = cfg.name.toString().toLowerCase();
|
|
117
116
|
const min = cfg.min && typeof cfg.min.n !== 'number' ? cfg.min.n : undefined;
|
|
118
117
|
const max = cfg.max && typeof cfg.max.n !== 'number' ? cfg.max.n : undefined;
|
|
119
118
|
|
|
@@ -133,8 +132,8 @@ export class SchemaFaker {
|
|
|
133
132
|
* Get a string value
|
|
134
133
|
* @param cfg Field config
|
|
135
134
|
*/
|
|
136
|
-
static #string(cfg:
|
|
137
|
-
const name = cfg.name.toLowerCase();
|
|
135
|
+
static #string(cfg: SchemaFieldConfig): string {
|
|
136
|
+
const name = cfg.name.toString().toLowerCase();
|
|
138
137
|
|
|
139
138
|
if (cfg.match) {
|
|
140
139
|
const mre = cfg.match && cfg.match.re;
|
|
@@ -158,7 +157,7 @@ export class SchemaFaker {
|
|
|
158
157
|
* Get a value for a field config
|
|
159
158
|
* @param cfg Field config
|
|
160
159
|
*/
|
|
161
|
-
static #value(cfg:
|
|
160
|
+
static #value(cfg: SchemaFieldConfig, subArray = false): unknown {
|
|
162
161
|
if (!subArray && cfg.array) {
|
|
163
162
|
return this.#array(cfg);
|
|
164
163
|
} else if (cfg.enum) {
|
|
@@ -175,7 +174,7 @@ export class SchemaFaker {
|
|
|
175
174
|
return this.#date(cfg);
|
|
176
175
|
} else if (typ === Boolean) {
|
|
177
176
|
return faker.datatype.boolean();
|
|
178
|
-
} else if (
|
|
177
|
+
} else if (SchemaRegistryIndex.has(typ)) {
|
|
179
178
|
return this.generate(typ);
|
|
180
179
|
}
|
|
181
180
|
}
|
|
@@ -187,15 +186,14 @@ export class SchemaFaker {
|
|
|
187
186
|
* @param view The view to define specifically
|
|
188
187
|
*/
|
|
189
188
|
static generate<T>(cls: Class<T>, view?: string): T {
|
|
190
|
-
const
|
|
189
|
+
const fields = SchemaRegistryIndex.getFieldMap(cls, view);
|
|
191
190
|
const out: Record<string, unknown> = {};
|
|
192
191
|
|
|
193
|
-
for (const f of
|
|
192
|
+
for (const [f, fieldConfig] of Object.entries(fields)) {
|
|
194
193
|
if (f === 'type' || f === 'id') { // Do not set primary fields
|
|
195
194
|
continue;
|
|
196
195
|
}
|
|
197
|
-
|
|
198
|
-
if (!fieldConfig.required && (Math.random() < .5)) {
|
|
196
|
+
if (fieldConfig.required?.active === false && (Math.random() < .5)) {
|
|
199
197
|
continue;
|
|
200
198
|
}
|
|
201
199
|
out[f] = this.#value(fieldConfig);
|