Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
fix: set PlainObject type on trim and fill
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Oct 3, 2018
1 parent 13dd9da commit e8edbe0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 31 deletions.
17 changes: 11 additions & 6 deletions src/fill.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
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)
const schemaProps = keys(schema)

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
Expand All @@ -27,7 +32,7 @@ export const fillBySchema = curry(
missingProperties,
)

return filledObject
return <PlainObject>filledObject
},
)

Expand All @@ -36,7 +41,7 @@ const fillObject = (
schemaName: string,
version: SchemaVersion,
object: object,
) => {
): PlainObject => {
const schema = getObjectSchema(schemas, schemaName, version)
if (!schema) {
throw new Error(
Expand All @@ -47,7 +52,7 @@ const fillObject = (
throw new Error('Expected an object to trim')
}

return fillBySchema(schema, object)
return fillBySchema(schema, <PlainObject>object)
}

/**
Expand Down
57 changes: 32 additions & 25 deletions src/trim.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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)

0 comments on commit e8edbe0

Please sign in to comment.