包详细信息

@sketch-hq/sketch-file-format

sketch-hq82MIT6.5.0

JSON Schemas for Sketch files

sketch, sketch files, file format, file spec

自述文件

Sketch File Format

JSON Schemas for Sketch files

🙋‍♀️ If you're writing code that reads or writes Sketch file JSON, you should be implementing this file format specification – either by using the schemas to validate your input and output, installing our TypeScript types or using the schemas to generate your own model, factory or type code.

Overview

These schemas describe as closely as possible the shape of Sketch file JSON, as it appears on disk. The schemas adhere to the Draft 7 JSON Schema spec and are published to npm.

In order to aid maintainability the schema are split up into multiple reusable sub-schema in separate files, and combined in a build step. YAML is used to improve readability of the schemas, and introduced the possibility of leaving comments etc.

Potential use cases include,

  • Document the Sketch file format
  • Validate programmatically generated Sketch documents
  • Use as a data source for generating:
    • Type code, (see sketch-file-format-ts)
    • Other model and factory code
    • Schemas in other formats, e.g. GraphQL or similar

Relationship to the Sketch Mac application

Conceptually this file format spec sits upstream of Sketch, and all other projects that implement it. This means we will endeavour to release a new version of this spec before the version of Sketch that supports it.

Currently Sketch documents declare both their document version and app version. These schemas however are related to the document version only. This value can change less frequently than the Sketch Mac app but is guaranteed to increment everytime there's any change to the format of Sketch file JSON.

The table below indicates the relationship between file format spec semver, the document version and the Sketch Mac app.

File format spec semver Sketch document version Sketch Mac app
1.*.* 119 Sketch 55.2 - 57.1
2.*.* 120 Sketch 58
3.*.* 121 - 135 Sketch 59 - 71

Related projects

Usage

JavaScript/TypeScript

Add the npm module using npm or yarn,

npm install @sketch-hq/sketch-file-format

And then,

import schemas from '@sketch-hq/sketch-file-format'

The shape of the schemas object above is illustrated by the following type definition (see the schema table below for explanations):

type Schemas = {
  version: number // Latest supported Sketch document version
  versions: number[] // All supported Sketch document versions
  document: JSONSchema7
  fileFormat: JSONSchema7
  meta: JSONSchema7
  page: JSONSchema7
  user: JSONSchema7
}

HTTP

Built schemas are available to download directly over HTTP from unpkg.

From source

  1. Check you have modern versions of Yarn and Node installed
  2. Check out the repo
  3. Run yarn to setup the dependencies
  4. Run yarn build to generate the schemas into the dist folder

Other platforms

Other platforms and package managers can be supported in future, if you have any requests please open an issue.

Schemas

Schema Description YAML entrypoint Built schema
File Format Schema for a Sketch file that has been unzipped and its entries parsed into a single object, with page references replaced with concrete page objects schema/file-format.schema.yaml dist/file-format.schema.json
Document Schema for the document JSON entry in a Sketch ZIP file schema/document.schema.yaml dist/document.schema.json
Page Schema for the page JSON entries in a Sketch ZIP file schema/layers/page.schema.yaml dist/page.schema.json
Meta Schema for the meta JSON entry in a Sketch ZIP file schema/meta.schema.yaml dist/meta.schema.json
User Schema for the user JSON entry in a Sketch ZIP file schema/user.schema.yaml dist/user.schema.json
Workspace Schema for the workspace JSON entry in a Sketch ZIP file schema/workspace.schema.yaml dist/workspace.schema.json

Check the changelog for more information.

Development

This section of the readme is related to developing the file format spec. If you just want to consume the schemas you can safely ignore this.

The schema YAML files in this repo are hand-editable but tooling can be used to improve the developer experience. Node, yarn and VS Code are required to make the most of the tooling in this repo, although this sort of developer environment is purely optional.

  • The required Node version is listed in the .nvmrc file
  • Yarn 1.13 or later is required, and delegates to the Yarn binary checked in at .yarn/releases

