Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minor issues with the python schema typesystem #113

Merged
merged 4 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions tests/validate_animations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,32 @@ const ajv = new Ajv({
});
const validate = ajv.compile(schema);

expect.extend({
toBeValid(data) {

if ( validate(data) )
{
return {
message: () => "data NOT be valid",
pass: true
};
}

return {
message: () => "Invalid data: " + JSON.stringify(validate.errors, null, 4),
pass: false
};
}
});

describe('run schema validation', () => {
describe('example animations', () => {
const exampleFiles = fs.readdirSync(EXAMPLES_DIR).map(file => EXAMPLES_DIR + file);

exampleFiles.forEach((file) => {
test(file, () => {
const animation = fs.readFileSync(file, 'utf8');
const valid = validate(JSON.parse(animation));

expect(valid).toBe(true);
expect(JSON.parse(animation)).toBeValid();
});
});
});
Expand All @@ -33,7 +49,7 @@ describe('run schema validation', () => {
test(file, () => {
const animation = fs.readFileSync(file, 'utf8');
const valid = validate(JSON.parse(animation));

expect(valid).toBe(true);
});
});
Expand All @@ -46,7 +62,7 @@ describe('run schema validation', () => {
test(file, () => {
const animation = fs.readFileSync(file, 'utf8');
const valid = validate(JSON.parse(animation));

expect(valid).toBe(false);
});
});
Expand Down
11 changes: 6 additions & 5 deletions tools/lottie_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,13 @@ def handleMatch(self, match, data):
schema_obj = self.schema_data.from_path(match.group("path"))

for row in schema_obj.concrete:
tr = etree.SubElement(tbody, "tr")
prop_schema = row.properties[attribute]
etree.SubElement(etree.SubElement(tr, "td"), "code").text = repr(prop_schema.const)
prop_schema = row.properties.get(attribute)
if prop_schema:
tr = etree.SubElement(tbody, "tr")
etree.SubElement(etree.SubElement(tr, "td"), "code").text = repr(prop_schema.const)

td = etree.SubElement(tr, "td")
row.link.to_element(td, self.md)
td = etree.SubElement(tr, "td")
row.link.to_element(td, self.md)

return table, match.start(0), match.end(0)

Expand Down
6 changes: 3 additions & 3 deletions tools/schema-merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def add_vals_to_unknown_object(
types = []

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

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

Expand Down
30 changes: 18 additions & 12 deletions tools/schema_tools/type_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ def load(path):
schema_data = Schema(json.load(file))
return TypeSystem(schema_data)

def resolve_type(self, schema: Schema):
if "oneOf" in schema:
local_type = schema.get("type", None)
if local_type is not None:
return local_type
return [self.resolve_type(choice) for choice in schema / "oneOf"]
if "$ref" in schema:
return self.types[schema["$ref"]]
return schema.get("type", None)


class Type:
def __init__(self, type_system: TypeSystem, schema: Schema):
Expand All @@ -50,20 +60,10 @@ def __init__(self, type_system: TypeSystem, schema: Schema):
super().__init__(type_system, schema)
self.const = schema.get("const", None)

def resolve_type(self, schema: Schema):
if "oneOf" in schema:
local_type = schema.get("type", None)
if local_type is not None:
return local_type
return [self.resolve_type(choice) for choice in schema / "oneOf"]
if "$ref" in schema:
return self.type_system.types[schema["$ref"]]
return schema.get("type", None)

def resolve(self):
self.type = self.resolve_type(self.schema)
self.type = self.type_system.resolve_type(self.schema)
if "items" in self.schema:
self.item_type = self.resolve_type(self.schema / "items")
self.item_type = self.type_system.resolve_type(self.schema / "items")
else:
self.item_type = ""

Expand All @@ -89,6 +89,12 @@ def __init__(self, type_system: TypeSystem, schema: Schema):
def get_properties(self, schema: Schema):
if "properties" in schema:
for name, value in (schema / "properties").items():
if (
isinstance(value.schema, dict) and
len(value.schema) == 1 and
list(value.schema.keys())[0] == "not"
):
continue
self.properties[name] = Property(self.type_system, value)

def resolve(self):
Expand Down
Loading