Comparing version 1.0.1 to 1.1.0
@@ -276,3 +276,3 @@ "use strict"; | ||
var schema = state.schemaDef; | ||
var listSchema = schema.items; | ||
var listSchema = schema.items || schema; | ||
var tupleSchema = []; | ||
@@ -307,3 +307,3 @@ var isTuple = Array.isArray(schema.items); | ||
// Get typescript type for the schema definition | ||
var schemaType = this.getTypeName(state.schemaDef, state); | ||
var schemaType = this.getTypeName(state.schemaDef, data, state); | ||
if (schemaType) { | ||
@@ -334,3 +334,3 @@ var circularRef = this.isCircularReference(schemaType, state); | ||
*/ | ||
Scrubbr.prototype.getTypeName = function (schema, state) { | ||
Scrubbr.prototype.getTypeName = function (schema, data, state) { | ||
var _this = this; | ||
@@ -347,6 +347,13 @@ if (!schema) { | ||
} | ||
if (schema.items) { | ||
schemaList.push(schema.items); | ||
} | ||
// Get all the types we have definitions for | ||
var foundTypes = new Map(); | ||
var dataIsArray = Array.isArray(data); | ||
schemaList.forEach(function (schemaRef) { | ||
var refPath = schemaRef.$ref; | ||
var _a; | ||
var refPath = dataIsArray | ||
? (_a = schemaRef.items) === null || _a === void 0 ? void 0 : _a.$ref | ||
: schemaRef.$ref; | ||
if (!refPath) { | ||
@@ -388,3 +395,3 @@ return; | ||
else { | ||
state.logger.debug("Type: '" + chosenType + "'"); | ||
state.logger.debug("Type: " + chosenType + (dataIsArray ? '[]' : '')); | ||
} | ||
@@ -391,0 +398,0 @@ return chosenType; |
{ | ||
"name": "scrubbr", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Serialize and sanitize JSON data using TypeScript.", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/jgillick/scrubbr", |
@@ -313,3 +313,3 @@ import * as fs from 'fs'; | ||
const schema = state.schemaDef; | ||
const listSchema = schema.items as JSONSchema7 | JSONSchema7[]; | ||
const listSchema = (schema.items as JSONSchema7 | JSONSchema7[]) || schema; | ||
@@ -357,3 +357,3 @@ let tupleSchema: JSONSchema7[] = []; | ||
// Get typescript type for the schema definition | ||
const schemaType = this.getTypeName(state.schemaDef, state); | ||
const schemaType = this.getTypeName(state.schemaDef, data, state); | ||
if (schemaType) { | ||
@@ -391,3 +391,7 @@ const circularRef = this.isCircularReference(schemaType, state); | ||
*/ | ||
private getTypeName(schema: JSONSchema7, state: ScrubbrState): string | null { | ||
private getTypeName( | ||
schema: JSONSchema7, | ||
data: unknown, | ||
state: ScrubbrState | ||
): string | null { | ||
if (!schema) { | ||
@@ -405,7 +409,13 @@ return null; | ||
} | ||
if (schema.items) { | ||
schemaList.push(schema.items as JSONSchema7); | ||
} | ||
// Get all the types we have definitions for | ||
const foundTypes = new Map<string, JSONSchema7>(); | ||
const dataIsArray = Array.isArray(data); | ||
schemaList.forEach((schemaRef) => { | ||
const refPath = schemaRef.$ref; | ||
const refPath = dataIsArray | ||
? (schemaRef.items as JSONSchema7)?.$ref | ||
: schemaRef.$ref; | ||
if (!refPath) { | ||
@@ -451,3 +461,3 @@ return; | ||
} else { | ||
state.logger.debug(`Type: '${chosenType}'`); | ||
state.logger.debug(`Type: ${chosenType}${dataIsArray ? '[]' : ''}`); | ||
} | ||
@@ -454,0 +464,0 @@ |
@@ -5,3 +5,3 @@ import 'jest'; | ||
const scrubbr = new Scrubbr(`${__dirname}/recordType.schema.ts`, { | ||
logLevel: LogLevel.DEBUG, | ||
logLevel: LogLevel.INFO, | ||
}); | ||
@@ -8,0 +8,0 @@ |
@@ -1,17 +0,25 @@ | ||
type UnionTypeTestSimple = { | ||
export type UnionTypeTestSimple = { | ||
value: UnionTypeTestType1 | UnionTypeTestType2; | ||
}; | ||
type UnionTypeTestPrimitive = { | ||
export type UnionTypeTestPrimitive = { | ||
value: string | boolean; | ||
}; | ||
type UnionTypeTestMixed = { | ||
export type UnionTypeTestMixed = { | ||
value: UnionTypeTestType1 | null; | ||
}; | ||
type UnionTypeTestAlias = { | ||
export type UnionTypeTestAlias = { | ||
value: AliasedUnion; | ||
}; | ||
export type UnionTypeSingularAndArray = { | ||
value: UnionTypeTestType1 | UnionTypeTestType2[]; | ||
}; | ||
export type UnionTypeArrayAndNull = { | ||
value: UnionTypeTestType2[] | null; | ||
}; | ||
type UnionTypeTestType1 = { | ||
@@ -18,0 +26,0 @@ nodeA: string; |
@@ -10,3 +10,5 @@ import 'jest'; | ||
beforeEach(() => { | ||
scrubbr = new Scrubbr(`${__dirname}/unionType.schema.ts`); | ||
scrubbr = new Scrubbr(`${__dirname}/unionType.schema.ts`, { | ||
logLevel: LogLevel.INFO, | ||
}); | ||
@@ -75,2 +77,44 @@ // Override this later | ||
describe('singular and array', () => { | ||
test('with array value', () => { | ||
const serialized = scrubbr.serialize('UnionTypeSingularAndArray', { | ||
value: [{ nodeA: 'foo' }, { nodeA: 'bar' }], | ||
}); | ||
expect(serialized.value.length).toBe(2); | ||
expect(serialized.value[0].nodeA).toBe('foo'); | ||
expect(serialized.value[1].nodeA).toBe('bar'); | ||
}); | ||
test('with singular value', () => { | ||
const serialized = scrubbr.serialize('UnionTypeSingularAndArray', { | ||
value: { nodeA: 'foo', nodeB: 'bar' }, | ||
}); | ||
expect(Array.isArray(serialized.value)).toBe(false); | ||
expect(serialized.value.nodeA).toBe('foo'); | ||
expect(serialized.value.nodeB).toBe('bar'); | ||
}); | ||
}); | ||
describe('array and null', () => { | ||
test('array value', () => { | ||
const serialized = scrubbr.serialize('UnionTypeArrayAndNull', { | ||
value: [{ nodeA: 'foo' }, { nodeA: 'bar' }], | ||
}); | ||
expect(serialized.value.length).toBe(2); | ||
expect(serialized.value[0].nodeA).toBe('foo'); | ||
expect(serialized.value[1].nodeA).toBe('bar'); | ||
}); | ||
test('null value', () => { | ||
const serialized = scrubbr.serialize('UnionTypeArrayAndNull', { | ||
value: null, | ||
}); | ||
expect(serialized.value).toBe(null); | ||
}); | ||
}); | ||
// test('change type and override value', () => { | ||
@@ -77,0 +121,0 @@ // firstSerializerFn.mockImplementation((data, state) => { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1076874
3006