Skip to content

Commit

Permalink
fix(schema): set correct options for ArrayType
Browse files Browse the repository at this point in the history
Closes: #716
  • Loading branch information
gsi-alejandro authored and ejscribner committed Apr 4, 2023
1 parent aba3e85 commit 2f05828
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions __test__/schema-cast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ test('support array of primitive types', () => {
coordinates: {
type: [Number],
required: true,
default: [0],
},
articles: { default: () => [], type: [{ type: String, ref: 'Article' }] },
});
const fields = schema.fields;
expect(fields.coordinates.typeName).toBe(Array.name);
expect((fields.articles as ArrayType).options).toBeDefined();
expect((fields.articles as ArrayType).options!.default).toBeInstanceOf(Function);
expect((fields.coordinates as ArrayType).itemType.typeName).toBe(Number.name);
});

Expand Down
16 changes: 13 additions & 3 deletions src/schema/helpers/fn-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { is, isSchemaFactoryType } from '../../utils';
import { BuildSchemaError, ValidationError } from '../errors';
import { Schema } from '../schema';
import { CoreType } from '../types';
import { CustomValidations, FactoryFunction, FieldMap, IOttomanType, SchemaDef } from '../interfaces/schema.types';
import {
CoreTypeOptions,
CustomValidations,
FactoryFunction,
FieldMap,
IOttomanType,
SchemaDef,
} from '../interfaces/schema.types';
import { cast, CAST_STRATEGY, CastOptions } from '../../utils/cast-strategy';

type ParseResult = {
Expand Down Expand Up @@ -37,7 +44,7 @@ export const buildFields = (obj: Schema | SchemaDef, strict = true): FieldMap =>
if (!opts.type) {
throw new BuildSchemaError(`Property '${_key}' is a required type`);
}
fields[_key] = _makeField(_key, opts);
fields[_key] = _makeField(_key, opts, obj[_key]);
}
return fields;
};
Expand Down Expand Up @@ -105,11 +112,14 @@ const _getFieldType = (type: any): any => {
* @param def result of parsing the field schema
* @throws BuildSchemaError
*/
const _makeField = (name: string, def: ParseResult): IOttomanType => {
const _makeField = (name: string, def: ParseResult, arrayOptions?: CoreTypeOptions): IOttomanType => {
const typeFactory = Schema.FactoryTypes[String(def.type)];
if (typeFactory === undefined) {
throw new BuildSchemaError(`Unsupported type specified in the property '${name}'`);
}
if (arrayOptions !== undefined) {
return typeFactory(name, def.options, arrayOptions);
}
return typeFactory(name, def.options);
};
/**
Expand Down
3 changes: 2 additions & 1 deletion src/schema/types/array-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ export class ArrayType extends CoreType {
}
}

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

0 comments on commit 2f05828

Please sign in to comment.