Skip to content

Commit

Permalink
Allow unknown shapes/layers (#47)
Browse files Browse the repository at this point in the history
* Unknown layers and shapes will no longer fail spec

* Add test files for unknown shape + layer

* Dynamically generate unknown layers/shape list

* Uncomment out adding unknown objects

* WIP - Add type enums to existing 'unkown' object

* Update unknown object generation and validation
  • Loading branch information
b-wils committed Jul 9, 2024
1 parent 503a8fc commit 4a3f142
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 2 deletions.
3 changes: 3 additions & 0 deletions schema/layers/all-layers.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
},
{
"$ref": "#/$defs/layers/shape-layer"
},
{
"$ref": "#/$defs/layers/unknown-layer"
}
]
}
14 changes: 14 additions & 0 deletions schema/layers/unknown-layer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "Unknown layer types",
"description": "Unknown layer types. Types not defined by the specification are still allowed.",
"properties": {
"ty": {
"not": {
"$comment": "enum list is dynamically generated",
"enum": []
}
}
}
}
3 changes: 2 additions & 1 deletion schema/shapes/all-graphic-elements.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
{"$ref": "#/$defs/shapes/rectangle"},
{"$ref": "#/$defs/shapes/stroke"},
{"$ref": "#/$defs/shapes/transform"},
{"$ref": "#/$defs/shapes/trim-path"}
{"$ref": "#/$defs/shapes/trim-path"},
{"$ref": "#/$defs/shapes/unknown-shape"}
]
}
14 changes: 14 additions & 0 deletions schema/shapes/unknown-shape.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"title": "Unknown shape types",
"description": "Unknown shape types. Types not defined by the specification are still allowed.",
"properties": {
"ty": {
"not": {
"$comment": "enum list is dynamically generated",
"enum": []
}
}
}
}
25 changes: 25 additions & 0 deletions tests/animations/valid/unknown-layer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"v": "5.5.7",
"ip": 0,
"op": 180,
"nm": "Animation",
"mn": "{8f1618e3-6f83-4531-8f65-07dd4b68ee2e}",
"fr": 60,
"w": 512,
"h": 512,
"assets": [
],
"layers": [
{
"ddd": 0,
"ty": 137,
"ind": 0,
"st": 0,
"ip": 0,
"op": 180,
"nm": "Unknown layer type"
}
]
}


63 changes: 63 additions & 0 deletions tests/animations/valid/unknown-shape.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"v": "5.5.7",
"ip": 0,
"op": 180,
"nm": "Animation",
"mn": "{8f1618e3-6f83-4531-8f65-07dd4b68ee2e}",
"fr": 60,
"w": 512,
"h": 512,
"assets": [
],
"layers": [
{
"ddd": 0,
"ty": 4,
"ind": 0,
"st": 0,
"ip": 0,
"op": 180,
"nm": "Shape Layer",
"mn": "{85f37d8b-1792-4a4f-82d2-1b3b6d829c07}",
"ks": {
"a": {
"a": 0,
"k": [
256,
256
]
},
"p": {
"a": 0,
"k": [
256,
256
]
},
"s": {
"a": 0,
"k": [
100,
100
]
},
"r": {
"a": 0,
"k": 0
},
"o": {
"a": 0,
"k": 100
}
},
"shapes": [
{
"ty": "unknown",
"nm": "Unknown Shape"
}
]
}
]
}


26 changes: 26 additions & 0 deletions tools/schema-merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import pathlib
import argparse

from schema_tools.schema import SchemaPath, Schema
from schema_tools import type_info

def join_parts(
json_data: dict,
Expand All @@ -30,7 +32,19 @@ def join_parts(

return json_data

def add_vals_to_unknown_object(
objects,
unknown_type_dict: dict
):
types = []

for ele in objects.concrete:
type = ele.properties['ty'].const
if type is not None:
types.append(type)

unknown_type_dict["properties"]["ty"]["not"]["enum"] = types

root = pathlib.Path(__file__).absolute().parent.parent

parser = argparse.ArgumentParser(description="Joins JSON schema in a single file")
Expand Down Expand Up @@ -63,6 +77,18 @@ def join_parts(

join_parts(json_data, input_dir, root_path)

schema = Schema(json_data)
ts = type_info.TypeSystem(schema)

add_vals_to_unknown_object(
ts.from_path(SchemaPath("#/$defs/layers/all-layers")),
json_data["$defs"]["layers"]["unknown-layer"]
)
add_vals_to_unknown_object(
ts.from_path(SchemaPath("#/$defs/shapes/all-graphic-elements")),
json_data["$defs"]["shapes"]["unknown-shape"]
)

os.makedirs(output_path.parent, exist_ok=True)

with open(output_path, "w") as file:
Expand Down
10 changes: 9 additions & 1 deletion tools/schema-validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
from schema_tools.schema import SchemaPath, Schema
import lottie_markdown

# By default, tool expects a link for all schema files.
# This is generally true, but may not always be the case
unneededLinks = [
("shapes","base-gradient"),
("layers","unknown-layer"),
("shapes","unknown-shape")
]

class Validator:
def __init__(self):
Expand Down Expand Up @@ -65,7 +72,8 @@ def check_links(self, html_path: pathlib.Path):
file_cache = {}
ts = lottie_markdown.typed_schema(self.root)

checked.add(("shapes","base-gradient"))
for link in unneededLinks:
checked.add(link)

for ref in self.expected_refs:
link = ts.from_path(ref).link
Expand Down

0 comments on commit 4a3f142

Please sign in to comment.