Repository branches

Branch Description
main Main development branch
v1, v2 etc. Branches for previous major versions

Scripts

Script Description
yarn build Builds the schema and the module entrypoint to dist/
yarn validate-schemas Checks the schema for correctness against the Draft 7 meta-schema
yarn validate-reference-files Builds the schemas and uses them to validate the suite of Sketch files from the sketch-reference-files repo. You need to pass the document versions you want to validate as an argument, e.g. yarn validate-reference-files 121,122,123
yarn validate-file Validate an arbitrary Sketch file with the current schemas, e.g. yarn validate-file /absolute/path/to/file.sketch
yarn format-check Checks the repo with Prettier

Semver

The version of these file format schemas will follow semver, remaining independent of the Sketch version.

Our ambition is to remain pragmatic while selecting semver bump types. Technically even a patch change can introduce breaking changes to some clients downstream. Use the table below as a guide only.

Bump type Discussion
Major Implies the addition of major changes that may be backwards incompatible, e.g. the transition of a property from optional to required. The schemas will fail to validate Sketch documents considered valid by the previous version before the major bump
Minor Implies the addition of a new backwards compatible feature, e.g. the addition of a new optional property
Patch Implies a bug fix or trivial change, but could introduce a breaking change if a dependant package was previously implementing buggy schemas
Pre Bumps between prereleases convey no specific semantics

Workflows

Conventional commits

Try and use the conventional commits convention when writing commit messages. This isn't enforced, but you can use the yarn commit command (in place of git commit -m "foo") to open an interactive CLI to walk you through generating a properly formatted commit message.

Updating the schemas

  1. Update the schema YAML source files to reflect your changes to the specification
  2. Use the yarn validate-schemas script to check that your changes are valid according to JSON Schema Draft 7
  3. Use the yarn validate-file and yarn validate-reference-files scripts to validate real Sketch files with your updated schemas.
  4. Determine the semver bump type and call yarn changeset to create an intent to release your changes (read more about changesets here).
  5. Open a PR to main

Processing during build

While the build output is valid JSON Schema, the YAML source files are not. They include a number of approaches to aid maintainability, listed below.

Abstract schemas

Abstract schemas are a device to aid DRYness in the YAML source. They are processed out of the final build output by the assemble function.

Additional properties

The additionalProperties keyword is used by JSON Schema to define whether an object allows arbitrary extra properties on itself beyond those explicitly listed. According to the spec it defaults to true, but in order to increase strictness we set it to false on every object schema in the output, unless already present.

Required properties

The required keyword is used by JSON Schema to list object properties that must be present in order for it to be considered valid. Again, in order to increase strictness we automatically set every object property as required. If a property is genuinely optional, then it can be listed in the non-standard optional keyword, which is processed out of the build output.

更新日志

@sketch-hq/sketch-file-format

6.5.0

Minor Changes

  • 147fb8a: Add support for per-document libraries

6.4.0

Minor Changes

  • 0cf07f5: Bump document version to reflect per-document library changes
  • 2d515aa: Add per-document libraries info

6.3.1

Patch Changes

  • a1323c4: Added missing document version number

6.3.0

Minor Changes

  • 6a66fb9: Added preservesSpaceWhenHidden to SymbolInstance and updated dependencies
  • 582e216: - Add isTemplate attribute to Layer to mark it as template for insertion of copies elsewhere.
    • Artboards now contain a prototypeViewport attribute with a PrototypeViewport object to describe the associated viewport for prototypes, replacing the untyped presetDictionary attribute.
    • FlowConnection now contains an automatic destination option.

6.2.1

Patch Changes

  • 52af1d1: Bump document version reflecting changes in Sketch.

6.2.0

Minor Changes

  • e22c7f3: Add a new export prefix naming scheme
  • 4ca5853: Add strikethroughStyle property to text style.
  • 91a0f47: Add cornerStyle to curve point

