Skip to content

Commit 2f05828

Browse files
gsi-alejandroejscribner
authored andcommitted
fix(schema): set correct options for ArrayType
Closes: #716
1 parent aba3e85 commit 2f05828

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

__test__/schema-cast.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,14 @@ test('support array of primitive types', () => {
4848
coordinates: {
4949
type: [Number],
5050
required: true,
51+
default: [0],
5152
},
53+
articles: { default: () => [], type: [{ type: String, ref: 'Article' }] },
5254
});
5355
const fields = schema.fields;
5456
expect(fields.coordinates.typeName).toBe(Array.name);
57+
expect((fields.articles as ArrayType).options).toBeDefined();
58+
expect((fields.articles as ArrayType).options!.default).toBeInstanceOf(Function);
5559
expect((fields.coordinates as ArrayType).itemType.typeName).toBe(Number.name);
5660
});
5761

src/schema/helpers/fn-schema.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { is, isSchemaFactoryType } from '../../utils';
22
import { BuildSchemaError, ValidationError } from '../errors';
33
import { Schema } from '../schema';
44
import { CoreType } from '../types';
5-
import { CustomValidations, FactoryFunction, FieldMap, IOttomanType, SchemaDef } from '../interfaces/schema.types';
5+
import {
6+
CoreTypeOptions,
7+
CustomValidations,
8+
FactoryFunction,
9+
FieldMap,
10+
IOttomanType,
11+
SchemaDef,
12+
} from '../interfaces/schema.types';
613
import { cast, CAST_STRATEGY, CastOptions } from '../../utils/cast-strategy';
714

815
type ParseResult = {
@@ -37,7 +44,7 @@ export const buildFields = (obj: Schema | SchemaDef, strict = true): FieldMap =>
3744
if (!opts.type) {
3845
throw new BuildSchemaError(`Property '${_key}' is a required type`);
3946
}
40-
fields[_key] = _makeField(_key, opts);
47+
fields[_key] = _makeField(_key, opts, obj[_key]);
4148
}
4249
return fields;
4350
};
@@ -105,11 +112,14 @@ const _getFieldType = (type: any): any => {
105112
* @param def result of parsing the field schema
106113
* @throws BuildSchemaError
107114
*/
108-
const _makeField = (name: string, def: ParseResult): IOttomanType => {
115+
const _makeField = (name: string, def: ParseResult, arrayOptions?: CoreTypeOptions): IOttomanType => {
109116
const typeFactory = Schema.FactoryTypes[String(def.type)];
110117
if (typeFactory === undefined) {
111118
throw new BuildSchemaError(`Unsupported type specified in the property '${name}'`);
112119
}
120+
if (arrayOptions !== undefined) {
121+
return typeFactory(name, def.options, arrayOptions);
122+
}
113123
return typeFactory(name, def.options);
114124
};
115125
/**

src/schema/types/array-type.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ export class ArrayType extends CoreType {
5252
}
5353
}
5454

55-
export const arrayTypeFactory = (name: string, item: CoreType): ArrayType => new ArrayType(name, item);
55+
export const arrayTypeFactory = (name: string, item: CoreType, options?: CoreTypeOptions): ArrayType =>
56+
new ArrayType(name, item, options);

0 commit comments

Comments
 (0)