@travetto/schema-faker 7.0.0-rc.1 → 7.0.0-rc.3
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/package.json +2 -2
- package/src/faker.ts +47 -47
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/schema-faker",
|
|
3
|
-
"version": "7.0.0-rc.
|
|
3
|
+
"version": "7.0.0-rc.3",
|
|
4
4
|
"description": "Data generation for schema-registered objects.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"faker",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@faker-js/faker": "^10.1.0",
|
|
29
|
-
"@travetto/schema": "^7.0.0-rc.
|
|
29
|
+
"@travetto/schema": "^7.0.0-rc.3"
|
|
30
30
|
},
|
|
31
31
|
"travetto": {
|
|
32
32
|
"displayName": "Schema Faker"
|
package/src/faker.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { faker } from '@faker-js/faker';
|
|
2
2
|
|
|
3
3
|
import { Class, TimeUtil } from '@travetto/runtime';
|
|
4
|
-
import { BindUtil, SchemaFieldConfig,
|
|
4
|
+
import { BindUtil, SchemaFieldConfig, CommonRegex, SchemaRegistryIndex } from '@travetto/schema';
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -10,10 +10,10 @@ import { BindUtil, SchemaFieldConfig, CommonRegExp, SchemaRegistryIndex } from '
|
|
|
10
10
|
export class SchemaFaker {
|
|
11
11
|
|
|
12
12
|
static #stringToReType = new Map<RegExp, () => string>([
|
|
13
|
-
[
|
|
14
|
-
[
|
|
15
|
-
[
|
|
16
|
-
[
|
|
13
|
+
[CommonRegex.email, faker.internet.email],
|
|
14
|
+
[CommonRegex.url, faker.internet.url],
|
|
15
|
+
[CommonRegex.telephone, faker.phone.number],
|
|
16
|
+
[CommonRegex.postalCode, faker.location.zipCode]
|
|
17
17
|
]);
|
|
18
18
|
|
|
19
19
|
/**
|
|
@@ -61,29 +61,29 @@ export class SchemaFaker {
|
|
|
61
61
|
|
|
62
62
|
/**
|
|
63
63
|
* Get an array of values
|
|
64
|
-
* @param
|
|
64
|
+
* @param config Field configuration
|
|
65
65
|
*/
|
|
66
|
-
static #array(
|
|
67
|
-
const min =
|
|
68
|
-
const max =
|
|
66
|
+
static #array(config: SchemaFieldConfig): unknown[] {
|
|
67
|
+
const min = config.minlength ? config.minlength.limit : 0;
|
|
68
|
+
const max = config.maxlength ? config.maxlength.limit : 10;
|
|
69
69
|
const size = faker.number.int({ min, max });
|
|
70
70
|
const out: unknown[] = [];
|
|
71
71
|
for (let i = 0; i < size; i++) {
|
|
72
|
-
out.push(this.#value(
|
|
72
|
+
out.push(this.#value(config, true));
|
|
73
73
|
}
|
|
74
74
|
return out;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
/**
|
|
78
78
|
* Get a new number value
|
|
79
|
-
* @param
|
|
79
|
+
* @param config Number config
|
|
80
80
|
*/
|
|
81
|
-
static #number(
|
|
82
|
-
let min =
|
|
83
|
-
let max =
|
|
84
|
-
let precision =
|
|
81
|
+
static #number(config: SchemaFieldConfig): number {
|
|
82
|
+
let min = config.min && typeof config.min.limit === 'number' ? config.min.limit : undefined;
|
|
83
|
+
let max = config.max && typeof config.max.limit === 'number' ? config.max.limit : undefined;
|
|
84
|
+
let precision = config.precision;
|
|
85
85
|
|
|
86
|
-
if (/(price|amt|amount)$/i.test(
|
|
86
|
+
if (/(price|amt|amount)$/i.test(config.name)) {
|
|
87
87
|
precision = [13, 2];
|
|
88
88
|
}
|
|
89
89
|
|
|
@@ -102,25 +102,25 @@ export class SchemaFaker {
|
|
|
102
102
|
|
|
103
103
|
const range = (max - min) * offset;
|
|
104
104
|
|
|
105
|
-
const
|
|
105
|
+
const result = Math.trunc(Math.random() * range);
|
|
106
106
|
|
|
107
|
-
return (
|
|
107
|
+
return (result / offset) + min;
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
/**
|
|
111
111
|
* Get a date value
|
|
112
|
-
* @param
|
|
112
|
+
* @param config Field config
|
|
113
113
|
*/
|
|
114
|
-
static #date(
|
|
115
|
-
const name =
|
|
116
|
-
const min =
|
|
117
|
-
const max =
|
|
114
|
+
static #date(config: SchemaFieldConfig): Date {
|
|
115
|
+
const name = config.name.toLowerCase();
|
|
116
|
+
const min = config.min && typeof config.min.limit !== 'number' ? config.min.limit : undefined;
|
|
117
|
+
const max = config.max && typeof config.max.limit !== 'number' ? config.max.limit : undefined;
|
|
118
118
|
|
|
119
119
|
if (min !== undefined || max !== undefined) {
|
|
120
120
|
return faker.date.between({ from: min || TimeUtil.fromNow(-50, 'd'), to: max || new Date() });
|
|
121
121
|
} else {
|
|
122
|
-
for (const [
|
|
123
|
-
if (
|
|
122
|
+
for (const [regex, fn] of this.#namesToType.date) {
|
|
123
|
+
if (regex.test(name)) {
|
|
124
124
|
return fn();
|
|
125
125
|
}
|
|
126
126
|
}
|
|
@@ -130,22 +130,22 @@ export class SchemaFaker {
|
|
|
130
130
|
|
|
131
131
|
/**
|
|
132
132
|
* Get a string value
|
|
133
|
-
* @param
|
|
133
|
+
* @param config Field config
|
|
134
134
|
*/
|
|
135
|
-
static #string(
|
|
136
|
-
const name =
|
|
135
|
+
static #string(config: SchemaFieldConfig): string {
|
|
136
|
+
const name = config.name.toLowerCase();
|
|
137
137
|
|
|
138
|
-
if (
|
|
139
|
-
const mre =
|
|
140
|
-
for (const [
|
|
141
|
-
if (mre ===
|
|
138
|
+
if (config.match) {
|
|
139
|
+
const mre = config.match && config.match.regex;
|
|
140
|
+
for (const [regex, fn] of this.#stringToReType) {
|
|
141
|
+
if (mre === regex) {
|
|
142
142
|
return fn();
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
-
for (const [
|
|
148
|
-
if (
|
|
147
|
+
for (const [regex, fn] of this.#namesToType.string) {
|
|
148
|
+
if (regex.test(name)) {
|
|
149
149
|
return fn();
|
|
150
150
|
}
|
|
151
151
|
}
|
|
@@ -155,23 +155,23 @@ export class SchemaFaker {
|
|
|
155
155
|
|
|
156
156
|
/**
|
|
157
157
|
* Get a value for a field config
|
|
158
|
-
* @param
|
|
158
|
+
* @param config Field config
|
|
159
159
|
*/
|
|
160
|
-
static #value(
|
|
161
|
-
if (!subArray &&
|
|
162
|
-
return this.#array(
|
|
163
|
-
} else if (
|
|
164
|
-
return faker.helpers.arrayElement(
|
|
160
|
+
static #value(config: SchemaFieldConfig, subArray = false): unknown {
|
|
161
|
+
if (!subArray && config.array) {
|
|
162
|
+
return this.#array(config);
|
|
163
|
+
} else if (config.enum) {
|
|
164
|
+
return faker.helpers.arrayElement(config.enum.values);
|
|
165
165
|
} else {
|
|
166
166
|
|
|
167
|
-
const typ =
|
|
167
|
+
const typ = config.type;
|
|
168
168
|
|
|
169
169
|
if (typ === Number) {
|
|
170
|
-
return this.#number(
|
|
170
|
+
return this.#number(config);
|
|
171
171
|
} else if (typ === String) {
|
|
172
|
-
return this.#string(
|
|
172
|
+
return this.#string(config);
|
|
173
173
|
} else if (typ === Date) {
|
|
174
|
-
return this.#date(
|
|
174
|
+
return this.#date(config);
|
|
175
175
|
} else if (typ === Boolean) {
|
|
176
176
|
return faker.datatype.boolean();
|
|
177
177
|
} else if (SchemaRegistryIndex.has(typ)) {
|
|
@@ -189,14 +189,14 @@ export class SchemaFaker {
|
|
|
189
189
|
const fields = SchemaRegistryIndex.get(cls).getFields(view);
|
|
190
190
|
const out: Record<string, unknown> = {};
|
|
191
191
|
|
|
192
|
-
for (const [
|
|
193
|
-
if (
|
|
192
|
+
for (const [name, fieldConfig] of Object.entries(fields)) {
|
|
193
|
+
if (name === 'type' || name === 'id') { // Do not set primary fields
|
|
194
194
|
continue;
|
|
195
195
|
}
|
|
196
196
|
if (fieldConfig.required?.active === false && (Math.random() < .5)) {
|
|
197
197
|
continue;
|
|
198
198
|
}
|
|
199
|
-
out[
|
|
199
|
+
out[name] = this.#value(fieldConfig);
|
|
200
200
|
}
|
|
201
201
|
|
|
202
202
|
return BindUtil.bindSchema(cls, out, { view });
|