Patch Changes

  • bc328f6: Bump Sketch document version.
  • 28270c7: Update dependencies

6.2.0-next.0

Minor Changes

  • e22c7f3: Add a new export prefix naming scheme
  • 4ca5853: Add strikethroughStyle property to text style.
  • 91a0f47: Add cornerStyle to curve point

Patch Changes

  • bc328f6: Bump Sketch document version.
  • 28270c7: Update dependencies

6.1.1

Patch Changes

  • 9853403: Revert adding didManuallyDisableResizingContent

6.1.0

Minor Changes

  • dd2e77d: Added didManuallyDisableResizingContent property to stop resizesContent from being enabled automatically when setting a constraint on a child layer.

Patch Changes

  • d5af64e: Updated dependencies

6.0.3

Patch Changes

  • fd65032: Removed fonts property from meta.json

6.0.2

Patch Changes

  • 1ceb662: Remove includeInCloudUpload

6.0.1

Patch Changes

  • c7c1df1: Reorganised source code and simplified build process

5.2.3

Patch Changes

  • 34bb488: Add schemas for coedit patch info.
  • 0ec06d1: Migration to monorepo

3.7.3

Patch Changes

  • c76a8a9: Remove unnecessary escape characters in UUID pattern

3.7.2

Patch Changes

  • 0464c6b: Bump version and add coeditCompatibilityVersion

3.7.1

Patch Changes

  • d3ac7d2: Make symbol source inherit directly from artboard
  • 4176aa2: Fix layers property to only allow correct types in pages, artboards and other layer types

3.7.0

Minor Changes

  • 91b0885: Add missingLibraryFontAcknowledged property and increase document version to 135.

Patch Changes

  • f2a5494: Fix overrideName regex to support layerStyle and nested symbols
  • b42a651: Remove changeIdentifier from symbol master

3.6.9

Patch Changes

  • 65693ef: Add missing bundle identifiers for feature preview variant

3.6.8

Patch Changes

  • 3f9aa0c: Add support for Sketch Feature Preview variant

3.6.7

Patch Changes

  • 36fa40f: Support document version 134, which adds do_objectID properties to object container schemas.

3.6.6

Patch Changes

  • fe001be: Fix the boolean operation enum mapping incorrect values.

3.6.5

Patch Changes

  • 19b357e: Do not define Assistants workspace item here in the file format. Assistants workspace is now only defined in the Assistant Types.

3.6.4

Patch Changes

  • 40af149: Document version bump to 133 (no other schema changes).

3.6.3

Patch Changes

  • b2e06cd: Add support for document version 132 and the documentState property.

3.6.2

Patch Changes

  • b46c865: Remove unused property dependencyMetadata from Assistants workspace, and make the workspace itself optional.
  • a285772: Support document version 131 and remove unused props related to embedded fonts.

3.6.1

Patch Changes

  • 922d77f: Fix embedded font related properties to match changes made during feature's development.
  • fda5f6c: Make file reference and data reference schemas generalized.

3.6.0

Minor Changes

  • 3543475: Add support for document version 130 (color swatches).

3.5.6

Patch Changes

  • 7b43342: Add workspace schema.

3.5.5

Patch Changes

  • d0e73e9: Add support for agreedToFontEmbedding property

3.5.4

Patch Changes

  • 384590c: Support document version 128

3.5.3

Patch Changes

  • 47d6e83: Fix incorrect enum values in layout axis schema

3.5.2

Patch Changes

  • fa41d87: Bump to document version 127, no other file format changes are associated with this change

3.5.1

Patch Changes

  • 2f53dc8: Add embedded prop to Embedded Font Reference schema

3.5.0

Minor Changes

  • 03e3835: Add support for document model version 126 and embedded fonts

3.4.4

Patch Changes

  • eebcfe5: add support for document version 125 and Sketch 64

3.4.3

Patch Changes

  • fec69b2: add missing text behaviour enum
  • 328a478: add changesets
  • fec69b2: add missing optional props to user schema