@sprucelabs/schema 32.2.4 → 32.3.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/build/SchemaTypesRenderer.d.ts +11 -0
- package/build/SchemaTypesRenderer.js +79 -0
- package/build/esm/SchemaTypesRenderer.d.ts +11 -0
- package/build/esm/SchemaTypesRenderer.js +73 -0
- package/build/esm/fields/DateField.js +6 -1
- package/build/esm/fields/DateTimeField.js +6 -1
- package/build/esm/fields/EmailField.js +6 -1
- package/build/esm/fields/IdField.js +6 -1
- package/build/esm/fields/ImageField.js +6 -2
- package/build/esm/fields/NumberField.js +15 -1
- package/build/esm/fields/PhoneField.js +6 -1
- package/build/esm/fields/SelectField.js +8 -2
- package/build/esm/fields/TextField.js +5 -2
- package/build/esm/types/template.types.d.ts +8 -6
- package/build/esm/types/template.types.js +3 -3
- package/build/fields/DateField.js +6 -1
- package/build/fields/DateTimeField.js +6 -1
- package/build/fields/EmailField.js +6 -1
- package/build/fields/IdField.js +6 -1
- package/build/fields/ImageField.js +6 -2
- package/build/fields/NumberField.js +15 -1
- package/build/fields/PhoneField.js +6 -1
- package/build/fields/SelectField.js +8 -2
- package/build/fields/TextField.js +5 -2
- package/build/types/template.types.d.ts +8 -6
- package/build/types/template.types.js +3 -3
- package/package.json +5 -5
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Schema } from './schemas.static.types';
|
|
2
|
+
import { TemplateLanguage } from './types/template.types';
|
|
3
|
+
export default class SchemaTypesRenderer {
|
|
4
|
+
static Renderer(): SchemaTypesRenderer;
|
|
5
|
+
render(schema: Schema, options: {
|
|
6
|
+
language: TemplateLanguage;
|
|
7
|
+
}): string;
|
|
8
|
+
private renderField;
|
|
9
|
+
private renderComment;
|
|
10
|
+
private ucFirst;
|
|
11
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fields_1 = require("./fields");
|
|
7
|
+
const assertOptions_1 = __importDefault(require("./utilities/assertOptions"));
|
|
8
|
+
class SchemaTypesRenderer {
|
|
9
|
+
static Renderer() {
|
|
10
|
+
return new this();
|
|
11
|
+
}
|
|
12
|
+
render(schema, options) {
|
|
13
|
+
(0, assertOptions_1.default)({ schema, options }, ['schema', 'options']);
|
|
14
|
+
const { id, fields } = schema;
|
|
15
|
+
const name = this.ucFirst(id);
|
|
16
|
+
const comment = this.renderComment(schema);
|
|
17
|
+
let body = '';
|
|
18
|
+
for (const [key, field] of Object.entries(fields ?? {})) {
|
|
19
|
+
let fieldLine = this.renderField(field, key);
|
|
20
|
+
body += fieldLine;
|
|
21
|
+
}
|
|
22
|
+
return `${comment ? `${comment}\n` : ''}type ${name} struct {
|
|
23
|
+
${body}}`;
|
|
24
|
+
}
|
|
25
|
+
renderField(field, key) {
|
|
26
|
+
const FieldClass = fields_1.fieldClassMap[field.type];
|
|
27
|
+
const { valueType, validation } = FieldClass.generateTemplateDetails({
|
|
28
|
+
//@ts-ignore
|
|
29
|
+
definition: field,
|
|
30
|
+
language: 'go',
|
|
31
|
+
importAs: 'SpruceSchema',
|
|
32
|
+
});
|
|
33
|
+
const { hint, isRequired, minArrayLength, isArray } = field;
|
|
34
|
+
let fieldLine = '';
|
|
35
|
+
if (hint) {
|
|
36
|
+
fieldLine += '\t// ' + hint + '\n';
|
|
37
|
+
}
|
|
38
|
+
fieldLine +=
|
|
39
|
+
'\t' + this.ucFirst(key) + ' ' + valueType + ' `json:"' + key;
|
|
40
|
+
const validateTags = [];
|
|
41
|
+
if (isRequired) {
|
|
42
|
+
validateTags.push('required');
|
|
43
|
+
}
|
|
44
|
+
if (minArrayLength !== undefined) {
|
|
45
|
+
validateTags.push(`min=${minArrayLength}`);
|
|
46
|
+
}
|
|
47
|
+
if (!isRequired) {
|
|
48
|
+
fieldLine += ',omitempty"';
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
fieldLine += '"';
|
|
52
|
+
}
|
|
53
|
+
if (validation && isArray) {
|
|
54
|
+
validateTags.push('dive');
|
|
55
|
+
}
|
|
56
|
+
if (validation) {
|
|
57
|
+
validateTags.push(...validation);
|
|
58
|
+
}
|
|
59
|
+
if (validateTags.length) {
|
|
60
|
+
fieldLine += ' validate:"' + validateTags.join(',') + '"';
|
|
61
|
+
}
|
|
62
|
+
fieldLine += '`\n';
|
|
63
|
+
return fieldLine;
|
|
64
|
+
}
|
|
65
|
+
renderComment(schema) {
|
|
66
|
+
let comment = '';
|
|
67
|
+
if (schema.name) {
|
|
68
|
+
comment = `// ${schema.name}`;
|
|
69
|
+
}
|
|
70
|
+
if (schema.description) {
|
|
71
|
+
comment += `: ${schema.description}`;
|
|
72
|
+
}
|
|
73
|
+
return comment;
|
|
74
|
+
}
|
|
75
|
+
ucFirst(str) {
|
|
76
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.default = SchemaTypesRenderer;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Schema } from './schemas.static.types';
|
|
2
|
+
import { TemplateLanguage } from './types/template.types';
|
|
3
|
+
export default class SchemaTypesRenderer {
|
|
4
|
+
static Renderer(): SchemaTypesRenderer;
|
|
5
|
+
render(schema: Schema, options: {
|
|
6
|
+
language: TemplateLanguage;
|
|
7
|
+
}): string;
|
|
8
|
+
private renderField;
|
|
9
|
+
private renderComment;
|
|
10
|
+
private ucFirst;
|
|
11
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { fieldClassMap } from './fields/index.js';
|
|
2
|
+
import assertOptions from './utilities/assertOptions.js';
|
|
3
|
+
export default class SchemaTypesRenderer {
|
|
4
|
+
static Renderer() {
|
|
5
|
+
return new this();
|
|
6
|
+
}
|
|
7
|
+
render(schema, options) {
|
|
8
|
+
assertOptions({ schema, options }, ['schema', 'options']);
|
|
9
|
+
const { id, fields } = schema;
|
|
10
|
+
const name = this.ucFirst(id);
|
|
11
|
+
const comment = this.renderComment(schema);
|
|
12
|
+
let body = '';
|
|
13
|
+
for (const [key, field] of Object.entries(fields !== null && fields !== void 0 ? fields : {})) {
|
|
14
|
+
let fieldLine = this.renderField(field, key);
|
|
15
|
+
body += fieldLine;
|
|
16
|
+
}
|
|
17
|
+
return `${comment ? `${comment}\n` : ''}type ${name} struct {
|
|
18
|
+
${body}}`;
|
|
19
|
+
}
|
|
20
|
+
renderField(field, key) {
|
|
21
|
+
const FieldClass = fieldClassMap[field.type];
|
|
22
|
+
const { valueType, validation } = FieldClass.generateTemplateDetails({
|
|
23
|
+
//@ts-ignore
|
|
24
|
+
definition: field,
|
|
25
|
+
language: 'go',
|
|
26
|
+
importAs: 'SpruceSchema',
|
|
27
|
+
});
|
|
28
|
+
const { hint, isRequired, minArrayLength, isArray } = field;
|
|
29
|
+
let fieldLine = '';
|
|
30
|
+
if (hint) {
|
|
31
|
+
fieldLine += '\t// ' + hint + '\n';
|
|
32
|
+
}
|
|
33
|
+
fieldLine +=
|
|
34
|
+
'\t' + this.ucFirst(key) + ' ' + valueType + ' `json:"' + key;
|
|
35
|
+
const validateTags = [];
|
|
36
|
+
if (isRequired) {
|
|
37
|
+
validateTags.push('required');
|
|
38
|
+
}
|
|
39
|
+
if (minArrayLength !== undefined) {
|
|
40
|
+
validateTags.push(`min=${minArrayLength}`);
|
|
41
|
+
}
|
|
42
|
+
if (!isRequired) {
|
|
43
|
+
fieldLine += ',omitempty"';
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
fieldLine += '"';
|
|
47
|
+
}
|
|
48
|
+
if (validation && isArray) {
|
|
49
|
+
validateTags.push('dive');
|
|
50
|
+
}
|
|
51
|
+
if (validation) {
|
|
52
|
+
validateTags.push(...validation);
|
|
53
|
+
}
|
|
54
|
+
if (validateTags.length) {
|
|
55
|
+
fieldLine += ' validate:"' + validateTags.join(',') + '"';
|
|
56
|
+
}
|
|
57
|
+
fieldLine += '`\n';
|
|
58
|
+
return fieldLine;
|
|
59
|
+
}
|
|
60
|
+
renderComment(schema) {
|
|
61
|
+
let comment = '';
|
|
62
|
+
if (schema.name) {
|
|
63
|
+
comment = `// ${schema.name}`;
|
|
64
|
+
}
|
|
65
|
+
if (schema.description) {
|
|
66
|
+
comment += `: ${schema.description}`;
|
|
67
|
+
}
|
|
68
|
+
return comment;
|
|
69
|
+
}
|
|
70
|
+
ucFirst(str) {
|
|
71
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -3,8 +3,13 @@ import isUndefinedOrNull from '../utilities/isUndefinedOrNull.js';
|
|
|
3
3
|
import AbstractField from './AbstractField.js';
|
|
4
4
|
class DateField extends AbstractField {
|
|
5
5
|
static generateTemplateDetails(options) {
|
|
6
|
+
const { definition, importAs, language } = options;
|
|
7
|
+
const { isArray } = definition;
|
|
8
|
+
const arrayNotation = isArray ? '[]' : '';
|
|
6
9
|
return {
|
|
7
|
-
valueType:
|
|
10
|
+
valueType: language === 'go'
|
|
11
|
+
? `${arrayNotation}${importAs}.DateFieldValue`
|
|
12
|
+
: `${importAs}.DateFieldValue${arrayNotation}`,
|
|
8
13
|
};
|
|
9
14
|
}
|
|
10
15
|
validate(value, options) {
|
|
@@ -2,8 +2,13 @@ import AbstractField from './AbstractField.js';
|
|
|
2
2
|
import { validateDateValue } from './DateField.js';
|
|
3
3
|
class DateTimeField extends AbstractField {
|
|
4
4
|
static generateTemplateDetails(options) {
|
|
5
|
+
const { definition, importAs, language } = options;
|
|
6
|
+
const { isArray } = definition;
|
|
7
|
+
const arrayNotation = isArray ? '[]' : '';
|
|
5
8
|
return {
|
|
6
|
-
valueType:
|
|
9
|
+
valueType: language === 'go'
|
|
10
|
+
? `${arrayNotation}${importAs}.DateTimeFieldValue`
|
|
11
|
+
: `${importAs}.DateTimeFieldValue${arrayNotation}`,
|
|
7
12
|
};
|
|
8
13
|
}
|
|
9
14
|
validate(value, options) {
|
|
@@ -2,8 +2,13 @@ import * as EmailValidator from 'email-validator';
|
|
|
2
2
|
import AbstractField from './AbstractField.js';
|
|
3
3
|
class EmailField extends AbstractField {
|
|
4
4
|
static generateTemplateDetails(options) {
|
|
5
|
+
const { definition, language } = options;
|
|
6
|
+
const { isArray } = definition;
|
|
7
|
+
const arrayNotation = isArray ? '[]' : '';
|
|
5
8
|
return {
|
|
6
|
-
valueType:
|
|
9
|
+
valueType: language === 'go'
|
|
10
|
+
? `${arrayNotation}string`
|
|
11
|
+
: `string${arrayNotation}`,
|
|
7
12
|
};
|
|
8
13
|
}
|
|
9
14
|
validate(value, options) {
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import AbstractField from './AbstractField.js';
|
|
2
2
|
class IdField extends AbstractField {
|
|
3
3
|
static generateTemplateDetails(options) {
|
|
4
|
+
const { language, definition } = options;
|
|
5
|
+
const { isArray } = definition;
|
|
6
|
+
const arrayNotation = isArray ? '[]' : '';
|
|
4
7
|
return {
|
|
5
|
-
valueType:
|
|
8
|
+
valueType: language === 'go'
|
|
9
|
+
? `${arrayNotation}string`
|
|
10
|
+
: `string${arrayNotation}`,
|
|
6
11
|
};
|
|
7
12
|
}
|
|
8
13
|
toValueType(value) {
|
|
@@ -2,9 +2,13 @@ import AbstractField from './AbstractField.js';
|
|
|
2
2
|
import { requiredImageSizes } from './ImageField.types.js';
|
|
3
3
|
class ImageField extends AbstractField {
|
|
4
4
|
static generateTemplateDetails(options) {
|
|
5
|
-
const { definition, importAs } = options;
|
|
5
|
+
const { definition, importAs, language } = options;
|
|
6
|
+
const { isArray } = definition;
|
|
7
|
+
const arrayNotation = isArray ? '[]' : '';
|
|
6
8
|
return {
|
|
7
|
-
valueType:
|
|
9
|
+
valueType: language === 'go'
|
|
10
|
+
? `${arrayNotation}${importAs}.ImageFieldValue`
|
|
11
|
+
: `${importAs}.ImageFieldValue${arrayNotation}`,
|
|
8
12
|
};
|
|
9
13
|
}
|
|
10
14
|
validate(value) {
|
|
@@ -2,8 +2,22 @@ import SpruceError from '../errors/SpruceError.js';
|
|
|
2
2
|
import AbstractField from './AbstractField.js';
|
|
3
3
|
class NumberField extends AbstractField {
|
|
4
4
|
static generateTemplateDetails(options) {
|
|
5
|
+
const { definition, language } = options;
|
|
6
|
+
const { isArray, options: fieldOptions } = definition;
|
|
7
|
+
const go = `${isArray ? '[]' : ''}float64`;
|
|
8
|
+
let validation = undefined;
|
|
9
|
+
if (language === 'go') {
|
|
10
|
+
validation = [];
|
|
11
|
+
if (fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.min) {
|
|
12
|
+
validation.push(`gte=${fieldOptions.min}`);
|
|
13
|
+
}
|
|
14
|
+
if (fieldOptions === null || fieldOptions === void 0 ? void 0 : fieldOptions.max) {
|
|
15
|
+
validation.push(`lte=${fieldOptions.max}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
5
18
|
return {
|
|
6
|
-
valueType: `number${
|
|
19
|
+
valueType: language === 'go' ? go : `number${isArray ? '[]' : ''}`,
|
|
20
|
+
validation,
|
|
7
21
|
};
|
|
8
22
|
}
|
|
9
23
|
toValueType(value) {
|
|
@@ -2,8 +2,13 @@ import formatPhoneNumber, { isValidNumber, } from '../utilities/formatPhoneNumbe
|
|
|
2
2
|
import AbstractField from './AbstractField.js';
|
|
3
3
|
class PhoneField extends AbstractField {
|
|
4
4
|
static generateTemplateDetails(options) {
|
|
5
|
+
const { definition, language } = options;
|
|
6
|
+
const { isArray } = definition;
|
|
7
|
+
const arrayNotation = isArray ? '[]' : '';
|
|
5
8
|
return {
|
|
6
|
-
valueType:
|
|
9
|
+
valueType: language === 'go'
|
|
10
|
+
? `${arrayNotation}string`
|
|
11
|
+
: `string${arrayNotation}`,
|
|
7
12
|
};
|
|
8
13
|
}
|
|
9
14
|
toValueType(value) {
|
|
@@ -25,12 +25,18 @@ class SelectField extends AbstractField {
|
|
|
25
25
|
return errors;
|
|
26
26
|
}
|
|
27
27
|
static generateTemplateDetails(options) {
|
|
28
|
-
const { definition } = options;
|
|
28
|
+
const { definition, language } = options;
|
|
29
29
|
const { isArray, options: { choices }, } = definition;
|
|
30
|
+
const arrayNotation = isArray ? '[]' : '';
|
|
31
|
+
if (language === 'go') {
|
|
32
|
+
return {
|
|
33
|
+
valueType: `${arrayNotation}string`,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
30
36
|
return {
|
|
31
37
|
valueType: `(${choices
|
|
32
38
|
.map((choice) => `${typeof choice.value === 'number' ? choice.value : `"${choice.value}"`}`)
|
|
33
|
-
.join(' | ')})${
|
|
39
|
+
.join(' | ')})${arrayNotation}`,
|
|
34
40
|
};
|
|
35
41
|
}
|
|
36
42
|
getChoices() {
|
|
@@ -2,9 +2,12 @@ import SpruceError from '../errors/SpruceError.js';
|
|
|
2
2
|
import AbstractField from './AbstractField.js';
|
|
3
3
|
class TextField extends AbstractField {
|
|
4
4
|
static generateTemplateDetails(options) {
|
|
5
|
-
const { definition } = options;
|
|
5
|
+
const { definition, language } = options;
|
|
6
|
+
const valueType = language === 'go'
|
|
7
|
+
? `${definition.isArray ? '[]' : ''}string`
|
|
8
|
+
: `string${definition.isArray ? '[]' : ''}`;
|
|
6
9
|
return {
|
|
7
|
-
valueType
|
|
10
|
+
valueType,
|
|
8
11
|
};
|
|
9
12
|
}
|
|
10
13
|
validate(value, options) {
|
|
@@ -1,23 +1,25 @@
|
|
|
1
1
|
import { FieldDefinitions } from '../fields';
|
|
2
2
|
import { Schema } from '../schemas.static.types';
|
|
3
3
|
export interface FieldTemplateDetails {
|
|
4
|
-
/** The type of value (string, number) */
|
|
4
|
+
/** The type literal of value (string, number) */
|
|
5
5
|
valueType: string;
|
|
6
|
+
/** The unique validation tags for the field (currently go only) */
|
|
7
|
+
validation?: string[];
|
|
6
8
|
}
|
|
7
9
|
/** How are we being rendered in the template */
|
|
8
10
|
export declare enum TemplateRenderAs {
|
|
9
|
-
/** We are rendering type
|
|
11
|
+
/** We are rendering type literal */
|
|
10
12
|
Type = "type",
|
|
11
|
-
/** We are rendering as a value (only called if a value is set in the schema definition) */
|
|
13
|
+
/** We are rendering as a value (only called if a value is set in the schema definition, like defaultValue) */
|
|
12
14
|
Value = "value",
|
|
13
|
-
/** We are rendering as the
|
|
15
|
+
/** We are rendering as the schema's field type (Schema) */
|
|
14
16
|
SchemaType = "schemaType"
|
|
15
17
|
}
|
|
18
|
+
export type TemplateLanguage = 'ts' | 'go';
|
|
16
19
|
/** The shape of options passed to AbstractField.generateTemplateDetails(options) */
|
|
17
20
|
export interface FieldTemplateDetailOptions<T extends FieldDefinitions> {
|
|
18
21
|
/** The language we're generating to, only TS for now */
|
|
19
|
-
language:
|
|
20
|
-
/** All other schemas schemas being rendered */
|
|
22
|
+
language: TemplateLanguage;
|
|
21
23
|
templateItems: SchemaTemplateItem[];
|
|
22
24
|
/** The global namespace to access items */
|
|
23
25
|
globalNamespace: string;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/** How are we being rendered in the template */
|
|
2
2
|
export var TemplateRenderAs;
|
|
3
3
|
(function (TemplateRenderAs) {
|
|
4
|
-
/** We are rendering type
|
|
4
|
+
/** We are rendering type literal */
|
|
5
5
|
TemplateRenderAs["Type"] = "type";
|
|
6
|
-
/** We are rendering as a value (only called if a value is set in the schema definition) */
|
|
6
|
+
/** We are rendering as a value (only called if a value is set in the schema definition, like defaultValue) */
|
|
7
7
|
TemplateRenderAs["Value"] = "value";
|
|
8
|
-
/** We are rendering as the
|
|
8
|
+
/** We are rendering as the schema's field type (Schema) */
|
|
9
9
|
TemplateRenderAs["SchemaType"] = "schemaType";
|
|
10
10
|
})(TemplateRenderAs || (TemplateRenderAs = {}));
|
|
@@ -9,8 +9,13 @@ const isUndefinedOrNull_1 = __importDefault(require("../utilities/isUndefinedOrN
|
|
|
9
9
|
const AbstractField_1 = __importDefault(require("./AbstractField"));
|
|
10
10
|
class DateField extends AbstractField_1.default {
|
|
11
11
|
static generateTemplateDetails(options) {
|
|
12
|
+
const { definition, importAs, language } = options;
|
|
13
|
+
const { isArray } = definition;
|
|
14
|
+
const arrayNotation = isArray ? '[]' : '';
|
|
12
15
|
return {
|
|
13
|
-
valueType:
|
|
16
|
+
valueType: language === 'go'
|
|
17
|
+
? `${arrayNotation}${importAs}.DateFieldValue`
|
|
18
|
+
: `${importAs}.DateFieldValue${arrayNotation}`,
|
|
14
19
|
};
|
|
15
20
|
}
|
|
16
21
|
validate(value, options) {
|
|
@@ -7,8 +7,13 @@ const AbstractField_1 = __importDefault(require("./AbstractField"));
|
|
|
7
7
|
const DateField_1 = require("./DateField");
|
|
8
8
|
class DateTimeField extends AbstractField_1.default {
|
|
9
9
|
static generateTemplateDetails(options) {
|
|
10
|
+
const { definition, importAs, language } = options;
|
|
11
|
+
const { isArray } = definition;
|
|
12
|
+
const arrayNotation = isArray ? '[]' : '';
|
|
10
13
|
return {
|
|
11
|
-
valueType:
|
|
14
|
+
valueType: language === 'go'
|
|
15
|
+
? `${arrayNotation}${importAs}.DateTimeFieldValue`
|
|
16
|
+
: `${importAs}.DateTimeFieldValue${arrayNotation}`,
|
|
12
17
|
};
|
|
13
18
|
}
|
|
14
19
|
validate(value, options) {
|
|
@@ -40,8 +40,13 @@ const EmailValidator = __importStar(require("email-validator"));
|
|
|
40
40
|
const AbstractField_1 = __importDefault(require("./AbstractField"));
|
|
41
41
|
class EmailField extends AbstractField_1.default {
|
|
42
42
|
static generateTemplateDetails(options) {
|
|
43
|
+
const { definition, language } = options;
|
|
44
|
+
const { isArray } = definition;
|
|
45
|
+
const arrayNotation = isArray ? '[]' : '';
|
|
43
46
|
return {
|
|
44
|
-
valueType:
|
|
47
|
+
valueType: language === 'go'
|
|
48
|
+
? `${arrayNotation}string`
|
|
49
|
+
: `string${arrayNotation}`,
|
|
45
50
|
};
|
|
46
51
|
}
|
|
47
52
|
validate(value, options) {
|
package/build/fields/IdField.js
CHANGED
|
@@ -6,8 +6,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const AbstractField_1 = __importDefault(require("./AbstractField"));
|
|
7
7
|
class IdField extends AbstractField_1.default {
|
|
8
8
|
static generateTemplateDetails(options) {
|
|
9
|
+
const { language, definition } = options;
|
|
10
|
+
const { isArray } = definition;
|
|
11
|
+
const arrayNotation = isArray ? '[]' : '';
|
|
9
12
|
return {
|
|
10
|
-
valueType:
|
|
13
|
+
valueType: language === 'go'
|
|
14
|
+
? `${arrayNotation}string`
|
|
15
|
+
: `string${arrayNotation}`,
|
|
11
16
|
};
|
|
12
17
|
}
|
|
13
18
|
toValueType(value) {
|
|
@@ -7,9 +7,13 @@ const AbstractField_1 = __importDefault(require("./AbstractField"));
|
|
|
7
7
|
const ImageField_types_1 = require("./ImageField.types");
|
|
8
8
|
class ImageField extends AbstractField_1.default {
|
|
9
9
|
static generateTemplateDetails(options) {
|
|
10
|
-
const { definition, importAs } = options;
|
|
10
|
+
const { definition, importAs, language } = options;
|
|
11
|
+
const { isArray } = definition;
|
|
12
|
+
const arrayNotation = isArray ? '[]' : '';
|
|
11
13
|
return {
|
|
12
|
-
valueType:
|
|
14
|
+
valueType: language === 'go'
|
|
15
|
+
? `${arrayNotation}${importAs}.ImageFieldValue`
|
|
16
|
+
: `${importAs}.ImageFieldValue${arrayNotation}`,
|
|
13
17
|
};
|
|
14
18
|
}
|
|
15
19
|
validate(value) {
|
|
@@ -7,8 +7,22 @@ const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
|
|
7
7
|
const AbstractField_1 = __importDefault(require("./AbstractField"));
|
|
8
8
|
class NumberField extends AbstractField_1.default {
|
|
9
9
|
static generateTemplateDetails(options) {
|
|
10
|
+
const { definition, language } = options;
|
|
11
|
+
const { isArray, options: fieldOptions } = definition;
|
|
12
|
+
const go = `${isArray ? '[]' : ''}float64`;
|
|
13
|
+
let validation = undefined;
|
|
14
|
+
if (language === 'go') {
|
|
15
|
+
validation = [];
|
|
16
|
+
if (fieldOptions?.min) {
|
|
17
|
+
validation.push(`gte=${fieldOptions.min}`);
|
|
18
|
+
}
|
|
19
|
+
if (fieldOptions?.max) {
|
|
20
|
+
validation.push(`lte=${fieldOptions.max}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
10
23
|
return {
|
|
11
|
-
valueType: `number${
|
|
24
|
+
valueType: language === 'go' ? go : `number${isArray ? '[]' : ''}`,
|
|
25
|
+
validation,
|
|
12
26
|
};
|
|
13
27
|
}
|
|
14
28
|
toValueType(value) {
|
|
@@ -40,8 +40,13 @@ const formatPhoneNumber_1 = __importStar(require("../utilities/formatPhoneNumber
|
|
|
40
40
|
const AbstractField_1 = __importDefault(require("./AbstractField"));
|
|
41
41
|
class PhoneField extends AbstractField_1.default {
|
|
42
42
|
static generateTemplateDetails(options) {
|
|
43
|
+
const { definition, language } = options;
|
|
44
|
+
const { isArray } = definition;
|
|
45
|
+
const arrayNotation = isArray ? '[]' : '';
|
|
43
46
|
return {
|
|
44
|
-
valueType:
|
|
47
|
+
valueType: language === 'go'
|
|
48
|
+
? `${arrayNotation}string`
|
|
49
|
+
: `string${arrayNotation}`,
|
|
45
50
|
};
|
|
46
51
|
}
|
|
47
52
|
toValueType(value) {
|
|
@@ -30,12 +30,18 @@ class SelectField extends AbstractField_1.default {
|
|
|
30
30
|
return errors;
|
|
31
31
|
}
|
|
32
32
|
static generateTemplateDetails(options) {
|
|
33
|
-
const { definition } = options;
|
|
33
|
+
const { definition, language } = options;
|
|
34
34
|
const { isArray, options: { choices }, } = definition;
|
|
35
|
+
const arrayNotation = isArray ? '[]' : '';
|
|
36
|
+
if (language === 'go') {
|
|
37
|
+
return {
|
|
38
|
+
valueType: `${arrayNotation}string`,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
35
41
|
return {
|
|
36
42
|
valueType: `(${choices
|
|
37
43
|
.map((choice) => `${typeof choice.value === 'number' ? choice.value : `"${choice.value}"`}`)
|
|
38
|
-
.join(' | ')})${
|
|
44
|
+
.join(' | ')})${arrayNotation}`,
|
|
39
45
|
};
|
|
40
46
|
}
|
|
41
47
|
getChoices() {
|
|
@@ -7,9 +7,12 @@ const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
|
|
7
7
|
const AbstractField_1 = __importDefault(require("./AbstractField"));
|
|
8
8
|
class TextField extends AbstractField_1.default {
|
|
9
9
|
static generateTemplateDetails(options) {
|
|
10
|
-
const { definition } = options;
|
|
10
|
+
const { definition, language } = options;
|
|
11
|
+
const valueType = language === 'go'
|
|
12
|
+
? `${definition.isArray ? '[]' : ''}string`
|
|
13
|
+
: `string${definition.isArray ? '[]' : ''}`;
|
|
11
14
|
return {
|
|
12
|
-
valueType
|
|
15
|
+
valueType,
|
|
13
16
|
};
|
|
14
17
|
}
|
|
15
18
|
validate(value, options) {
|
|
@@ -1,23 +1,25 @@
|
|
|
1
1
|
import { FieldDefinitions } from '../fields';
|
|
2
2
|
import { Schema } from '../schemas.static.types';
|
|
3
3
|
export interface FieldTemplateDetails {
|
|
4
|
-
/** The type of value (string, number) */
|
|
4
|
+
/** The type literal of value (string, number) */
|
|
5
5
|
valueType: string;
|
|
6
|
+
/** The unique validation tags for the field (currently go only) */
|
|
7
|
+
validation?: string[];
|
|
6
8
|
}
|
|
7
9
|
/** How are we being rendered in the template */
|
|
8
10
|
export declare enum TemplateRenderAs {
|
|
9
|
-
/** We are rendering type
|
|
11
|
+
/** We are rendering type literal */
|
|
10
12
|
Type = "type",
|
|
11
|
-
/** We are rendering as a value (only called if a value is set in the schema definition) */
|
|
13
|
+
/** We are rendering as a value (only called if a value is set in the schema definition, like defaultValue) */
|
|
12
14
|
Value = "value",
|
|
13
|
-
/** We are rendering as the
|
|
15
|
+
/** We are rendering as the schema's field type (Schema) */
|
|
14
16
|
SchemaType = "schemaType"
|
|
15
17
|
}
|
|
18
|
+
export type TemplateLanguage = 'ts' | 'go';
|
|
16
19
|
/** The shape of options passed to AbstractField.generateTemplateDetails(options) */
|
|
17
20
|
export interface FieldTemplateDetailOptions<T extends FieldDefinitions> {
|
|
18
21
|
/** The language we're generating to, only TS for now */
|
|
19
|
-
language:
|
|
20
|
-
/** All other schemas schemas being rendered */
|
|
22
|
+
language: TemplateLanguage;
|
|
21
23
|
templateItems: SchemaTemplateItem[];
|
|
22
24
|
/** The global namespace to access items */
|
|
23
25
|
globalNamespace: string;
|
|
@@ -4,10 +4,10 @@ exports.TemplateRenderAs = void 0;
|
|
|
4
4
|
/** How are we being rendered in the template */
|
|
5
5
|
var TemplateRenderAs;
|
|
6
6
|
(function (TemplateRenderAs) {
|
|
7
|
-
/** We are rendering type
|
|
7
|
+
/** We are rendering type literal */
|
|
8
8
|
TemplateRenderAs["Type"] = "type";
|
|
9
|
-
/** We are rendering as a value (only called if a value is set in the schema definition) */
|
|
9
|
+
/** We are rendering as a value (only called if a value is set in the schema definition, like defaultValue) */
|
|
10
10
|
TemplateRenderAs["Value"] = "value";
|
|
11
|
-
/** We are rendering as the
|
|
11
|
+
/** We are rendering as the schema's field type (Schema) */
|
|
12
12
|
TemplateRenderAs["SchemaType"] = "schemaType";
|
|
13
13
|
})(TemplateRenderAs || (exports.TemplateRenderAs = TemplateRenderAs = {}));
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"!build/__tests__",
|
|
9
9
|
"esm"
|
|
10
10
|
],
|
|
11
|
-
"version": "32.
|
|
11
|
+
"version": "32.3.0",
|
|
12
12
|
"main": "./build/index.js",
|
|
13
13
|
"types": "./build/index.d.ts",
|
|
14
14
|
"module": "./build/esm/index.js",
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"url": "https://github.com/sprucelabsai/spruce-schema/issues"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
|
-
"build.ci": "yarn run build.tsc && yarn run build.resolve-paths && yarn run lint",
|
|
36
|
-
"build.dev": "yarn run build.tsc --sourceMap ; yarn run resolve-paths.lint",
|
|
35
|
+
"build.ci": "yarn run build.tsc && yarn run build.resolve-paths && yarn build.copy-files && yarn run lint",
|
|
36
|
+
"build.dev": "yarn run build.tsc --sourceMap ; yarn run resolve-paths.lint ; yarn run build.copy-files",
|
|
37
37
|
"build.dist": "tsc --project tsconfig.dist.json && yarn build.resolve-paths && mv build esm && yarn build.esm-postbuild && yarn build.tsc && yarn build.resolve-paths && mv esm build/ && yarn clean.dist",
|
|
38
38
|
"build.esm-postbuild": "esm-postbuild --target esm --patterns '**/*.js'",
|
|
39
39
|
"build.resolve-paths": "resolve-path-aliases --target build --patterns '**/*.js,**/*.d.ts'",
|
|
@@ -52,14 +52,14 @@
|
|
|
52
52
|
"update.dependencies": "yarn run clean.dependencies && yarn",
|
|
53
53
|
"watch.build.dev": "tsc-watch --sourceMap --onCompilationComplete 'yarn run post.watch.build'",
|
|
54
54
|
"watch.tsc": "tsc -w",
|
|
55
|
-
"post.watch.build": "yarn build.resolve-paths",
|
|
55
|
+
"post.watch.build": "yarn build.resolve-paths && yarn build.copy-files",
|
|
56
56
|
"resolve-paths.lint": "yarn run build.resolve-paths ; yarn run lint",
|
|
57
57
|
"watch.rebuild": "yarn run clean.all && yarn install && yarn run watch.build.dev",
|
|
58
58
|
"build.copy-files": "mkdir -p build && rsync -avzq --exclude='*.ts' ./src/ ./build/"
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
61
|
"@sprucelabs/error": "^7.1.32",
|
|
62
|
-
"@sprucelabs/test-utils": "^6.0.
|
|
62
|
+
"@sprucelabs/test-utils": "^6.0.105",
|
|
63
63
|
"email-validator": "^2.0.4",
|
|
64
64
|
"just-safe-get": "^4.2.0",
|
|
65
65
|
"just-safe-set": "^4.2.1"
|