diff --git a/src/fill.ts b/src/fill.ts index e20add2..7160d1b 100644 --- a/src/fill.ts +++ b/src/fill.ts @@ -1,10 +1,15 @@ import { curry, difference, keys, reduce } from 'ramda' import { getObjectSchema } from './api' -import { ObjectSchema, SchemaCollection, SchemaVersion } from './objects' +import { + ObjectSchema, + PlainObject, + SchemaCollection, + SchemaVersion, +} from './objects' // TODO: add types to input args export const fillBySchema = curry( - (schema: ObjectSchema, object: object): object => { + (schema: ObjectSchema, object: PlainObject): PlainObject => { // @ts-ignore schema = schema.properties || (schema.schema || schema.items).properties const objectProps = keys(object) @@ -12,7 +17,7 @@ export const fillBySchema = curry( const missingProperties = difference(schemaProps, objectProps) const filledObject = reduce( - (result: object, key: string): object => { + (result: PlainObject, key: string): PlainObject => { const property = schema[key] if ('defaultValue' in property) { const value = property.defaultValue @@ -27,7 +32,7 @@ export const fillBySchema = curry( missingProperties, ) - return filledObject + return filledObject }, ) @@ -36,7 +41,7 @@ const fillObject = ( schemaName: string, version: SchemaVersion, object: object, -) => { +): PlainObject => { const schema = getObjectSchema(schemas, schemaName, version) if (!schema) { throw new Error( @@ -47,7 +52,7 @@ const fillObject = ( throw new Error('Expected an object to trim') } - return fillBySchema(schema, object) + return fillBySchema(schema, object) } /** diff --git a/src/trim.ts b/src/trim.ts index 6951475..e76a38b 100644 --- a/src/trim.ts +++ b/src/trim.ts @@ -1,41 +1,48 @@ import { contains, curry, keys, map, reduce } from 'ramda' import { getObjectSchema } from './api' -import { ObjectSchema, SchemaCollection, SchemaVersion } from './objects' +import { + ObjectSchema, + PlainObject, + SchemaCollection, + SchemaVersion, +} from './objects' import { hasPropertiesArray, isJsonSchema } from './sanitize' // TODO: add types to input args /** * Takes an object and removes all properties not listed in the schema */ -export const trimBySchema = curry((schema: ObjectSchema, object: object) => { - // @ts-ignore - schema = schema.properties || (schema.schema || schema.items).properties - const objectProps = keys(object) - const schemaProps = keys(schema) - return reduce( - (trimmedObj, prop) => { - if (contains(prop, schemaProps)) { - if (object[prop] && isJsonSchema(schema[prop])) { - trimmedObj[prop] = trimBySchema(schema[prop], object[prop]) - } else if (object[prop] && hasPropertiesArray(schema[prop])) { - trimmedObj[prop] = map(trimBySchema(schema[prop]), object[prop]) - } else { - trimmedObj[prop] = object[prop] +export const trimBySchema = curry( + (schema: ObjectSchema, object: object): PlainObject => { + // @ts-ignore + schema = schema.properties || (schema.schema || schema.items).properties + const objectProps = keys(object) + const schemaProps = keys(schema) + return reduce( + (trimmedObj, prop) => { + if (contains(prop, schemaProps)) { + if (object[prop] && isJsonSchema(schema[prop])) { + trimmedObj[prop] = trimBySchema(schema[prop], object[prop]) + } else if (object[prop] && hasPropertiesArray(schema[prop])) { + trimmedObj[prop] = map(trimBySchema(schema[prop]), object[prop]) + } else { + trimmedObj[prop] = object[prop] + } } - } - return trimmedObj - }, - {}, - objectProps, - ) -}) + return trimmedObj + }, + {}, + objectProps, + ) + }, +) const trimObject = ( schemas: SchemaCollection, schemaName: string, version: SchemaVersion, - object: object, -) => { + object: PlainObject, +): PlainObject => { const schema = getObjectSchema(schemas, schemaName, version) if (!schema) { throw new Error( @@ -55,6 +62,6 @@ const trimObject = ( * @example * const o = ... // some object * const t = trim('Person', '1.0.0', o) - * // t only has properties from the schema Person@1.0.0 + * // t only has properties from the schema Person v1.0.0 */ export const trim = curry(trimObject)