-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split blueprint and field so schema is attached
- Loading branch information
1 parent
3788e26
commit 1981932
Showing
12 changed files
with
168 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Record, Map, List } from "immutable"; | ||
|
||
const DEFAULTS = { | ||
key: null, | ||
type: null, | ||
children: null, | ||
props: null | ||
}; | ||
|
||
export default class Field extends Record(DEFAULTS) { | ||
static create(field = {}) { | ||
if (Field.isField(field)) { | ||
return field; | ||
} | ||
|
||
if (typeof field !== "object" || field == null) { | ||
throw new Error("Expecting object for field."); | ||
} | ||
|
||
let { | ||
key=null, | ||
type=null, | ||
children, | ||
...props | ||
} = field; | ||
|
||
children = Field.createList(children); | ||
props = Map.isMap(props) ? props : Map(props); | ||
|
||
return new Field({ key, type, props, children }); | ||
} | ||
|
||
static createList(fields) { | ||
if (List.isList(fields)) return fields; | ||
|
||
if (Array.isArray(fields)) { | ||
fields = fields.map(Field.create); | ||
} else if (typeof fields === "object" && fields != null) { | ||
fields = Object.keys(fields).map(key => { | ||
return Field.create({ ...fields[key], key }); | ||
}); | ||
} | ||
|
||
return List(fields); | ||
} | ||
|
||
static isField(b) { | ||
return b instanceof Field; | ||
} | ||
|
||
static kind() { | ||
return "field"; | ||
} | ||
|
||
getChildField(key) { | ||
return this.children.find(o => o.key === key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import Blueprint from "./blueprint"; | ||
import Schema, {defaultSchema} from "./schema"; | ||
import Field from "./field"; | ||
import * as rules from "./rules/index"; | ||
|
||
export { | ||
Blueprint, | ||
Schema, | ||
Field, | ||
defaultSchema, | ||
rules | ||
}; | ||
|
||
export function normalize(blueprint, schema=defaultSchema) { | ||
blueprint = Blueprint.create(blueprint); | ||
schema = Schema.create(schema); | ||
return schema.normalize(blueprint); | ||
export default function(field, schema=defaultSchema) { | ||
return Blueprint.create({ root: field, schema }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,21 @@ | ||
import legacyRoot from "./legacy-root"; | ||
import legacySection from "./legacy-section"; | ||
import legacyRoot from "./legacy-root"; | ||
import section from "./section"; | ||
import sectionList from "./section-list"; | ||
import list from "./list"; | ||
import defaults from "./defaults"; | ||
|
||
export { | ||
legacyRoot, | ||
legacySection, | ||
legacyRoot, | ||
section, | ||
sectionList, | ||
list, | ||
defaults | ||
}; | ||
|
||
export default [ | ||
legacyRoot, | ||
legacySection, | ||
legacyRoot, | ||
section, | ||
sectionList, | ||
list, | ||
defaults | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.