Skip to content

Commit

Permalink
self-review: fix asserts, add examples to documentation, add document…
Browse files Browse the repository at this point in the history
…ation examples to tests
  • Loading branch information
Joshua Pollak committed Sep 27, 2019
1 parent a94d811 commit 3d37dea
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/core/serialize.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
import { invariant, isPrimitive } from "../utils/utils"
import getDefaultModelSchema from "../api/getDefaultModelSchema"
import { SKIP, _defaultPrimitiveProp } from "../constants"

/**
* Serializes an object (graph) into json using the provided model schema.
* The model schema can be omitted if the object type has a default model schema associated with it.
Expand Down Expand Up @@ -36,7 +36,7 @@ export default function serialize(arg1, arg2) {
}

export function checkStarSchemaInvariant(propDef) {
invariant(propDef === true || propDef.pattern, `prop schema '*' can only be used with 'true': ${JSON.stringify(propDef)}`)
invariant(propDef === true || propDef.pattern, `prop schema '*' can only be used with 'true' or a prop def with a 'pattern': ${JSON.stringify(propDef)}`)
}

export function serializeWithSchema(schema, obj) {
Expand Down
21 changes: 19 additions & 2 deletions src/core/serializeAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import setDefaultModelSchema from "../api/setDefaultModelSchema"
import object from "../types/object"

/**
* The `serializeAll` decorator can be used on a class to signal that all primitive properties should be serialized automatically.
* The `serializeAll` decorator can may used on a class to signal that all primitive properties,
* or complex properties with a name matching a `pattern`, should be serialized automatically.
*
* @example
* @serializeAll class Store {
Expand All @@ -16,7 +17,23 @@ import object from "../types/object"
* const store = new Store();
* store.c = 5;
* store.d = {};
* t.deepEqual(serialize(store), { a: 3, b: undefined, c: 5 });
* t.deepEqual(serialize(store), { c: 5 });
*
* @example
* class DataType {
* @serializable
* x;
* @serializable
* y;
* }
* @serializeAll(/^[a-z]$/, DataType) class ComplexStore {
* }
*
* const store = new ComplexStore();
* store.a = {x: 1, y: 2};
* store.b = {};
* store.somethingElse = 5;
* t.deepEqual(serialize(store), { a: {x: 1, y: 2}, b: { x: undefined, y: undefined } });
*/
export default function serializeAll(targetOrPattern, clazzOrSchema) {
let propSchema;
Expand Down
29 changes: 29 additions & 0 deletions test/typescript/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,32 @@ test("[ts] @serializeAll(list schema)", t => {

t.end()
})

test("[ts] tests from serializeAll documentation", t => {
@serializeAll class Store {
[key: string]: number;
}

const store = new Store();
store.c = 5;
(store as any).d = {};
t.deepEqual(serialize(store), { c: 5 });

class DataType {
@serializable
x?: number;
@serializable(optional())
y?: number;
}
@serializeAll(/^[a-z]$/, DataType) class ComplexStore {
[key: string]: DataType;
}

const complexStore = new ComplexStore();
complexStore.a = {x: 1, y: 2};
complexStore.b = {};
(complexStore as any).somethingElse = 5;
t.deepEqual(serialize(complexStore), { a: {x: 1, y: 2}, b: { x: undefined } });

t.end();
})

0 comments on commit 3d37dea

Please sign in to comment.