forked from marshmallow-code/marshmallow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage_json_example.py
More file actions
51 lines (40 loc) · 1.43 KB
/
package_json_example.py
File metadata and controls
51 lines (40 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
import sys
from pprint import pprint
from packaging import version
from marshmallow import INCLUDE, Schema, ValidationError, fields
class Version(fields.Field):
"""Version field that deserializes to a Version object."""
def _deserialize(self, value, *args, **kwargs):
try:
return version.Version(value)
except version.InvalidVersion as e:
raise ValidationError("Not a valid version.") from e
def _serialize(self, value, *args, **kwargs):
return str(value)
class PackageSchema(Schema):
name = fields.Str(required=True)
version = Version(required=True)
description = fields.Str(required=True)
main = fields.Str(required=False)
homepage = fields.URL(required=False)
scripts = fields.Dict(keys=fields.Str(), values=fields.Str())
license = fields.Str(required=True)
dependencies = fields.Dict(keys=fields.Str(), values=fields.Str(), required=False)
dev_dependencies = fields.Dict(
keys=fields.Str(),
values=fields.Str(),
required=False,
data_key="devDependencies",
)
class Meta:
# Include unknown fields in the deserialized output
unknown = INCLUDE
if __name__ == "__main__":
pkg = json.load(sys.stdin)
try:
pprint(PackageSchema().load(pkg))
except ValidationError as error:
print("ERROR: package.json is invalid")
pprint(error.messages)
sys.exit(1)